how to use a two dimensional array as class property in visual basic .net

altariamx2003
altariamx2003 used Ask the Experts™
on
Hi
I use the following code to declare a simple array as a class property
Class data
    Private dmyarray() As Boolean
    Public Property myarray() As Boolean()
        Get
            Return dmyarray
        End Get
        Set(ByVal value As Boolean())
            dmyarray = value
        End Set
    End Property

   
End Class

Open in new window



I would like to know how to do same but with a two dimensional array as a class property in visual basic .net
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I found the solution bymyself

this is what i need to do

Class data
    Private dmyarray(,) As Boolean
    Public Property myarray() As Boolean(,)
        Get
            Return dmyarray
        End Get
        Set(ByVal value As Boolean(,))
            dmyarray = value
        End Set
    End Property

   
End Class

Open in new window


But checking on several forums a lot of programmers says that is better to use a list instead an array as property

Somebody know the reazon of that???
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
A list (and the other collections found in the Framework) is usually easier to work with. It exposes methods which automatically take care of resizing the internal storage (itself an array) for you. You just worry about adding and removing elements.

Author

Commented:
ok

would you have and example about how to work with list as properties in vb class???

I found this example but I dont know what im doing wrong
Public Class world
    Public Class strings
        Private _pruebas As New List(Of StringsForWorld)
        Public Property strings() As List(Of world.StringsForWorld)
            Get
                Return _pruebas
            End Get
            Set(ByVal value As List(Of world.StringsForWorld))
                _pruebas = value
            End Set
        End Property

        Public Sub New()
            strings = New List(Of StringsForWorld)
        End Sub
    End Class

    Public Class StringsForWorld
        Private _StringA As String
        Public Property StringA() As String
            Get
                Return _StringA
            End Get
            Set(ByVal value As String)
                _StringA = value
            End Set
        End Property
        Private _StringB As String

        Public Property StringB() As String
            Get
                Return _StringB
            End Get
            Set(ByVal value As String)
                _StringB = value
            End Set
        End Property

        Public Sub New(ByVal FirstString As String, ByVal SecondString As String)
            StringA = FirstString
            StringB = SecondString
        End Sub
    End Class
End Class

Open in new window


When I tried to use it
Dim test As New world
        test.strings.Add(New world.StringsForWorld("a", "b"))

but Vb always says that I need to change test for world

what im doing wrong??
Learn 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.

I found "ArrayList" is much a better control than all arrays. It holds items you can created within an ArrayList, try it, you will love it.

Author

Commented:
arraylist???

mmmm let me check it, do you have an example???

and what about list???

I would like to try it to

Do you have an example about how to add a list as property in a vb class????
Dim pcSQL As String
        Dim ProductList As New ArrayList()
        Dim ProdCodeSearch As String
        Dim InstrumentSearch As String

        pcSQL = " select distinct instrument_name, product_code from tblXref order by instrument_name "
        Dim DBConn As SqlConnection
         DBConn = New SqlConnection(SQLString)
         DBConn.Open()

        Dim reader As SqlDataReader
            Dim DBCommand As New SqlCommand(pcSQL, DBConn)      
        reader = DBCommand.ExecuteReader()

        While reader.Read()
        ProductList.Add(reader(0),reader(1))
        End While

        dProdCodeSearch.DataSource = reader
        dProdCodeSearch.DataTextField  = "instrument_name"
        dProdCodeSearch.DataValueField = "product_code"    
        dProdCodeSearch.DataBind()
        reader.Close()


....
.....

Author

Commented:
Thanks for your example

It help me to add an arralist as property in a class
Public Class test
    Private _NmyProperty As New ArrayList
    Public Property myProperty() As ArrayList
        Get
            Return _NmyProperty
        End Get
        Set(ByVal value As ArrayList)
            _NmyProperty = value
        End Set
    End Property
End Class

Open in new window


I would like to know if it is possible to do the same with a two-dimentional arraylist?
Private _NmyProperty As New ArrayList()

Or you can add an Arraylist into an Arraylist item.

ArrayList1.Add(ArrayList2)

Author

Commented:
Would this work??

I did this
public class test
    Private dmylist(,) As ArrayList
    Public Property mylist() As ArrayList(,)
        Get
            Return dmylist
        End Get
        Set(ByVal value As ArrayList(,))
            dmylist = value
        End Set
    End Property
End class

Open in new window


but when I tried to use it the add method it is not part of my arraylist property, what Im doing wrong???
Most Valuable Expert 2012
Top Expert 2014
Commented:
What would you be storing in this property? May be use a DataTable?
Private dmylist As ArrayList()

and not what you did. If you wish to store many things, then as CodeCruiser mentioned will be handful

Author

Commented:
hi adrian

it works but shows the following error when I tried to use it.

"Null Reference Exception was unhandled"
example
Most Valuable Expert 2012
Top Expert 2014

Commented:
As I said in your other question

>I read that Many programmers in diferents blogs says  that it is better to use list instead of arrays

That does not just apply to everyone. Arrays exist for a reason. If arrays are doing the job for you and you do not have any issues then I think you do not need to convert to lists.

Author

Commented:
ok

I thing youre right

I would have liked to know how to do it, but I need to continue

thanks anyway

Author

Commented:
thanks a lot for your time

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial