Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Strange behavior of DropDownList after function call

Reply
Thread Tools

Strange behavior of DropDownList after function call

 
 
oerz
Guest
Posts: n/a
 
      07-05-2004
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?
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-05-2004
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)


--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke










"oerz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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?



 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-05-2004
And to add,

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

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke



"oerz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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?



 
Reply With Quote
 
oerz
Guest
Posts: n/a
 
      07-06-2004
"Teemu Keiski" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      07-06-2004
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.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke


"oerz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Teemu Keiski" <(E-Mail Removed)> wrote in message

news:<(E-Mail Removed)>...
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function call after switch but before first case - behavior? yndygo@gmail.com C++ 9 05-02-2007 10:44 PM
Strange behavior with os call in cgi script sophie_newbie Python 10 02-08-2006 10:39 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
some strange behavior after SP2 =?Utf-8?B?cmFkbWFu?= Microsoft Certification 2 10-07-2004 11:13 PM
DropDownList Strange Behavior PJ ASP .Net 0 07-07-2003 11:26 PM



Advertisments