variable usage

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

    #1

    variable usage

    Hello-

    Let me start out by saying that I am new to .net and was only a hobby
    vb6 programmer.

    I am getting the warning:

    "Variable 'arrOutputData' is used before it has been assigned a value.
    A null reference exception could result at runtime."

    I get this warning in the line:

    "arrOutputData( i).Days = sinDays"

    I also have the following in my code:

    Structure Data
    Dim Days As Single
    Dim Events As Integer
    Dim EventsPerWeek As Single
    Dim TotalTime As TimeSpan
    Dim TimePerWeek As TimeSpan
    Dim TimePerEvent As TimeSpan
    Dim TimeMin As TimeSpan
    Dim TimeMax As TimeSpan
    End Structure

    Dim arrOutputData() As Data
    Dim sinDays As Single

    I don't understand why I get a warning saying that the variable is used
    before it has been assigned a value when I am trying to assign it a
    value at that point in the code.

    Any help is appreciated. Thank you.

  • Tiago Salgado

    #2
    Re: variable usage

    Try to put this instead of the line "Dim arrOutputData() As Data":

    Dim arrOutputData() As New Data



    On Mon, 10 Jul 2006 18:54:20 +0100, krollenhagen
    <keith.rollenha gen@gmail.comwr ote:
    Hello-
    >
    Let me start out by saying that I am new to .net and was only a hobby
    vb6 programmer.
    >
    I am getting the warning:
    >
    "Variable 'arrOutputData' is used before it has been assigned a value.
    A null reference exception could result at runtime."
    >
    I get this warning in the line:
    >
    "arrOutputData( i).Days = sinDays"
    >
    I also have the following in my code:
    >
    Structure Data
    Dim Days As Single
    Dim Events As Integer
    Dim EventsPerWeek As Single
    Dim TotalTime As TimeSpan
    Dim TimePerWeek As TimeSpan
    Dim TimePerEvent As TimeSpan
    Dim TimeMin As TimeSpan
    Dim TimeMax As TimeSpan
    End Structure
    >
    Dim arrOutputData() As Data
    Dim sinDays As Single
    >
    I don't understand why I get a warning saying that the variable is used
    before it has been assigned a value when I am trying to assign it a
    value at that point in the code.
    >
    Any help is appreciated. Thank you.
    >


    --
    Tiago Salgado

    Comment

    • Theo Verweij

      #3
      Re: variable usage

      VB6 types and .Net structures are not the same; a .net structure is a
      class (yes, you can add functions and properties), and therefore, you
      have to use the new keyword to instantiate the class.

      The way you did it, is comparable to the following vb6 code:

      dim x as aClass
      x.property = 5

      The last line will give you a runtime error, because the class x hasn't
      been instantiated.
      In .Net, the compiler detects these errors, so you get them at compile
      time instead of runtime.


      krollenhagen wrote:
      Hello-
      >
      Let me start out by saying that I am new to .net and was only a hobby
      vb6 programmer.
      >
      I am getting the warning:
      >
      "Variable 'arrOutputData' is used before it has been assigned a value.
      A null reference exception could result at runtime."
      >
      I get this warning in the line:
      >
      "arrOutputData( i).Days = sinDays"
      >
      I also have the following in my code:
      >
      Structure Data
      Dim Days As Single
      Dim Events As Integer
      Dim EventsPerWeek As Single
      Dim TotalTime As TimeSpan
      Dim TimePerWeek As TimeSpan
      Dim TimePerEvent As TimeSpan
      Dim TimeMin As TimeSpan
      Dim TimeMax As TimeSpan
      End Structure
      >
      Dim arrOutputData() As Data
      Dim sinDays As Single
      >
      I don't understand why I get a warning saying that the variable is used
      before it has been assigned a value when I am trying to assign it a
      value at that point in the code.
      >
      Any help is appreciated. Thank you.
      >

      Comment

      • Chris Dunaway

        #4
        Re: variable usage

        Theo Verweij wrote:
        a .net structure is a
        class (yes, you can add functions and properties), and therefore, you
        A .Net structure is *not* a class, even though you can functions and
        properties.
        have to use the new keyword to instantiate the class.
        And you do *not* have to use new when instantiating them:

        Public Structure MyStruct
        Public AnInteger As Integer
        Public AString As String
        End Structure


        'Note that New is not required
        Dim structInstance As MyStruct

        structInstance. AnInteger = 4
        structInstance. AString = "Hello"

        This is not to say that you cannot have a constructor for your
        Structure.

        Also, structures are stored differently in memory than classes. See
        this article:



        Chris

        Comment

        • Chris Dunaway

          #5
          Re: variable usage

          krollenhagen wrote:
          "Variable 'arrOutputData' is used before it has been assigned a value.
          A null reference exception could result at runtime."
          >
          I get this warning in the line:
          >
          "arrOutputData( i).Days = sinDays"
          >
          But where did you assign a value to arrOutputData ? This looks like an
          array, perhaps you forgot to initialize the array? Can you show the
          code where you declare arrOutputData and how you initialize it?

          Comment

          • Branco Medeiros

            #6
            Re: variable usage


            krollenhagen wrote:
            I am getting the warning:
            >
            "Variable 'arrOutputData' is used before it has been assigned a value.
            A null reference exception could result at runtime."
            >
            I get this warning in the line:
            >
            "arrOutputData( i).Days = sinDays"
            >
            I also have the following in my code:
            >
            Structure Data
            Dim Days As Single
            Dim Events As Integer
            Dim EventsPerWeek As Single
            Dim TotalTime As TimeSpan
            Dim TimePerWeek As TimeSpan
            Dim TimePerEvent As TimeSpan
            Dim TimeMin As TimeSpan
            Dim TimeMax As TimeSpan
            End Structure
            >
            Dim arrOutputData() As Data
            Dim sinDays As Single
            The Visual Studio IDE performs a data flow analisys in your code to
            catch, among other things, when you access members of non-initialized
            classes: accessing a member of a class that is still Nothing will
            result in a run-time error, as you may know.

            Now, arrays, in .Net, are classes -- you can, for instance, verify if
            an array is Nothing (well, you can do the same with strings, also).
            Therefore, you must instanciate the array before accessing an item of
            it.

            One way of doing this is like in VB6:

            Dim arrOutputData(0 to 99) As Data

            If you don't know how many items your array will have, you may declare
            an empty array:

            'Creates an empty array
            Dim arrOutputData() As Data = {}

            Or

            'Creates an empty array also
            Dim arrOutputData(-1) As Data

            Afterwards you're free to Redim the array:

            Dim Max As Integer = arrOutputData.G etLowerBound(0)
            Redim Preserve arrOutputData(0 To Max + 1)

            And *then* access items from the array =))

            Regards,

            Branco.

            Comment

            • Michel Posseth  [MCP]

              #7
              Re: variable usage

              To prevent the nagging warning a lot of programmers have already set the
              option off ( cause it warns sometimes without a good reasson )

              sometimes the warning is even wrong when using a select case for instance
              where you assign in every case a value
              what will happen is that it will still display the nag warning that it
              hasn`t been assigned a value or that a function doesn`t return a value on
              all codepaths while it does

              so to prevent this i learned myself to

              1. assign a Nothing pointer after delcaration when such a situation occurs

              2. use the return statement in functions where i use a slect case and as
              last codeline i return a default value ( wich will never get executed )

              regards

              Michel Posseth [MCP]




              "Chris Dunaway" <dunawayc@gmail .comschreef in bericht
              news:1152560903 .794268.16470@7 5g2000cwc.googl egroups.com...
              krollenhagen wrote:
              >
              >"Variable 'arrOutputData' is used before it has been assigned a value.
              >A null reference exception could result at runtime."
              >>
              >I get this warning in the line:
              >>
              >"arrOutputData (i).Days = sinDays"
              >>
              >
              But where did you assign a value to arrOutputData ? This looks like an
              array, perhaps you forgot to initialize the array? Can you show the
              code where you declare arrOutputData and how you initialize it?
              >

              Comment

              • krollenhagen

                #8
                Re: variable usage

                I declared the array with the statement:

                Dim arrOutputData() As Data

                I don't know the length of the array when I am declaring it. Is that
                why I have to initialize the array? If I were to redim the array,
                would I have to initialize it?

                Thanks for the help so far.

                Keith

                Chris Dunaway wrote:
                krollenhagen wrote:
                >
                "Variable 'arrOutputData' is used before it has been assigned a value.
                A null reference exception could result at runtime."

                I get this warning in the line:

                "arrOutputData( i).Days = sinDays"
                >
                But where did you assign a value to arrOutputData ? This looks like an
                array, perhaps you forgot to initialize the array? Can you show the
                code where you declare arrOutputData and how you initialize it?

                Comment

                • M. Posseth

                  #9
                  Re: variable usage



                  you could go for a construct like this

                  Dim arrOutputData() As Data
                  Dim sindays As Single = 5 'just a dummy value
                  For i As Integer = 0 To 50
                  ReDim Preserve arrOutputData(i )
                  arrOutputData(i ).Days = sindays
                  Next

                  regards

                  Michel Posseth [MCP]



                  "krollenhag en" wrote:
                  I declared the array with the statement:
                  >
                  Dim arrOutputData() As Data
                  >
                  I don't know the length of the array when I am declaring it. Is that
                  why I have to initialize the array? If I were to redim the array,
                  would I have to initialize it?
                  >
                  Thanks for the help so far.
                  >
                  Keith
                  >
                  Chris Dunaway wrote:
                  krollenhagen wrote:
                  "Variable 'arrOutputData' is used before it has been assigned a value.
                  A null reference exception could result at runtime."
                  >
                  I get this warning in the line:
                  >
                  "arrOutputData( i).Days = sinDays"
                  >
                  But where did you assign a value to arrOutputData ? This looks like an
                  array, perhaps you forgot to initialize the array? Can you show the
                  code where you declare arrOutputData and how you initialize it?
                  >
                  >

                  Comment

                  • Branco Medeiros

                    #10
                    Re: variable usage

                    krollenhagen wrote:
                    I declared the array with the statement:
                    >
                    Dim arrOutputData() As Data
                    >
                    I don't know the length of the array when I am declaring it. Is that
                    why I have to initialize the array? If I were to redim the array,
                    would I have to initialize it?
                    <snip>

                    Redim and Redim Preserve both 'understand' non-initialized arrays,
                    therefore you may pass your aray to them and they'll do the right
                    thing, *unless*... remember that you can only access members of your
                    array after it's already initialized. So, a typical use of Redim
                    Preserve, like the following code, may fail if you forget the array
                    initialization:

                    'adding a new element to the array:
                    Redim Preserve arrOutputData(a rrOutputData.Ge tLowerBound(0) + 1)

                    The code above will fail because GetLowerBound(0 ) wil be accessing
                    Nothing. If you use this pattern, the safest approach is to initialize
                    the array on the declaration, as suggested, or use something like the
                    following:

                    'adding a new element to the array:
                    If arrOutputData Is Nothing Then
                    Redim arrOutputData(0 )
                    Else
                    Redim Preserve arrOutputData(a rrOutputData.Ge tLowerBound(0) + 1)
                    End If

                    Personally, I prefer the initialization upon declaration, so I don't
                    need to test:

                    Dim arrOutputData(-1) As Data

                    Notice that in the current version of the framework you can use generic
                    lists, which are more versatile than arrays.

                    Dim OutputData As New List(Of Data)

                    Since using generics I don't remember ever declaring an array
                    anymore... =))

                    HTH

                    Regards,

                    Branco.

                    Comment

                    Working...