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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of ddls

Status
Not open for further replies.

kristinac

Programmer
Jul 18, 2003
98
US
I have an array of drop down lists. I also have a procedure that initializes the array (at least I thought) or rather, loads the ddls if I'm saying that right. I also have a for loop that takes the data from a data table and binds it to each of the four drop down lists. When I run the program, the loadDDLs seems to work fine, the array length is right and so forth, but when it gets to the for loop to load the ddls with data, I get an Object Reference Not set to an instance of an object error.

I might also mention when I try and load a single drop down list by name, it works fine. But I was trying to avoid writing the same lines four times.

This is my relative code:

Dim theDDLs() As DropDownList = {ddlBuilding, ddlElectrical, ddlMechanical, ddlPlumbing}
______________________

Private Sub LoadDDLs()
DbProcedures.LoadDDLs(ddlBuilding, theDDLs)
DbProcedures.LoadDDLs(ddlElectrical, theDDLs)
DbProcedures.LoadDDLs(ddlMechanical, theDDLs)
DbProcedures.LoadDDLs(ddlPlumbing, theDDLs)

End Sub
______________________

Friend Sub LoadDDLs(ByVal theddl As DropDownList, ByVal theddls() As DropDownList)
Dim Index As Int32
If theddls Is Nothing Then
ReDim theddls(0)
End If
If theddls(0) Is Nothing Then
Index = 0
Else
Index = theddls.Length
End If
ReDim Preserve theddls(Index)
theddls(Index) = theddl
End Sub
____________________________
....(In Private Sub BindContractors())...

If dt.Rows.Count > 0 Then
Dim rw As DataRow = dt.Rows(0)
Dim x As Integer
For x = 0 To (theDDLs.Length - 1)
lblMessage.Text = theDDLs(x).ID
***theDDLs(x).DataSource = dt
theDDLs(x).DataValueField = "Id"
theDDLs(x).DataTextField = "Contractor"
theDDLs(x).DataBind()
Next
End If
_____________________

The line with the stars is where the error is generated.

Any suggestions would be appreciated...Perhaps I have handling the array incorrectly? Can you have an array of drop down lists and populate them through a for loop as I am attempting?

Thanks! :)
 
If you are saying
Code:
theddls(Index) = theddl

you need to make sure the object is instantiated.

To illustrate it in simple terms, consider this:
Code:
Dim X as MyObject
X = New MyObject()
MyArray(0) = X
 
Okay, I probably don't understand completely. I tried

Friend Sub LoadDDLs(ByVal theddl As DropDownList, ByVal theddls() As DropDownList)
Dim Index As Int32
Dim theddl As New DropDownList
If theddls Is Nothing Then
ReDim theddls(0)...

But it says theddl has already been declared. I tried to reDim too but that didn't work either.

And btw when I did not use the LoadDDLs procedures I still got the same error. I know I haven't instantiated something, but I can't seem to see where or how.
 
Try this:

Code:
Dim DDL As DropDownList
For Each DDL In theDDLS
     DDL = New DropDownList()
Next
 
Thanks...I changed the code to this:

Dim theDDLs() As DropDownList = {ddlBuilding, ddlElectrical, ddlMechanical, ddlPlumbing}

Private Sub LoadDDLs()
Dim DDL As DropDownList
For Each DDL In theDDLs
DDL = New DropDownList
Next
End Sub

....(In Private Sub BindContractors())...

If dt.Rows.Count > 0 Then
Dim rw As DataRow = dt.Rows(0)
Dim x As Integer
For x = 0 To (theDDLs.Length - 1)
lblMessage.Text = theDDLs(x).ID
***theDDLs(x).DataSource = dt
theDDLs(x).DataValueField = "Id"
theDDLs(x).DataTextField = "Contractor"
theDDLs(x).DataBind()
Next
End If


However I am receiving the same error. Sorry, I guess I'm just not catching on. I've bound drop down lists before but I've never tried with an array of them.

Incidentally, how'd ya make the little code window? :)



 
I didn't mean to change your complete LoadDDLs() routine. I meant to add my code to the bottom of it.

To get the code window, type [ code ] followed by [ /code ]. Take out the spaces.
 
OH okay. I misunderstood, sorry. Although I still must be doing something wrong...However, before your last post I managed to get the thing to work by doing:

Code:
Dim theDDLs(3) As DropDownList

Private Sub LoadDDLs()
        theDDLs(0) = ddlBuilding
        theDDLs(1) = ddlElectrical
        theDDLs(2) = ddlMechanical
        theDDLs(3) = ddlPlumbing
End Sub

And that actually worked...I was overcomplicating wasn't I? lol. Thank you for your advice...and the little [ code ] tip. :)

 
It's hard to tell without seeing all your code. You may have initialized them correctly at some point. Try a metaphor. Instead of Drop Down Lists, let's talk about Fire Trucks.

Say you are going to make a fleet of "Elite Fire Trucks" (your array). So you say, "We have our group of Elite Fire Trucks." Now this doesn't do much. You might say "Our Elite Fire Trucks are Truck Numbers 1, 2, and 3." These trucks are trucks you have in your garages already. Or you might say "We are going to buy 3 new fire trucks to put in this Elite Group." Going the second route, you have just cost your department money (memory). But after you have purchased the trucks, you need to go pick them up and put them in your garage. They won't do you much good if you leave them at the truck lot. This is like adding the controls to your Form's control collection.

So by declaring a Drop Down List, you need to make it equal to a Drop Down List. You can make it equal to one that exists, or equal to a brand new Drop Down List.

Read "Declaring Array Variables" and "Using the New Keyword" under the help section in .Net.
 
Thanks very much RiverGuy...I will read those sections. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top