OOP question

D

Dave

Hi All,

I am new to OOP and new to VB.net and have an object/Class question. I want
to create and array of objects. Each object needs to have a Property that is
and array.



Basicly here is what I have to initialize my objects



Dim MyObject() as MyClass

For x = 0 to 5

Dim MyObject(x) as MyClass

Next



It seems to let me do the above but when I call the Method AddArray() I get
an 'NullReferenceexeption was unhandled ' error that also says 'Object
reference not set to an instance of an object.' As seen below



MyObject(x).AddMyArray(5) - This throws the error



Here is my AddMyArray method as it is in my class



Public Sub AddMyArray(ByVal value As Integer)

Dim x As Integer

Dim dblWeight(value - 1) 'Private value in my class

For x = 0 To (value - 1)

dblWeight(x) = 0

Next

End Sub



I guess what I don't understand is why my object MyObject(x) is null after I
have declared it. What am I doing wrong?
 
M

Mr. Arnold

Dave said:
Hi All,

I am new to OOP and new to VB.net and have an object/Class question. I
want
to create and array of objects. Each object needs to have a Property that
is
and array.

<snipped>

I am going to make this suggest. You need to find a book on OOP programming
in VB.Net and get the basics. It makes sense when you don't know what you're
doing. The books are out there.
 
A

Armin Zingler

Dave said:
Hi All,

I am new to OOP and new to VB.net and have an object/Class question.

I agree with Mr. Arnold, however see below...
I want to create and array of objects. Each object needs to have a
Property that is and array.



Basicly here is what I have to initialize my objects



Dim MyObject() as MyClass

For x = 0 to 5

Dim MyObject(x) as MyClass

Next



It seems to let me do the above

How does it seem? Does not work for me because the compiler complains at
the 2nd Dim. In addition, MyClass is a reserved keyword.
but when I call the Method
AddArray() I get an 'NullReferenceexeption was unhandled ' error
that also says 'Object reference not set to an instance of an
object.' As seen below



MyObject(x).AddMyArray(5) - This throws the error



Here is my AddMyArray method as it is in my class



Public Sub AddMyArray(ByVal value As Integer)

Dim x As Integer

Dim dblWeight(value - 1) 'Private value in my class

For x = 0 To (value - 1)

dblWeight(x) = 0

Next

End Sub


Where do you _add_ the array? Because that's the name of the method.
Where do you store it? Is the array a public field (aka a variable at
class level)?
I guess what I don't understand is why my object MyObject(x) is null
after I have declared it. What am I doing wrong?

Here's a working example: (just for learning purpose)

Class Class1
Public Items As Form()
End Class

'...

Dim MyObject(5) As Class1

For x = 0 To 5
MyObject(x) = New Class1
ReDim MyObject(x).Items(3)

For y As Integer = 0 To 3
MyObject(x).Items(y) = New Form
Next
Next


Armin
 
T

Trammel

Dave said:
Hi All,

I am new to OOP and new to VB.net and have an object/Class question. I
want
to create and array of objects. Each object needs to have a Property that
is
and array.



Basicly here is what I have to initialize my objects



Dim MyObject() as MyClass

For x = 0 to 5

Dim MyObject(x) as MyClass

Next
[snippage]

All your code above does is create the array 6 times without putting
anything in it. Try the following code instead:

'Place holder for array
Dim MyObject(5) as MyClass
'Loop array (0 to 5 = 6)
For x = 0 to 5
'Create new instance of MyClass and throw it in the array
MyObject(x) = New MyClass
Next

Rest should work fine (maybe rename away from starting "My" and start like
"c")
 
C

Cor Ligthert[MVP]

Trammel-,

In my idea should you first look at the complete message from Armin,
especially about myclass.
I cannot believe that the result of your code is the wish of the OP.

-Cor

Trammel said:
Dave said:
Hi All,

I am new to OOP and new to VB.net and have an object/Class question. I
want
to create and array of objects. Each object needs to have a Property that
is
and array.



Basicly here is what I have to initialize my objects



Dim MyObject() as MyClass

For x = 0 to 5

Dim MyObject(x) as MyClass

Next
[snippage]

All your code above does is create the array 6 times without putting
anything in it. Try the following code instead:

'Place holder for array
Dim MyObject(5) as MyClass
'Loop array (0 to 5 = 6)
For x = 0 to 5
'Create new instance of MyClass and throw it in the array
MyObject(x) = New MyClass
Next

Rest should work fine (maybe rename away from starting "My" and start like
"c")
 
T

Trammel

Cor Ligthert said:
Trammel-,

In my idea should you first look at the complete message from Armin,
especially about myclass.
I cannot believe that the result of your code is the wish of the OP.

-Cor

Trammel said:
Dave said:
Hi All,

I am new to OOP and new to VB.net and have an object/Class question. I
want
to create and array of objects. Each object needs to have a Property
that is
and array.



Basicly here is what I have to initialize my objects



Dim MyObject() as MyClass

For x = 0 to 5

Dim MyObject(x) as MyClass

Next
[snippage]

All your code above does is create the array 6 times without putting
anything in it. Try the following code instead:

'Place holder for array
Dim MyObject(5) as MyClass
'Loop array (0 to 5 = 6)
For x = 0 to 5
'Create new instance of MyClass and throw it in the array
MyObject(x) = New MyClass
Next

Rest should work fine (maybe rename away from starting "My" and start
like "c")

The OP says they already have a method/function named "AddArray, while
trying to call the method they get the error "NullReferenceexeption was
unhandled" this is due to him not creating an instance of a class.

He did however mention (and give an example code hinting towards) that he is
using an internal method/function of the class to add to an array

Therefor my code fixes his "NullReferenceexeption" by creating the instances
of the class and leaves him to use the method he has coded into the classes
already.

I my opinion he did not ask how to modify the class/object externally and
only wanted to know how to get it working in an array, along with its
internal addarray function/method (already coded). Either way - it gives
more examples for the OP to learn from and ultimately adapt to what he needs
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top