improving my writing - 2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kaypee

    #1

    improving my writing - 2005

    below is what I have as part of registry class


    Public Function DoesKeyExist() As Boolean

    Dim oReg As RegistryKey

    Dim bExist As Boolean = False

    Try

    oReg = m_MSRegRoot.Ope nSubKey(m_sKeyP ath, False)

    If Not oReg Is Nothing Then

    bExist = True

    End If

    Catch ex As Exception

    '

    Finally

    If Not oReg Is Nothing Then

    oReg.Close()

    oReg = Nothing

    End If

    End Try

    Return bExist

    End Function





    2005 IDE warns 'Variable oReg is used before it has been assigned a value.
    A null reference exception could result at runtime'



    My question is - how am I supposed to do this?






  • Cor Ligthert [MVP]

    #2
    Re: improving my writing - 2005

    Terry,
    [color=blue]
    > dim oReg as RegistryKey = Nothing[/color]

    I strongly disagree with you. The only thing what you do with this is
    creating extra code and disable the checking on wrong used code.

    Cor


    Comment

    • Terry Olsen

      #3
      Re: improving my writing - 2005

      When you declare the variable, assign it an initial value...such as:

      dim oReg as RegistryKey = Nothing

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Kaypee

        #4
        Re: improving my writing - 2005

        :-0

        OMG. I use that in most other places.

        Why did my brain not associate that with the error message recieved?


        Thanks Terry


        "Terry Olsen" <tolsen64@hotma il.com> wrote in message
        news:%23cQm%23f NtFHA.1136@TK2M SFTNGP12.phx.gb l...[color=blue]
        > When you declare the variable, assign it an initial value...such as:
        >
        > dim oReg as RegistryKey = Nothing
        >
        > *** Sent via Developersdex http://www.developersdex.com ***[/color]


        Comment

        • Cor Ligthert [MVP]

          #5
          Re: improving my writing - 2005


          "Kaypee"[color=blue]
          >
          >
          > Public Function DoesKeyExist() As Boolean
          > Dim oReg As RegistryKey
          > Dim bExist As Boolean = False
          > Try
          > oReg = m_MSRegRoot.Ope nSubKey(m_sKeyP ath, False)
          > If Not oReg Is Nothing Then
          > bExist = True
          > End If
          > Catch ex As Exception
          > '
          > Finally
          > If Not oReg Is Nothing Then
          > oReg.Close()
          > oReg = Nothing
          > End If
          > End Try
          > Return bExist
          > End Function
          >[/color]
          As the warning tells you have a look at the use of oReg, in most places you
          have a lot of code around it, as far as I can see it now, does that code
          nothing.

          Cor


          Comment

          • Cor Ligthert [MVP]

            #6
            Re: improving my writing - 2005

            Terry,
            [color=blue]
            > dim oReg as RegistryKey = Nothing[/color]

            I strongly disagree with you. The only thing what you do with this is
            creating extra code and disable the checking on wrong used code.

            Cor


            Comment

            • Dennis

              #7
              Re: improving my writing - 2005

              Cor, I agree that there should be no need to assign a variable to nothing.
              Is there a way to turn off these types of warning's in VB 2005? They would
              be quite annoying. I haven't gotten VB2005 yet but do plan to do so when it
              comes out and am trying to learn as much about it as I can from this group.
              --
              Dennis in Houston


              "Cor Ligthert [MVP]" wrote:
              [color=blue]
              >
              > "Kaypee"[color=green]
              > >
              > >
              > > Public Function DoesKeyExist() As Boolean
              > > Dim oReg As RegistryKey
              > > Dim bExist As Boolean = False
              > > Try
              > > oReg = m_MSRegRoot.Ope nSubKey(m_sKeyP ath, False)
              > > If Not oReg Is Nothing Then
              > > bExist = True
              > > End If
              > > Catch ex As Exception
              > > '
              > > Finally
              > > If Not oReg Is Nothing Then
              > > oReg.Close()
              > > oReg = Nothing
              > > End If
              > > End Try
              > > Return bExist
              > > End Function
              > >[/color]
              > As the warning tells you have a look at the use of oReg, in most places you
              > have a lot of code around it, as far as I can see it now, does that code
              > nothing.
              >
              > Cor
              >
              >
              >[/color]

              Comment

              • Larry Lard

                #8
                Re: improving my writing - 2005

                I am confused...

                Kaypee wrote:[color=blue]
                > below is what I have as part of registry class
                >
                >
                > Public Function DoesKeyExist() As Boolean
                > Dim oReg As RegistryKey
                > Dim bExist As Boolean = False
                > Try
                > oReg = m_MSRegRoot.Ope nSubKey(m_sKeyP ath, False)
                > If Not oReg Is Nothing Then
                > bExist = True
                > End If
                > Catch ex As Exception
                > '
                > Finally
                > If Not oReg Is Nothing Then
                > oReg.Close()
                > oReg = Nothing
                > End If
                > End Try
                > Return bExist
                > End Function
                >
                > 2005 IDE warns 'Variable oReg is used before it has been assigned a value.
                > A null reference exception could result at runtime'[/color]

                Terry Olsen wrote:[color=blue]
                > When you declare the variable, assign it an initial value...such as:
                >
                > dim oReg as RegistryKey = Nothing[/color]

                Terry,

                Perhaps you could explain to me why the IDE is complaining? As far as I
                can see the variable is declared and then immediately assigned a value
                before ever being accessed. So what's the problem?

                --
                Larry Lard
                Replies to group please

                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: improving my writing - 2005

                  Dennis,

                  Because I have placed something about this somewhere else, did I get a
                  message from Herfried. (We agree about this before you misunderstand it).

                  What you ask is in the properties from myproject in the solution explorer.

                  There you see when you open that a tabpage with tabs in front

                  There you can tell at 'compile' what is a 'warning' 'none' or an 'error'.

                  My opinion is that this is something that should be set at end of
                  programming to see if there are some things not completly done. And when the
                  warning is correct set of again to none, to prevent that more important
                  warnings are overseen.

                  I hope this helps,

                  Cor


                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: improving my writing - 2005

                    Dennis

                    My message can be read wrong. The text is mine not from Herfried.

                    Cor


                    Comment

                    • Dennis

                      #11
                      Re: improving my writing - 2005

                      Cor, I agree that there should be no need to assign a variable to nothing.
                      Is there a way to turn off these types of warning's in VB 2005? They would
                      be quite annoying. I haven't gotten VB2005 yet but do plan to do so when it
                      comes out and am trying to learn as much about it as I can from this group.
                      --
                      Dennis in Houston


                      "Cor Ligthert [MVP]" wrote:
                      [color=blue]
                      >
                      > "Kaypee"[color=green]
                      > >
                      > >
                      > > Public Function DoesKeyExist() As Boolean
                      > > Dim oReg As RegistryKey
                      > > Dim bExist As Boolean = False
                      > > Try
                      > > oReg = m_MSRegRoot.Ope nSubKey(m_sKeyP ath, False)
                      > > If Not oReg Is Nothing Then
                      > > bExist = True
                      > > End If
                      > > Catch ex As Exception
                      > > '
                      > > Finally
                      > > If Not oReg Is Nothing Then
                      > > oReg.Close()
                      > > oReg = Nothing
                      > > End If
                      > > End Try
                      > > Return bExist
                      > > End Function
                      > >[/color]
                      > As the warning tells you have a look at the use of oReg, in most places you
                      > have a lot of code around it, as far as I can see it now, does that code
                      > nothing.
                      >
                      > Cor
                      >
                      >
                      >[/color]

                      Comment

                      • Larry Lard

                        #12
                        Re: improving my writing - 2005

                        I am confused...

                        Kaypee wrote:[color=blue]
                        > below is what I have as part of registry class
                        >
                        >
                        > Public Function DoesKeyExist() As Boolean
                        > Dim oReg As RegistryKey
                        > Dim bExist As Boolean = False
                        > Try
                        > oReg = m_MSRegRoot.Ope nSubKey(m_sKeyP ath, False)
                        > If Not oReg Is Nothing Then
                        > bExist = True
                        > End If
                        > Catch ex As Exception
                        > '
                        > Finally
                        > If Not oReg Is Nothing Then
                        > oReg.Close()
                        > oReg = Nothing
                        > End If
                        > End Try
                        > Return bExist
                        > End Function
                        >
                        > 2005 IDE warns 'Variable oReg is used before it has been assigned a value.
                        > A null reference exception could result at runtime'[/color]

                        Terry Olsen wrote:[color=blue]
                        > When you declare the variable, assign it an initial value...such as:
                        >
                        > dim oReg as RegistryKey = Nothing[/color]

                        Terry,

                        Perhaps you could explain to me why the IDE is complaining? As far as I
                        can see the variable is declared and then immediately assigned a value
                        before ever being accessed. So what's the problem?

                        --
                        Larry Lard
                        Replies to group please

                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: improving my writing - 2005

                          Dennis,

                          Because I have placed something about this somewhere else, did I get a
                          message from Herfried. (We agree about this before you misunderstand it).

                          What you ask is in the properties from myproject in the solution explorer.

                          There you see when you open that a tabpage with tabs in front

                          There you can tell at 'compile' what is a 'warning' 'none' or an 'error'.

                          My opinion is that this is something that should be set at end of
                          programming to see if there are some things not completly done. And when the
                          warning is correct set of again to none, to prevent that more important
                          warnings are overseen.

                          I hope this helps,

                          Cor


                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: improving my writing - 2005

                            Dennis

                            My message can be read wrong. The text is mine not from Herfried.

                            Cor


                            Comment

                            • Kaypee

                              #15
                              Re: improving my writing - 2005

                              Larry

                              From what I can figure:

                              "Larry Lard" <larrylard@hotm ail.com> wrote in message
                              news:1126272665 .961485.82420@g 49g2000cwa.goog legroups.com...[color=blue]
                              >I am confused...
                              >
                              > Kaypee wrote:[color=green]
                              >> below is what I have as part of registry class
                              >>
                              >>
                              >> Public Function DoesKeyExist() As Boolean
                              >> Dim oReg As RegistryKey
                              >> Dim bExist As Boolean = False
                              >> Try[/color][/color]

                              it is possible that exception may occur in next line - thus nothing ever
                              assigned to oReg
                              execution moves onto Catch and then Finally
                              [color=blue][color=green]
                              >> oReg = m_MSRegRoot.Ope nSubKey(m_sKeyP ath, False)
                              >> If Not oReg Is Nothing Then
                              >> bExist = True
                              >> End If
                              >> Catch ex As Exception
                              >> '
                              >> Finally[/color][/color]

                              thus it may get here before oReg actually used?
                              [color=blue][color=green]
                              >> If Not oReg Is Nothing Then
                              >> oReg.Close()
                              >> oReg = Nothing
                              >> End If
                              >> End Try
                              >> Return bExist
                              >> End Function
                              >>
                              >> 2005 IDE warns 'Variable oReg is used before it has been assigned a
                              >> value.
                              >> A null reference exception could result at runtime'[/color]
                              >
                              > Terry Olsen wrote:[color=green]
                              >> When you declare the variable, assign it an initial value...such as:
                              >>
                              >> dim oReg as RegistryKey = Nothing[/color]
                              >
                              > Terry,
                              >
                              > Perhaps you could explain to me why the IDE is complaining? As far as I
                              > can see the variable is declared and then immediately assigned a value
                              > before ever being accessed. So what's the problem?
                              >
                              > --
                              > Larry Lard
                              > Replies to group please
                              >[/color]


                              Comment

                              Working...