dynamically dimension an object

  • Thread starter Thread starter TJS
  • Start date Start date
T

TJS

how can I dynamically dimension objects in a loop ?

trying to do something like this -but it doesn't compile

for i = 1 to 5
Dim "submenu" & i As New objectName_here
next

Compiler Error Message: BC30203: Identifier expected.
 
Hi TJS

That one is absolutely impossible, but i can suggest you can alternate way of doing in the same way, that is identifying the objects with some name

look at the following code

Dim ht As New Hashtable(
Dim i As Intege
For i = 0 To
ht.Add("Obj" & i, "" & i
Nex
MsgBox(CType(ht("Obj1"), String)

But remember the key is case sensitive

I hope that this could help u a little

Sadha Sivam
Microsoft Community Star
Malleable Minds Software Pvt Ltd., India
(e-mail address removed)
 
You could probably do this using the CodeDOM but it would be non-trivial.
Have you thought about using an array (or a collection) like:

Dim a(4) As objectName
For i As Integer = 0 to 4
a(i) = New objectName(...)
Next
 
Hi TJS,

Before you get confuse, although the one from Rob is simpler, I would try
the sample TJS did give you. When you only reference them by index you can
also use the arraylist as an alternative, however the sample from TJS is
better in my opinion.

Just to help you choose.

Cor
 
I tried your suggestion but it does not allow me to convert object to a
string

Rob Windsor said:
You could probably do this using the CodeDOM but it would be non-trivial.
Have you thought about using an array (or a collection) like:

Dim a(4) As objectName
For i As Integer = 0 to 4
a(i) = New objectName(...)
Next

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


TJS said:
how can I dynamically dimension objects in a loop ?

trying to do something like this -but it doesn't compile

for i = 1 to 5
Dim "submenu" & i As New objectName_here
next

Compiler Error Message: BC30203: Identifier expected.
 
how do I set this to an object ?


Sadha Sivam S said:
Hi TJS,

That one is absolutely impossible, but i can suggest you can alternate way
of doing in the same way, that is identifying the objects with some name.
 
I'm sorry, I don't know what you mean by this statement.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


TJS said:
I tried your suggestion but it does not allow me to convert object to a
string

Rob Windsor said:
You could probably do this using the CodeDOM but it would be non-trivial.
Have you thought about using an array (or a collection) like:

Dim a(4) As objectName
For i As Integer = 0 to 4
a(i) = New objectName(...)
Next

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


TJS said:
how can I dynamically dimension objects in a loop ?

trying to do something like this -but it doesn't compile

for i = 1 to 5
Dim "submenu" & i As New objectName_here
next

Compiler Error Message: BC30203: Identifier expected.
 
Back
Top