ASP.net VB - How to use a for loop and apend the loop count to the end of variable name

Hi all,
I am trying to use a for loop in my code.  I have a several variables defined.  They are all named UserNum1, UserNum2 etc ...... I want to use the variable in a loop .  I can't figure out how to pull it off.  any help would be great.  please see code attached
NCSA SCADAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NCSA SCADAAuthor Commented:
forgot to attach code :)

this is not the exact code but is the part I cant figure out...
Thanks


dim UserNum1 as string
dim UserNum2 as string
for i = 1 to 5
  UserNum(i) = "somthing"
next

Open in new window

0
NCSA SCADAAuthor Commented:
I thougt I would add a bit more to the code


dim UserNum1 as string = "Dave"
dim UserNum2 as string = "Paul"
for i = 1 to 5
  If UserNum(i) = "paul" then
     do somthing
  elseif  
     do somthing
  end if
next

Open in new window

0
NCSA SCADAAuthor Commented:
one last try :)
dim UserNum1 as string = "Dave"
dim UserNum2 as string = "Paul"
for i = 1 to 5
  If UserNum(i) = "paul" then
     do somthing
  elseif  UserNum(i) = "Dave"
     do somthing
  end if
next

Open in new window

0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Tom BeckCommented:
Private Sub LoopUserNumbers()
        Dim namesStr As String = "Paul,Dave"
        Dim names() As String = namesStr.Split(",")
        For i As Integer = 0 To names.Length - 1
            Select Case names(i)
                Case "Paul"
                    'DO SOMETHING
                Case "Dave"
                    'DO SOMETHING
                Case Else
                    'DO SOMETHING
            End Select
        Next
    End Sub
0
NCSA SCADAAuthor Commented:
How would I make that work if the list is created by users selecting a checkbox.
so my string will change depending on the boxes they pick
0
NCSA SCADAAuthor Commented:
A little more detail..
I have five check boxes that represent a user role.  based on the selection use a variable (from above) to change a sql query to reflect the options selected.  I don't want to repeat the code so Im using a loop. Each time it checks if box1 is checked next loop box two.......
 

Here is the sql statement I am trying to fill
Notice the UserRole(i)
(_SqlConnection, "select UserId from tuser_trole where userid = '" & Usernum & "' and RoleId = '" & UserRole1(i) & "'")

'cecking if checked and giving a value      
        If chk1.Checked Then
            UserRole1 = "3"
        ElseIf chk2.Checked Then
            UserRole2 = "4"
        ElseIf chk3.Checked Then
            UserRole3 = "12"
        ElseIf chk4.Checked Then
            UserRole4 = "7"
        ElseIf chk5.Checked Then
            UserRole5 = "5"
        End If

;then the loop

Open in new window

0
Tom BeckCommented:
How is the UserNum being determined? By another series of checkboxes, by a login?
0
NCSA SCADAAuthor Commented:
I run a query earier in my code - that part stays the same and works fine - it represents a user number -

as roles are added they are always added to the same user number (UserNum)
0
Ron MalmsteadInformation Services ManagerCommented:
How about this ?

How about this ?

Dim UserRoles as string = ""

        If chk1.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'3'"
        ElseIf chk2.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'4'"
        ElseIf chk3.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'12'"
        ElseIf chk4.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'7'"
        ElseIf chk5.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'5'"
        End If

'Example UserRole =    '12','7','5'
Dim sqlUserSelect as string = "select UserId from tuser_trole where userid = '" & Usernum & "' and RoleId IN(" & UserRole & ")"

Open in new window

0
Ron MalmsteadInformation Services ManagerCommented:
...I just caught that only one box can be checked in the above code.  Should still work though.
0
NCSA SCADAAuthor Commented:
It might be one, it could be all five but each represents a seperate entry in a sql table -
If user num = 200  and he had role 3 and 5 table would look like this

UserID     UserRole
  200           3
  200           5

would not look like
200    3  5
(i might be confusing myself) :0
0
Ron MalmsteadInformation Services ManagerCommented:
...ahh..so per your last statement, more than one box can be checked.


Dim UserRoles as string = ""

        If chk1.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'3'"
end If
        if chk2.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'4'"
end If

       If chk3.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'12'"
end If
       
       If chk4.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'7'"
end If

        If chk5.Checked Then
            UserRole = UserRole & iif(UserRole = "","",",") & "'5'"
        End If

'Example UserRole =    '12','7','5'
Dim sqlUserSelect as string = "select UserId from tuser_trole where userid = '" & Usernum & "' and RoleId IN(" & UserRole & ") ORDER BY UserRole Desc"


That would come out like this...
UserID     UserRole
  200           12
  200           7
  200           5


0
NCSA SCADAAuthor Commented:
forgive my newness .... I want to make sure i understand the code not just get it to work...
can you break this portion down a bit
iif(UserRole = "","",",") & "'3'"

iif ?

the = "" etc

thanks again - this looks perfect
0
Ron MalmsteadInformation Services ManagerCommented:
The   IIF()   ..Is basically just testing the "UserRole" variable..to see if it already has a value.  If it does...then it inserts a ","  COMMA  ..before appending the next value.

IIF() is a very useful function.

IIF(User role has no value , then no comma , else comma  before next value is appended to this string) & nextvalue
0
Ron MalmsteadInformation Services ManagerCommented:
I spotted a mistake.


...change

Dim UserRoles as string = ""

To...

Dim UserRole as string = ""


0
NCSA SCADAAuthor Commented:
somthing strange is happening .. I don't get an error now. thats great.
I use the first query to check and see if the record exists

 If RecordExists(_SqlConnection, "select UserId from tuser_trole where userid = '" & Usernum & "' and RoleId IN(" & UserRole & ")") Then
                    ' record found in DB, lets do record found task
                    MsgBox("Record exists")
                Else

It comes back as though every record exists
curently only
3

5
do
any ideas
0
Ron MalmsteadInformation Services ManagerCommented:
Please post the contents of this function  >>  RecordExists()

0
Ron MalmsteadInformation Services ManagerCommented:
"""curently only
3

5
do
"""""


3 and 5 ...what ?.. .userRole or userId ?
I may be a bit confused as to your table design and logic you are using to validate.

More details on your project would help.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NCSA SCADAAuthor Commented:
Thanks for your help. You pushed me in the right direction. thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.