To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Nov 7th, 2009, 06:01 AM   #1
batvink
Member
 
Join Date: Jun 07
Posts: 34
batvink is an unknown quantity at this point (<10)
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.
batvink is offline   Reply With Quote
Old Nov 7th, 2009, 06:30 AM   #2
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
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.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET
jmcilhinney is offline   Reply With Quote
Old Nov 7th, 2009, 06:32 AM   #3
batvink
Member
 
Join Date: Jun 07
Posts: 34
batvink is an unknown quantity at this point (<10)
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.
batvink is offline   Reply With Quote
Old Nov 7th, 2009, 06:35 AM   #4
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
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.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET
jmcilhinney is offline   Reply With Quote
Old Nov 7th, 2009, 07:21 AM   #5
batvink
Member
 
Join Date: Jun 07
Posts: 34
batvink is an unknown quantity at this point (<10)
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.
batvink is offline   Reply With Quote
Old Nov 7th, 2009, 08:40 AM   #6
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
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.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET
jmcilhinney is offline   Reply With Quote
Old Nov 7th, 2009, 08:59 AM   #7
batvink
Member
 
Join Date: Jun 07
Posts: 34
batvink is an unknown quantity at this point (<10)
Thumbs up Re: Initialising an array of Structures

Quote:
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!
batvink is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:40 AM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.