Use of arrary in classes

  • Thread starter Thread starter Chris J.
  • Start date Start date
C

Chris J.

I am trying to use a array as a variable in a class, but
having problem to declare and implement it into my class.
Can anyone help me here?
 
Hi Chris,

Can you say anything about what you want to do with your array? Is it a
list, a table?

It may be that an array is what you need. Maybe an ArrayList would be
better.

Will you be passig the array into and out of the class or only items of
the array?

Regards,
Fergus
 
Chris J. said:
I am trying to use a array as a variable in a class, but
having problem to declare and implement it into my class.
Can anyone help me here?

Which problems do you have?

Declaration:
Dim Values As Integer()
The line above does _not_ create the array, it only creates a variable able
to reference an array.

Use
Redim Values(4)
to create an array containing 5 values (indexes 0 to 4). After creation, all
items are set to it's default value, i.e. zero in this case.

Or, instead of Redim:
Values = New Integer(){0, 1, 2, 3, 4}

Note that the size of an array can not be changed. You can use
Redim Preserve Values(9)
to keep the values, but internally a new array with 10 items is created and
the content of the previous array is copied to the new array.
 
OK I will try to explain the problem better. I am a newbie
at vb.net

I am reading data from a database into a class. There are
18 integer variables that i would like to put into an
array insted of declaring each.
I have tryied the following, which does not work:

Private _Hole(18) As Integer
Public Property Hole() As Integer
Get
Return _Hole(i)
End Get
Set(ByVal Value As Integer)
_Hole(i) = Value
End Set
End Property

I am still doing some reading on arraylists to see if that
can help me out.

Thanks so far.

Chris
 
Chris J. said:
I am reading data from a database into a class. There are
18 integer variables that i would like to put into an
array insted of declaring each.
I have tryied the following, which does not work:

Private _Hole(18) As Integer
Public Property Hole() As Integer
Get
Return _Hole(i)
End Get
Set(ByVal Value As Integer)
_Hole(i) = Value
End Set
End Property

Simply write

Public Property Hole(ByVal Index As Integer) As Integer

and press <Enter>, and the editor creates the procedures for you. After
adding another two lines you get:

Private _Hole(18) As Integer
Public Property Hole(ByVal Index As Integer) As Integer
Get
Return _Hole(Index)
End Get
Set(ByVal Value As Integer)
_Hole(Index) = Value
End Set
End Property

BTW, the array holds _19_ values (indexes 0 to 18).
 
Hi Chris,

What's missing there is that you're not giving Hole() a value for i. The
compiler's then complaining when it comes to Return _Hole(i) because i hasn't
been declared to it.

Here's the missing bit added:
Public Property Hole (I As Integer) As Integer

As it's looking so far, an ArrayList gives you no advantage because you
know that you'll have exactly 18 values. ArrayLists are very useful when you
don't know how many values you are going to have.

With the array as you've defined it, you know that it will, and can, only
store Integers - it's strongly typed. An ArrayList, on the other hand, takes
Objects which means it'll take anything - it's not strongly typed.

There are differences in how the memory is used too, but we won't go into
that yet. Suffice to say that an Array of Integers is considerably cheaper
than the equivalent ArrayL:ist.

Regards,
Fergus
 
Hello,

Chris J. said:
I am reading data from a database into a class. There are
18 integer variables that i would like to put into an

Notice that 'Private m_Hole(18) As Integer' will declare an array with
19 integer variables with indices 0, 1, ..., 18.
array insted of declaring each.
I have tryied the following, which does not work:

You must add an index parameter to the property:

\\\
Private m_Hole(18) As Integer

Public Property Hole(ByVal Index As Integer) As Integer
Get
Return m_Hole(Index)
End Get
Set(ByVal Value As Integer)
m_Hole(Index) = Value
End Set
End Property
///
 
Back
Top