Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass dropdownlist as parameter 1

Status
Not open for further replies.

may1hem

Programmer
Jul 28, 2002
262
0
0
GB
I want to create a sub which accepts a dropdownlist and a comma separated string as a parameter and populates the dropdownlist. The code does do the job when I enter the name of the dropdownlist directly, but it doesn't work when I create a separate sub for general use. I think it's because it's not passing the dropdownlist correctly. How can I do this?

Code:
Private Sub fnPopulateDropdownlist(ByVal pdDropdownlist As DropDownList, ByVal psSeparatedItems As String, ByVal psSeparator As String)
        Dim str As String = psSeparator
        'Declare a arraylist for getting comma separated string
        Dim arr As ArrayList = New ArrayList
        'check wether the re is comma in the end of the string
        If str.Trim.EndsWith(psSeparator) Then
            str = str.Substring(0, (str.Length - 1))
        End If
        'split the comma separated string into arraylist
        arr.AddRange(str.Split(psSeparator))
        'loop through the arraylist items & add the item to Dropdownlist
        Dim i As Integer = 0
        Do While (i < arr.Count)
            pdDropdownlist.Items.Insert(i, New ListItem(arr(i).ToString, (i + 1).ToString))
            i = (i + 1)
        Loop
    End Sub
 

Passing a control ByVal creates a local copy of a DropDownList and populates it, not the original. Since you're trying to populate an existing DropDownList, you'll need to pass it by reference:

Private Sub fnPopulateDropdownlist([red]ByRef[/red] pdDropdownlist As DropDownList, ByVal psSeparatedItems As String, ByVal psSeparator As String)



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Thanks for your reply Mark.

Actually I found the real problem was that I'd replaced the wrong parameter name in the first string definition.

Anyway I learned something from your reply!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top