Strange behavior of DropDownList after function call

Discussion in 'ASP .Net Web Controls' started by oerz, Jul 5, 2004.

  1. oerz

    oerz Guest

    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?
    oerz, Jul 5, 2004
    #1
    1. Advertising

  2. oerz

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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?
    Teemu Keiski, Jul 5, 2004
    #2
    1. Advertising

  3. oerz

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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?
    Teemu Keiski, Jul 5, 2004
    #3
  4. oerz

    oerz Guest

    "Teemu Keiski" <> wrote in message news:<>...
    > 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
    oerz, Jul 6, 2004
    #4
  5. oerz

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > "Teemu Keiski" <> wrote in message

    news:<>...
    > > 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
    Teemu Keiski, Jul 6, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PJ

    DropDownList Strange Behavior

    PJ, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    348
  2. Kris Vanherck

    Strange compiler behavior after CS0006 error

    Kris Vanherck, Apr 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    444
    Ignacio Machin \( .NET/ C# MVP \)
    Apr 21, 2004
  3. jjblanco
    Replies:
    0
    Views:
    426
    jjblanco
    Sep 23, 2006
  4. Efy
    Replies:
    4
    Views:
    276
  5. Replies:
    9
    Views:
    351
    shish
    May 2, 2007
Loading...

Share This Page