Strange behavior of DropDownList after function call

O

oerz

Hey y'all,

I'm stuck. I know it will turn out it's not the system's fault, but at
the moment I do not have a clue.
What I am doing is very simple. I have a dropdownlist which is
populated by a function. That works fine. But if I try to access that
same dropdownlist AFTER the function was called, I am getting an
"Object reference not set to an instance of an object"-error.
Please look at the following code:

testDdl.aspx
============
<%@ Page Language="VB" Inherits="TestDdl" Src="testDdl.aspx.vb"
Debug="true" %>
<html>
<body>
<form id="testform" runat="server">
<asp:dropdownlist class="text_form" id="ddlTest"
runat="server"/>
<asp:Label class="text_label" id="lblInfo"
runat="server"/>
</form>
</body>
</html>


testDdl.aspx.vb
===============
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections


Public Class TestDdl
Inherits Page
public lblInfo as label
public ddlTest as dropDownList

sub Page_Load
'*** this works fine ***
lblInfo.Text = ddlTest.UniqueID

'*** this works, too ***
ddlTest = fillDdlTest(ddlTest)

'*** same as above, produces error ***
lblInfo.Text &= ddlTest.UniqueID

'*** also this produces an error ***
setTest()

end sub

public sub setTest()
'*** same line as the last of fillDdlTest, but won't work ***
ddlTest.Items.FindByText("Test 1").Selected=true
end sub

public function fillDdlTest(ByRef dropDown as dropdownlist)
dim arrTest As New ArrayList
arrTest.Add(" ")
arrTest.Add("Test 1")
arrTest.Add("Test 2")
arrTest.Add("Test 3")
arrTest.TrimToSize()
dropDown.DataSource=arrTest
dropDown.DataBind()
dropDown.Items.FindByText("Test 2").Selected=true
end function
End Class

Of course would I find a work-around, but this is just a simplified
example of a more complex application.

Does anyone have the answer for me?
 
T

Teemu Keiski

Hi,

you pass the DDL instance (ddlTest) as a reference to the function (wouldn't
be needed as it is reference type by default) and then get it back as return
value of the function.However, the function doesn't actually return anything
so after the function call ddlTest is a null reference which is why you get
the error as you try to access the DDL via this reference

So change your code as follows.
=======================
public function fillDdlTest(ByRef dropDown as dropdownlist)

TO
==
public function fillDdlTest(ByVal dropDown as dropdownlist)

And call to the fillddlTest
=====================
ddlTest = fillDdlTest(ddlTest)

TO
==

fillDdlTest(ddlTest)
 
T

Teemu Keiski

And to add,

fillDdlTest method could be just a sub in this case. There is no need for it
to return anything.
 
O

oerz

Teemu Keiski said:
Hi,

you pass the DDL instance (ddlTest) as a reference to the function (wouldn't
be needed as it is reference type by default) and then get it back as return
value of the function.However, the function doesn't actually return anything
so after the function call ddlTest is a null reference which is why you get
the error as you try to access the DDL via this reference

Thank you so much!!
I was totally unaware of the consequences of having a function not
really returning anything! As far as I was concerned, it worked...
So it can be a 'Sub' just as well. I'll test the 'ByVal' version's
behaviour if the function (or sub) is in a compiled superclass.

GREAT! Thanks again!

Oerz
 
T

Teemu Keiski

Hi,

because DropDownList is a reference type, a reference (to the ddl) is passed
to the method (function or sub, doesn't matter) and via that reference you
can access the DDL just as you'd access it normally when it is a local
member/variable in a method even if you use ByVal keyword. Therefore the
method could be sub and you could access the DDL. And again you wouldn't
need to roundtrip it via a function (pass a reference to function and return
the referenmce as function's return value)

Real difference with ByVal and ByRef used with reference types is that when
you have ByRef you can reassign the reference (in this case like having a
output parameter in the method), otherwise there is no real practical
difference (what happens under the covers is more detailed).

ByVal and reference type gives a reference to the reference type and if you
use ByRef and reference type, a copy of this reference is given. And with
reference types the reference and copy of the reference both point to the
same underlying object (the ddl), it has no difference in practise. Of
course, because ByRef allows you to reassign the reference you can think it
can be a bit inefficient when compared to ByVal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top