Results 1 to 7 of 7

Thread: Initialising an array of Structures

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    36

    Question Initialising an array of Structures

    Hi,

    let's hope I can explain this clearly! I have a Structure, and I am having trouble initialising it. Specifically when I try to populate the Vector3 variables, it tells me that they are Nothing, so I need to make them New when the array of this structure is declared.

    Code:
    Structure tTrackPoint
            Dim id
            Dim obj As DarkGDK.Basic3D.Cube
            Dim pos As DarkGDK.Math.Vector3
            Dim handle1 As DarkGDK.Math.Vector3
            Dim handle2 As DarkGDK.Math.Vector3
            Dim handle1Obj As DarkGDK.Basic3D.Cube
            Dim handle2Obj As DarkGDK.Basic3D.Cube
            Dim width As Double
    
        End Structure
    Next, I added a Sub New to the structure:

    Code:
            Public Sub New()
                pos = New DarkGDK.Math.Vector3
                handle1 = New DarkGDK.Math.Vector3
                handle2 = New DarkGDK.Math.Vector3
            End Sub
    Now my problem is that I must supply a parameter, the syntax error being:

    Structures cannot declare a non-shared 'Sub New' with no parameters.

    I have no parameter to supply, and this will be an array so I'm not even sure how I would supply it.
    The second option to fix the error is to make the Sub New shared, but then I get other errors:

    Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.


    Does anyone know how I can resolve this. My end goal is to have an array of this structure type with the Vector3s ready to use.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Initialising an array of Structures

    You shouldn't be using a structure, plain and simple. Tour tTrackPoint type should be a class. Structures have to obey certain rules and if your type can't obey those rules then it can't be a structure. You can't initialise fields of a structure where they're declared and using a constructor is not a solution because you can't enforce use of that constructor.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    36

    Re: Initialising an array of Structures

    Thanks for the response. I'm converting code from a procedural language and this was my halfway house until I could make everything into classes. I guess this one needs to make the change now rather than later.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Initialising an array of Structures

    Quote Originally Posted by batvink View Post
    Thanks for the response. I'm converting code from a procedural language and this was my halfway house until I could make everything into classes. I guess this one needs to make the change now rather than later.
    The only thing you need to change is the word "Structure" in the declaration into "Class". You can then initialise the fields where they're declared and/or in a constructor. Classes are not something that's really complex.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    36

    Re: Initialising an array of Structures

    Thanks again, I see how the conversion is so easy.

    The issue I have now though, is that the whole object is Nothing, so I somehow have to initialise it. The problem I'm struggling to get over is that it is used for an array. When the program starts I have no idea what the array size will be, so I simply:

    Code:
    Public arrTrackPoint() As TrackPoint
    Later, I redim to the correct size:

    Code:
    ReDim arrTrackPoint(Me.iSections)
    but at this point it's Nothing. As soon as I reference any of the variables it crashes:

    Object reference not set to an instance of an object.

    Debugging the class Sub New(), I can see it does not get called when creating objects like this, and because it's an array I can't use the New keyword.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Initialising an array of Structures

    That's part of the difference between classes and structures. Because structures are value types, i.e. a variable contains the value itself, when you declare a variable of a structure type you have inherently created an instance of that structure. The problem is though, no constructor has been executed, so your fields haven't been initialised.

    Classes are reference types, i.e. a variable contains a reference to an object. That means that when you declare a variable it is initially Nothing, i.e. it refers to no object. You have to use the New key word to invoke a constructor and create an instance, which guarantees that your fields will be initialised.

    Now, array elements are just like variables, so if your array elements are declared as a reference type then, when you create the array, all its elements are initially Nothing, just like an egg carton with no eggs in it. It's up to you to create each new object and assign them to the elements. You would normally do that with a For loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    36

    Thumbs up Re: Initialising an array of Structures

    It's up to you to create each new object and assign them to the elements. You would normally do that with a For loop
    Thanks again, I'm comfortable with that approach. Sometimes you get the feeling that .net will have some shortcut to everything, thankfully in this case I get to write a few more lines of code!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width