Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

declaring an array as an element inside of a structure..HELP

Status
Not open for further replies.

eipisquared

Programmer
May 15, 2009
6
US
This seems straight forward and simple...I want to create an array as one of the elements inside my structure, but when I go to run the code it gives me an exception...HELP!!!

Public Class Form1
Private Structure Polynomial
Dim degree As Int16
Dim coeffArray() As Double
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myPolynomials(10) As Polynomial
myPolynomials(1).degree = 3
myPolynomials(1).coeffArray(0) = 3 'I get the "Object reference not set to an instance of an object" error.
myPolynomials(1).coeffArray(1) = 2
myPolynomials(1).coeffArray(2) = -4
myPolynomials(1).coeffArray(3) = 5
End Sub
End Class

What the heck is wrong with this??? Wasn't this allowed in the past?
 
Size it:
Code:
Private Structure Polynomial
Dim degree As Int16
Dim coeffArray(3) As Double
End Structure

 
I tried that initially and got the following error..."Arrays declared as structure members cannot be declared with an initial size"...

I had done this exact thing before switching to VB.Net...I am using 2008 professional edition...

Any other ideas?
 
Well, in that case, you can size it when your declaring a Polynomial:
Code:
        Dim myPolynomials(10) As Polynomial
        [b]ReDim myPolynomials(1).coeffArray(3)[/b]
        myPolynomials(1).degree = 3
        myPolynomials(1).coeffArray(0) = 3 'I get the "Object reference not set to an instance of an object" error.
        myPolynomials(1).coeffArray(1) = 2
        myPolynomials(1).coeffArray(2) = -4
        myPolynomials(1).coeffArray(3) = 5
 
RiverGuy....Thank you...I had previously tried ReDim, but I accidentally applied it to myPolynomials() as opposed the the other array <what a dummy> , the moment I read this reply I realized my error!!!!

Thanks and have an awesome day!!!
 
This should work:
Code:
Public Class Form1
    Private Structure Polynomial
        Dim degree As Int16
        Dim coeffArray() As Double
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myPolynomials(10) As Polynomial
        Dim dbl As Double() = {3, 2, -4, 5}

        myPolynomials(1).degree = 3
        myPolynomials(1).coeffArray = dbl
    End Sub
End Class

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks Sorwen...The example I posted was not an actual example of what I am trying to do, just a simplified version, but your answer gave me an alternate idea! I appreciate it!!!

Mike
 
NP. :) Yeah, as long as you set the empty structure array to an array that is filled then you don't have to use redim. I was writing that while you were posting that it worked. :) Any time in the past when I used ReDim on arrays it caused an error. I figured RiverGuy wouldn't have posted it if it didn't work, but I wanted to add an alternative just in case. It is interesting that it works. I'll have to try what RiverGuy posted and see what is different that it works. Hmmm....maybe it is a setting I have enabled.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Interesting....what error have you received with ReDim? Was it an actual exception or just unexpected behavior? One thing to always keep in mind with ReDim is the need to use the Preserve keyword when appropriate.

But actually, I think I like Sorwen's solution better. No particular reason other than yours seems a little more ".Net style" whereas ReDim is a little more "VB6 style.
 
Yeah, I may actually do something similar to your code to kind of initialize the arrays...I wrote a program that allows teachers to graph various functions quickly and easily, now I am rewriting it so that they can display multiple functions at the same time...I want to store the information in an array and use it to graph the functions...this will work perfectly!!!

Thanks for the help and Happy Friday:)
 
Whoops, that last message was to Sorwen, I must have typed it while RiverGuy was typing his...Both solutions actually work...So thanks to both of you!!! I appreciate it!
 
'ReDim' statements can no longer be used to declare array variables.
It happens while typing, even before I complie.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top