2 Dimentional array declaraion and seting size?

I would like to declare 2 D array in a class but I don't know the size yet.
How can I declare it and resize it later?


Public Class DataCollection

public Property ItemData(,) as double   < this line is not OK
  Public VaporConcentration(,) As Double  < this is ok
End
dkim18Asked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
Try

public Property ItemData() as double(,)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Pretty sure this falls under the same conditions as the answer given by CodeCruiser in your PAQ:
http://www.experts-exchange.com/Programming/Languages/.NET/Q_27869165.html#a38410554
0
 
dkim18Author Commented:
How do you handle 2 dimensional array?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
dkim18Author Commented:
I tried something like this but Itemdata(,) was asking for an identifier.


public Property ItemData(,) as double 
Get
            Array.Resize(_ItemDatas, 3,5)   
            Return _ItemData
End Get

Open in new window

0
 
djon2003Connect With a Mentor Commented:
The property declaration should be :
    Private _ItemData As Double(,)

    Public Property ItemData() As Double(,)
        Get
            ReDim Preserve _ItemData(3, 5)
            Return _ItemData
        End Get
        Set(ByVal value As Double(,))
            ' ...
        End Set
    End Property

Open in new window

0
 
dkim18Author Commented:
This looks like it is OK.

public Property ItemData() as double(,)

searched online for a while but couldn't find any example.
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Adding to djon2003's example, you can check if the array is "nothing" and then give it a size before returning it:
    Private _ItemData As Double(,)

    Public ReadOnly Property ItemData() As Double(,)
        Get
            If IsNothing(_ItemData) Then
                ReDim _ItemData(a, b)
            End If
            Return _ItemData
        End Get
    End Property

Open in new window


Where "a" and "b" are the size and could be stored as other variables that get set.  You haven't really explained how you will determine what the size needs to be.
0
 
dkim18Author Commented:
I am going to declare name only and set the size later so is it ok to just declare it?

public Property ItemData() as double(,) 

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
By using the Property wrapper you can ENSURE that it gets initialized before someone attempts to use it.

If you only declare it, you can't prevent someone from using it before it's initialized...which can lead to errors.  This is why Computer Science teachers get ulcers every time they see a Public variable declared in a class...

A key concept in OOP is ENCAPSULATION, which hides this safeguard from the end user (which could also be the programmer too!).  The property allows you to control how the data within is accessed and managed, without the end user needing to worry about it.
0
 
CodeCruiserCommented:
If you will initialize it in constructor then just declaration is fine.
0
 
djon2003Commented:
Well, I would just end this up by saying : Initialization have to be done before usage, though, where you do it is at your discretion. As long as it is before usage.

I would like to pin point the "Preserve" key I had put in the Redim code line. This was there to keep current data within the array. Use it if you need to change the length of dimension dynamically when the array is already containing data.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Regarding "Preserve" though, you can only change the LAST dimension in a multi-dimensional array.  All others must remain the same...

This doesn't apply if the array is being initialized, but then you also don't have any data to preserve.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.