469,281 Members | 2,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

Array of Objects

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
Nov 21 '05 #1
10 30133
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.

--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"Altman" <No******@SickOfSpam.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2
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
"Altman" <No******@SickOfSpam.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #3
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?
"Gerry O'Brien [MVP]" <gerry dot obrien at gmail dot com> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
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.

--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"Altman" <No******@SickOfSpam.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #4
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5
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

"Altman" <No******@SickOfSpam.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #6
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
"ZorpiedoMan" <No********@Beatles.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #7
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
"Altman" wrote:
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
"ZorpiedoMan" <No********@Beatles.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #8
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
"Altman" wrote:
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
"ZorpiedoMan" <No********@Beatles.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #9
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
--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"Altman" <No******@SickOfSpam.com> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
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
"ZorpiedoMan" <No********@Beatles.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #10
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
--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"Altman" <No******@SickOfSpam.com> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
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
"ZorpiedoMan" <No********@Beatles.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Kutty Banerjee | last post: by
2 posts views Thread by Raphael Iloh | last post: by
10 posts views Thread by Steve | last post: by
21 posts views Thread by yeti349 | last post: by
2 posts views Thread by JJA | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.