473,765 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.myprop erty = "whatever"
End for
Nov 21 '05 #1
10 31000
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******@SickO fSpam.com> wrote in message
news:Oa******** ******@TK2MSFTN GP12.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.myprop erty = "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******@SickO fSpam.com> wrote in message
news:Oa******** ******@TK2MSFTN GP12.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.myprop erty = "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******** ******@TK2MSFTN GP12.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******@SickO fSpam.com> wrote in message
news:Oa******** ******@TK2MSFTN GP12.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.myprop erty = "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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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******@SickO fSpam.com> wrote in message
news:Oa******** ******@TK2MSFTN GP12.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.myprop erty = "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
"ZorpiedoMa n" <No********@Bea tles.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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
"findcontro l" 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
"ZorpiedoMa n" <No********@Bea tles.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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
"findcontro l" 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
"ZorpiedoMa n" <No********@Bea tles.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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******@SickO fSpam.com> wrote in message
news:eZ******** ******@TK2MSFTN GP12.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
"ZorpiedoMa n" <No********@Bea tles.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4976
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 of course. kutty
38
5228
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 let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of all files in the current dir. array contains records of all subs in the current dir
2
2041
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 expression to return true as both arrays are identically the same but it keeps returning false. Any info on how to solve this problem will be appreciated.
10
12227
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
3221
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
1
1269
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 bit to make things clearer So basically the purpose of this code was to have to arrays passed in byref _BInfo and CInfo. Then base on another list of items that gets passed through is to fill one or the other.
2
2515
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 the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
4
3828
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 saying I'm not asking you to solve the question - that won't help come exam time :(). Anyway I have a CSV delimited file in the following format; STRING,INT STRING,INT,INT,INT,INT,INT STRING,INT,INT,INT,INT,INT STRING,INT,INT,INT,INT,INT ...
13
2602
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 each Customer. Then I want to readout the binary file... either one or all of them. Could someone help me? Thanks. Customer oneArray = new Customer; oneArray = new Customer("John", "email@gmail.com", "123-345-1234", "1, 2"); oneArray =...
0
10156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.