Avatar of altariamx2003
altariamx2003Flag 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
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

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???
Avatar of kaufmed
kaufmed
Flag of United States of America image

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.
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

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??
SOLUTION
Avatar of adriankohws
adriankohws
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

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
Avatar of adriankohws
adriankohws
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

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
Avatar of adriankohws
adriankohws
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

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???
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of adriankohws
adriankohws
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

ASKER

hi adrian

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

"Null Reference Exception was unhandled"
User generated image
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

ASKER

ok

I thing youre right

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

thanks anyway
Avatar of altariamx2003
altariamx2003
Flag of Mexico image

ASKER

thanks a lot for your time
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo