?
Solved

2 Dimentional array declaraion and seting size?

Posted on 2012-09-20
12
Medium Priority
?
285 Views
Last Modified: 2012-09-20
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
0
Comment
Question by:dkim18
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38418021
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
 

Author Comment

by:dkim18
ID: 38418059
How do you handle 2 dimensional array?
0
 

Author Comment

by:dkim18
ID: 38418077
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 668 total points
ID: 38418099
Try

public Property ItemData() as double(,)
0
 
LVL 9

Assisted Solution

by:djon2003
djon2003 earned 668 total points
ID: 38418111
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
 

Author Comment

by:dkim18
ID: 38418149
This looks like it is OK.

public Property ItemData() as double(,)

searched online for a while but couldn't find any example.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 664 total points
ID: 38418160
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
 

Author Comment

by:dkim18
ID: 38418224
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38418273
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38418549
If you will initialize it in constructor then just declaration is fine.
0
 
LVL 9

Expert Comment

by:djon2003
ID: 38418710
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38418736
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Loops Section Overview
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question