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

passing objects...

Status
Not open for further replies.

gagz

Programmer
Nov 21, 2002
333
US
objParam = objCmd.Parameters.Add("@DataProjector", System.Data.SqlDbType.bit)
objParam.Direction = ParameterDirection.Input
objParam.Value = ""
If (chkAVList.items.FindByValue("Data Projector").Selected = True) Then
objParam.Value = chkAVList.items.FindByValue("Data Projector").Value
End If
 
crap, that was a mispost... here's what it should have said.

I have two checkboxlists on my form. they have the same options, but different names. Instead of duplicating the code to set up the parameters for my stored procedure, i'd like to pass the checkboxlist to a subroutine. can this be done?

so far i tried this and it hasn't worked.

sub page_load
insertSub(chkbox1)
insertSub(chkbox2)
end sub

sub insertSub(chkAVList as object)
'stuff
objParam = objCmd.Parameters.Add("@DataProjector", System.Data.SqlDbType.bit)
objParam.Direction = ParameterDirection.Input
objParam.Value = ""
If (chkAVList.items.FindByValue("Data Projector").Selected = True) Then
objParam.Value = chkAVList.items.FindByValue("Data Projector").Value
End If
'more stuff
end sub

any help would be great.

THANKS
 
I think it might if you pass the check box in byref rather than the default byval That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Sure, but use

sub insertSub(ByVal chkAVList as System.Web.Ui.WebControls.Checkbox)

ByVal works with objects. You only need ByRef if you need to pass back a different reference than the one you were passed.



Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
i think its close... but i'm getting:
System.NullReferenceException: Object reference not set to an instance of an object.


the line it complains about is:
If (chkAVList.items.FindByValue("Slide Projector").Selected = True) Then


(which is the first time i reference the object.)
 
Which object is missing: the checkboxList object, or the object returned by Items("Slide Projector")?

The Docs say:
"Use the FindByValue method to search the collection for a ListItem with a Value property that contains value specified by the value parameter. If an item is not found in the collection using this criteria, a null reference (Nothing in Visual Basic) is returned."

It is possible that the Checkboxlist exists but that the ListItem to be found by Items("Slide Projector") does not.

Instead of
If (chkAVList.items.FindByValue("Slide Projector").Selected = True) Then

Try.
Dim licW = ListItem
Dim blnSelected a boolean
licW = chkAVList.items("Slide Projector") ' Get ListItem or NOTHING
if licW is Nothing then
.....missing the Slide Projector
else
blnSelectecd = licW.Selected
End if
if blnSelected then
.... found
End If

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
well, i think its passing the object that is the issues... because I know this should work:

If chkAVList.Items(0).Selected Then


and it doesn't... I was doing something overly complicated with the findbyvalue stuff, since its a static list and I know the correlation between index number and text value...

any other ideas? [dazed]
 
Passing objects is done all the time. It is simple.

First, what do you mean by "does not work"? Is Selected = false when you expect it to be true or is there an "object required" exception?

Use strong typing when you can do so, not As Object.
Are those checkboxes or CheckBoxLists???

Are you saying that
sub page_load
if chkbox.Items(0).selected = true ' THIS IS OK
end if
insertSub(chkbox1) '
insertSub(chkbox2)
end sub
sub insertSub(byVal chkAVList as checkBoxlist)
If chkAVList.Items(0).Selected = True ' BUT THIS IS NOT
End if
End Sub Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top