473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 30924
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kutty Banerjee | last post by:
Hi, MyClass *myclass_=new MyClass; and MyClass::MyClass(int) and no default constructor. My object assignment obviously gives an error. So waht is the correct way to do it without using vectors...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
1
by: plau011 | last post by:
Hi all, I thought I understood .Net Framework with value types and referencetypes but apparently not. So I have this code. Excuse me if it's not 100% correct I cut and paste and shortened it a...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
4
Ispep
by: Ispep | last post by:
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without...
13
by: milk242 | last post by:
I know I'm making a mistake, but I'm wondering if someone could tell me the type of mistake I'm making. I know I can write the whole array to a file, but I want to create separate binary files for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.