Array of Objects

A

Altman

In vb 6 you use to be able to create an array of objects and I can't seem to
figure this out in .NET. I figured you can't make this but I was wondering
how I could do it if I had for instance 3 objects named Object1, Object2,
and Object3. Then I would do something like this

For n = 1 to 3
myobject = Object & n
myobject.myproperty = "whatever"
End for
 
G

Gerry O'Brien [MVP]

Dim arrObject() As ObjectName

Replace arrObject with the name you want to use for your array and
ObjectName should be replaced with the name of the class. That's all there
is to it.
 
T

Think_Fast

You mean something like this?

Dim myobject(2) As Object
Dim n As Integer
For n = 0 To 2
myobject(n) = "whatever " & n.ToString
Next

Debug.WriteLine(myobject(0))---->whatever 0
Debug.WriteLine(myobject(1))---->whatever 1
Debug.WriteLine(myobject(2))---->whatever 2
 
A

Altman

Not sure I understand this, I don't know if you understood what I was saying
either. Say I have a form with 50 textboxes on it (with the names
txtBox1,txtBox2,txtBox3...) and I want to set the text property to "hello"
upon loading. Is there an easy way to loop through this. I am a FoxPro
programmer and this is how I'd do it in foxpro

For n = 1 to 50
**lcObject is a string with the name of the object
lcObject = "txtBox" + ALLTRIM(STR(n))
**the & is "Macro Substitution", with this statement loObject is now
equal to the txtBox(n) object
loObject = &lcObject
loObject.Value = "Hello"
Endfor

Is there a way to do this?
 
Z

ZorpiedoMan

I don't recall being able to do this in VB6, either... at least not the
way you are showing it. However, it would be a simple matter of:

Dim A(2) As Object

A is now an array of three objects. Since everything in VB.NET is now
an object of some sort, you can set the value of these objects to just
about anything:

A(0) = 1
A(1) = "HELLO"
A(2) = New TextBox

Then you can buzz through them:

Debug.WriteLine A(0).Tostring
Debug.WriteLine A(1).Tostring
Debug.WriteLine A(2).Tostring


---OR---


For each Obj as Object in A
Debug.WriteLine Obj.toString
Next


Hope this helps..


--Zorpie
 
O

One Handed Man \( OHM - Terry Burns \)

Private Class aClass

Private m_name As String

Public Sub New(ByVal i As String)
m_name = i
End Sub
Public Property name() As String
Get
Return (m_name)
End Get
Set(ByVal Value As String)
m_name = Value
End Set
End Property

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim myArray() As aClass = {New aClass("Zero"), New aClass("one"),
New aClass("two")}

For x As Integer = 0 To 2

Debug.WriteLine("Object : " & myArray(x).name)
Next

End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
A

Altman

in vb6 you were able to do it by creating a object with the same name. For
Instance the easy way I always did it was to copy a control and then paste
it on the same form. Each of the objects then had an index to it. So in
that case if I wanted 50 textboxes to say "Hello" on one form I could give
them all the same name but a different index and then do this

For n = 1 to 50
txtBox(n).Text = "Hello"
next n
 
G

Guest

Hi,
Those were control arrays which are no more supported. In vb.net you can do
lots of things to achieve this. Like dynamically creating textboxes and
placing them on form and handling there events, for example:
dim txtboxes() as TextBox = new TextBox () {new TextBox() , new Textbox(),
new TextBox () }
the above code gives you 3 textboxes in an array and you can access them
like txtboxes(0), txtboxes(1), or txtboxes(2).
Or you can have textboxes name Text1, Text2, Text3 and access them through a
controls collection property. I'm not sure but I think I read in one of the
posts once that controls collection in windows forms does not have a
"findcontrol" method (which is there in asp.net) so somebody wrote it and
you'll have to look at the older posts which may help you. You can use it
like
dim mytextbox as textbox = FindControl ("txt" & i.tostring( ) )
where "i" can be your loop variable.

hope that helps.
Abubakar.
http://joehacker.blogspot.com
 
G

Gerry O'Brien [MVP]

Here is what you want.

Dim ctl As Control

For Each ctl In Me.Controls

If TypeOf ctl Is TextBox Then

ctl.Text = "Hello"

End If

Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top