Loop through Non-Array Variables

S

Steve

How can I achieve this in VB?

I'm using VB.Net 2005. I have a framework that auto-generates a lot
of non-arrayed variables with a consistent naming scheme, like:
float1, float2, ... float24

The framework also gives me a "Count" variable. I need to find
someway to loop through the variables in order. I know how to do this
in other languages, but not in VB. I'm looking for something like the
following pseudocode:

FOR i = 1 TO Count
current = get_var_by_name("float" & i.ToStr())
DoWork(current)
NEXT i

Obviously this is what arrays are for, but I have no control over the
framework and I cannot change it to produce arrays.

Any ideas how to work with what I've got?

Thanks,
Steve
 
K

kimiraikkonen

How can I achieve this in VB?

I'm using VB.Net 2005. I have a framework that auto-generates a lot
of non-arrayed variables with a consistent naming scheme, like:
float1, float2, ... float24

The framework also gives me a "Count" variable. I need to find
someway to loop through the variables in order. I know how to do this
in other languages, but not in VB. I'm looking for something like the
following pseudocode:

FOR i = 1 TO Count
current = get_var_by_name("float" & i.ToStr())
DoWork(current)
NEXT i

Obviously this is what arrays are for, but I have no control over the
framework and I cannot change it to produce arrays.

Any ideas how to work with what I've got?

Thanks,
Steve

Steve,
Try specifyiny "Count - 1" after "to".

Dim i As Integer
For i = 0 To Count - 1
' Code to be looped
Next

Note that "Count" (usually property) must get amount of items'
collection. (like listbox control's)

Regards,

Onur Güzel
 
C

Cor Ligthert[MVP]

Steve,

As I understand it, than it is something very strange that you want to
accomplise, however for that is reflection.

Cor
 
H

Herfried K. Wagner [MVP]

Steve said:
I'm using VB.Net 2005. I have a framework that auto-generates a lot
of non-arrayed variables with a consistent naming scheme, like:
float1, float2, ... float24

The framework also gives me a "Count" variable. I need to find
someway to loop through the variables in order. I know how to do this
in other languages, but not in VB. I'm looking for something like the
following pseudocode:

FOR i = 1 TO Count
current = get_var_by_name("float" & i.ToStr())
DoWork(current)
NEXT i

There is no way to do that for local variables. For type-level variables
you can use '<type>.GetType().GetFields(...)' to access the variables.
 
K

kimiraikkonen

There is no way to do that for local variables. For type-level variables
you can use '<type>.GetType().GetFields(...)' to access the variables.

Yes, it's good to be aware of this. I just supposed the OP was trying
to perform a normal loop through a integer-based collection.
 
A

Alan Gillott

another approach is to dynamically create a VBScript sub and use the
Microsoft Script control Activex driver that lets you pass a static class to
it. the script can then be one line "myclass.Value = float" & count . You
can then use myclass.value in your .Net program.

It'll look something like this:

Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.AddObject "MyClass", myclass 'Or whatever
try
sc.AddCode "myclass.Value = float" & count
DostuffTo(Myclass.value,.....)
catch...
 

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

Similar Threads


Top