1

I got stucked on making looping for adding these Variables to ComboBox, i want to call it with a simpler way with Looping for, but i failed so many times, i've been googling around but i still failed, so any help will be appreciated

Public MyPass1 As String = "John"
Public MyPass2 As String = "Andrew"
Public MyPass3 As String = "Stewart"
Public MyPass4 As String = "Meiny"
Public MyPass5 As String = "Franco"
Public MyPass6 As String = "Hanks"
Public MyPass7 As String = "Buzz"
Public MyPass8 As String = "Timmy"
Public MyPass9 As String = "George"
Public MyPass10 As String = "Sanders"

Sub Putitem(ByVal MyPass)
    With cmbAsk
        For i As Integer = 0 To 9
            Dim c As Integer
            c = i + 1
            Items.Add(MyPass(c)) 'The main problem is here, i want to do looping for calling it.
            i = c
        Next
    End With
End Sub

Any help would be appreciated. Thanks in advance.

2 Answers 2

1

Instead of storing the values in individual variables, you can store them in an array:

Public MyPasses As String() = New String() { 
                             "John",
                             "Andrew",  
                             "Stewart",
                             "Meiny",
                             "Franco",
                             "Hanks",
                             "Buzz",
                             "Timmy",
                             "George",                               
                             "Sanders"
                          }

You can then access via:

Items.Add(MyPasses(c))
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you're right, That's the best choice by using array, but eerrr i'm just curious about calling multiple variables with looping for. so, any clue will be appreciated.
@laruffii The only way to accomplish this with separate variables would be to use Reflection, which is slow and error prone. See: msdn.microsoft.com/en-us/magazine/cc163750.aspx
1

You need a collection to add not public strings.

Private collection() As String = {"John", "Mark", "Frank"} 'initializer

cmbAsk.Items.AddRange(collection.ToArray)

AddRange method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.