Avatar of altariamx2003
altariamx2003
Flag for Mexico asked on

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

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
Visual Basic.NET

Avatar of undefined
Last Comment
altariamx2003

8/22/2022 - Mon
altariamx2003

ASKER
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???
kaufmed

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.
altariamx2003

ASKER
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??
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
adriankohws

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
altariamx2003

ASKER
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????
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
altariamx2003

ASKER
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?
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
altariamx2003

ASKER
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???
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
altariamx2003

ASKER
hi adrian

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

"Null Reference Exception was unhandled"
example
Nasir Razzaq

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.
altariamx2003

ASKER
ok

I thing youre right

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

thanks anyway
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
altariamx2003

ASKER
thanks a lot for your time