473,657 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterate (loop) through list of variables

Hello-

I have numerous variables called varNameX (where X is equal to a
number from 0 to a known number "n").

Using VB.NET, I would like to iterate through all of these variables
to run comparisons.

Example:

For x as Integer = 0 to N
If varName(x).Text = "Some Text" Then
boolFlag = True
End If
Next

Is there a way to "dynamicall y" iterate through a collection of
variables? If so, what is the suggested way to do this? (An example
would be greatly appreciated).

Steve

Jul 13 '07 #1
3 16513
On Jul 13, 11:13 am, Steve <leat...@gmail. comwrote:
Hello-

I have numerous variables called varNameX (where X is equal to a
number from 0 to a known number "n").

Using VB.NET, I would like to iterate through all of these variables
to run comparisons.

Example:

For x as Integer = 0 to N
If varName(x).Text = "Some Text" Then
boolFlag = True
End If
Next

Is there a way to "dynamicall y" iterate through a collection of
variables? If so, what is the suggested way to do this? (An example
would be greatly appreciated).

Steve
Look up the for each loop.

i.e.

Dim varNames As String() = {"one", "two", "three", "four", "five",
"etc"}

For Each name As String In varNames
If name = "three" Then
boolFlag = True
End If
Next

Thanks,

Seth Rowe

Jul 13 '07 #2
Hi Steve,

why not using an array?

In case you really need these variables, you should use reflection
for getting/setting their values.

Regards
Bernd
Jul 13 '07 #3
On 13 Lug, 17:33, Bernd Schend <bernd.off...@g mx.dewrote:
Hi Steve,

why not using an array?

In case you really need these variables, you should use reflection
for getting/setting their values.

Regards
Bernd
Bernd is right.

If you feel you need that, double check your program architecture.

Anyway that would be something like (need to add a few checks in real
world programs):

'----------------------------------------

Imports System.Reflecti on

Public Class Form1

Public Var1 As String = "Name1"
Public Var2 As String = "Name2"
Public Var3 As String = "Name3"
Public Var4 As String = "Name4"
Public Var5 As String = "Name5"

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Const Prefix As String = "Var"

For i As Integer = 1 To 5

Dim Flags As BindingFlags = BindingFlags.Ge tField Or
BindingFlags.In stance Or BindingFlags.Pu blic
Dim f As System.Reflecti on.FieldInfo =
Me.GetType.GetF ield(Prefix & i, Flags)
Dim Text As String = f.GetValue(Me)

If Text = "Name3" Then
MsgBox(Text)
End If
Next

End Sub

End Class

'----------------------------------------
Tommaso

Jul 13 '07 #4

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

Similar topics

2
4122
by: Dave | last post by:
Is it possible to extract all the used variables (&type) from a piece of sourcecode, rather than doing it manually? Cheers, Dave
10
8739
by: bst | last post by:
Is there a way to iterate through member variables of different names, of course, one by one from the very first one in a while loop, for example? Thanks!
2
6043
by: Yves Glodt | last post by:
Hello list, I need to iterate over a class and get all her variable names and values, e.g. considering this example: class testclass: var1 = 'ab' var2 = 'cd' var3 = 'ef'
6
9458
by: Oliver | last post by:
Hi there! Does anyone know of a way on how you could access/loop through the variable names. E.g. if you have a struct and want to loop through all variables in that struct to give its value to the console? Thanks in advance, Oliver
12
21076
by: Dave Dean | last post by:
Hi all, I'm looking for a way to iterate through a list, two (or more) items at a time. Basically... myList = I'd like to be able to pull out two items at a time - simple examples would be: Create this output: 1 2
1
1695
by: Steve | last post by:
Hello- I have numerous variables called varNameX (where X is equal to a number from 0 to a known number "n"). Using VB.NET, I would like to iterate through all of these variables to run comparisons. Example:
7
1264
aas4mis
by: aas4mis | last post by:
In this for loop is it possible to reference a different label with each iteration? c# for (x = 1;x <=3; x++) { label & x.tostring & .visible = true; } Sorry for the mess of code. That was the only way I know how to get my point across. I'm used to php and vb, my c# is embarrassing.
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8726
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
8503
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
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
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...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
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.