problem with getter and setter not working

Discussion in 'ASP .Net' started by Adam Sandler, May 25, 2006.

  1. Adam Sandler

    Adam Sandler Guest

    Hi all,

    I hope this is an easy one... Using VWD 2005. When I call my accessor
    method (getName) I always receive an empty string back. Debugging
    shows there should be something there but I cannot figure out where the
    problem is actually occurring.

    The first couple of lines of my code behind:

    Partial Class _Default
    Inherits System.Web.UI.Page

    Dim sName As String ' for getter and setter

    Here's my getter and setter:

    Function getName()
    Return sName
    End Function

    Sub setName(ByVal s As String)
    sName = s
    End Sub

    Here's the code which calls the setter:

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button2.Click
    Try
    setName(Me.GridView1.SelectedRow.Cells(2).Text)
    Catch nre As NullReferenceException
    MsgBox("Please select a row.")
    End Try
    End Sub

    Lastly, here's some code which calls the getter:

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button4.Click
    MsgBox(getName())
    End Sub


    When Button4 is clicked, the message box pops up but there's nothing in
    it... where am I going wrong here?

    Thanks!
     
    Adam Sandler, May 25, 2006
    #1
    1. Advertising

  2. Adam Sandler

    Jimi200478 Guest

    the reason that you are getting this is because once you have clicked
    button4, the value of sName is no longer there because a post back has
    occured. what you want to do is something like this:

    private string SetName
    {
    set{ViewState["sName"] = value;}
    }

    private string GetName
    {
    get{return ViewState["sName"].ToString();}
    }

    Notice also that I am using the "getter" and "setter" as they are
    intended to be used. they should be properties, not methods.
     
    Jimi200478, May 25, 2006
    #2
    1. Advertising

  3. Adam Sandler

    sloan Guest

    C# and VB.net... don't really go with the java setters/getters

    In VB, you do a

    Public Property LastName
    Set
    Me.m_lastName = Value
    End Set
    Get
    return Me.m_lastName
    End Get
    End Property

    where m_lastName is a member variable.

    Yeah, stuff "disappears" on the PostBack, you have to be aware of objects
    called in Page_Load, aren't around on a button click.

    I have a "smart" object holder at my blog:

    http://spaces.msn.com/sholliday/ 10/24/2005

    that should help if you want to go fancy.
    Or learn how to perist items in the Session["mykeyname"] format.



    "Adam Sandler" <> wrote in message
    news:...
    > Hi all,
    >
    > I hope this is an easy one... Using VWD 2005. When I call my accessor
    > method (getName) I always receive an empty string back. Debugging
    > shows there should be something there but I cannot figure out where the
    > problem is actually occurring.
    >
    > The first couple of lines of my code behind:
    >
    > Partial Class _Default
    > Inherits System.Web.UI.Page
    >
    > Dim sName As String ' for getter and setter
    >
    > Here's my getter and setter:
    >
    > Function getName()
    > Return sName
    > End Function
    >
    > Sub setName(ByVal s As String)
    > sName = s
    > End Sub
    >
    > Here's the code which calls the setter:
    >
    > Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Button2.Click
    > Try
    > setName(Me.GridView1.SelectedRow.Cells(2).Text)
    > Catch nre As NullReferenceException
    > MsgBox("Please select a row.")
    > End Try
    > End Sub
    >
    > Lastly, here's some code which calls the getter:
    >
    > Protected Sub Button4_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Button4.Click
    > MsgBox(getName())
    > End Sub
    >
    >
    > When Button4 is clicked, the message box pops up but there's nothing in
    > it... where am I going wrong here?
    >
    > Thanks!
    >
     
    sloan, May 25, 2006
    #3
  4. Adam Sandler

    Jimi200478 Guest

    i would just use viewstate. that way you dont have to create variables
    that you dont need, and you're not carrying around unescessary data in
    session. if you're using this value on only one page...use viewstate,
    if you need it on other pages, then you can use session. code i posted
    above is in c#. translation is fairly easy.
     
    Jimi200478, May 25, 2006
    #4
  5. Adam Sandler

    Adam Sandler Guest

    Thanks all... As sloan guessed, I'm a Java guy that's been thrown in to
    the deep end of the ASP.NET pool without knowing how to swim.

    But I don't exactly find the translation easy... probably because it's
    a bit of a paradigm shift and Googling the terms: property, viewstate,
    and vb isn't yeilding results I think I can use.

    http://www.codeguru.com/csharp/.net/net_asp/controls/article.php/c11971/
    is the same thing as above and in C#

    Either I've translated from C# incorrectly or the the post back still
    over writes what was sent to the setter.

    If I got this working and I actually want to work with the data, do I
    do that by typing this LastName.Get (sloan's example) or GetName
    (Jimi200478's example)?
     
    Adam Sandler, May 25, 2006
    #5
  6. Adam Sandler

    sloan Guest

    Are you trying to Display the value in the Page (somewhere in the html
    output)?

    Or are you trying to use it in the code_behind page (where the actualy
    vb.net code resides)

    ?




    "Adam Sandler" <> wrote in message
    news:...
    > Thanks all... As sloan guessed, I'm a Java guy that's been thrown in to
    > the deep end of the ASP.NET pool without knowing how to swim.
    >
    > But I don't exactly find the translation easy... probably because it's
    > a bit of a paradigm shift and Googling the terms: property, viewstate,
    > and vb isn't yeilding results I think I can use.
    >
    > http://www.codeguru.com/csharp/.net/net_asp/controls/article.php/c11971/
    > is the same thing as above and in C#
    >
    > Either I've translated from C# incorrectly or the the post back still
    > over writes what was sent to the setter.
    >
    > If I got this working and I actually want to work with the data, do I
    > do that by typing this LastName.Get (sloan's example) or GetName
    > (Jimi200478's example)?
    >
     
    sloan, May 25, 2006
    #6
  7. Adam Sandler

    Adam Sandler Guest

    sloan wrote:
    > Are you trying to Display the value in the Page (somewhere in the html
    > output)?
    >
    > Or are you trying to use it in the code_behind page (where the actualy
    > vb.net code resides)


    In the code behind... In the past 15 minutes I tried this in my class
    declaration:

    Private _myname As String

    Public Property MyName() As String
    Get
    Return _myname
    End Get
    Set(ByVal value As String)
    _myname = value
    End Set
    End Property

    And then in my event handlers:

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button2.Click
    MyName = Me.GridView1.SelectedRow.Cells(2).Text
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button4.Click
    MsgBox(MyName)
    End Sub

    And I get the same result... an empty string returned.

    Thanks for your help thus far!!!
     
    Adam Sandler, May 25, 2006
    #7
  8. Adam Sandler

    Jimi200478 Guest

    Adam Sandler:

    You are going to have to store "MyName" in either ViewState or Session
    if you want it to survive a PostBack. Both mine and Sloan's examples
    are the same, just different syntax. Sloans example demonstrates how to
    use a property in .NET, but it doesnt answer base question which is
    "how do i make the value survive a postback". The answer to that
    question is viewstate or session. if you do want to have a variable on
    your page called myname, you will have to do something like this:

    in C#:

    private string _MyName;

    public string MyName
    {
    get
    {
    _MyName = ViewState["MyName"].ToString();
    return _MyName;
    }
    set
    {
    _MyName = value;
    ViewState["MyName"] = _MyName;
    }
    }

    if the property is followed by an equals sign, then it knows to "set"
    the value. if it is called without an equals sign, the property knows
    to "get" the value. see below:

    Setting MyName :

    MyName = "John"

    Getting MyName :

    MsgBox(MyName)

    Truthfully, at the end of it all, it doesnt matter how you do it, all
    that matters is that you use viewstate or session to keep your value
    alive. well, good luck!
     
    Jimi200478, May 26, 2006
    #8
  9. Adam Sandler

    sloan Guest

    Yeah... I wrote an example up before seeing Jimi's example.

    But here is mine... Same bat time, same bat channel.



    ASPX
    <HTML>
    <HEAD>
    <title>ViewStateTest</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <P><asp:textbox id="TextBox1" runat="server"></asp:textbox>&nbsp;(Put a
    Number In
    Here, and click the Button Below)</P>
    <P><asp:button id="Button1" runat="server"
    Text="ButtonONE"></asp:button></P>
    <P><asp:button id="Button2" runat="server" Text="ButtonTWO"
    Visible="False"></asp:button></P>

    </form>
    </body>
    </HTML>


    ..cs code behind:


    public int SelectedRecordID

    {

    get

    {

    if (ViewState["SelectedRecordID"] != null)

    return Convert.ToInt32(ViewState["SelectedRecordID"]);

    else

    return -1;

    }

    set

    {

    ViewState["SelectedRecordID"] = value;

    }

    }

    private void Button1_Click(object sender, System.EventArgs e)

    {


    int whatsMyValue = this.SelectedRecordID;



    this.SelectedRecordID = int.Parse(this.TextBox1.Text);



    this.Button1.Visible = false;

    this.Button2.Visible = true;



    }

    private void Button2_Click(object sender, System.EventArgs e)

    {

    int whatsMyValue = this.SelectedRecordID;



    this.SelectedRecordID = int.Parse(this.TextBox1.Text);



    }



    private void Page_Load(object sender, System.EventArgs e)

    {

    // Put user code to initialize the page here

    if (!Page.IsPostBack)

    {

    this.SelectedRecordID = -999; // use -999 for the PageLoad

    }



    }


    "Jimi200478" <> wrote in message
    news:...
    > Adam Sandler:
    >
    > You are going to have to store "MyName" in either ViewState or Session
    > if you want it to survive a PostBack. Both mine and Sloan's examples
    > are the same, just different syntax. Sloans example demonstrates how to
    > use a property in .NET, but it doesnt answer base question which is
    > "how do i make the value survive a postback". The answer to that
    > question is viewstate or session. if you do want to have a variable on
    > your page called myname, you will have to do something like this:
    >
    > in C#:
    >
    > private string _MyName;
    >
    > public string MyName
    > {
    > get
    > {
    > _MyName = ViewState["MyName"].ToString();
    > return _MyName;
    > }
    > set
    > {
    > _MyName = value;
    > ViewState["MyName"] = _MyName;
    > }
    > }
    >
    > if the property is followed by an equals sign, then it knows to "set"
    > the value. if it is called without an equals sign, the property knows
    > to "get" the value. see below:
    >
    > Setting MyName :
    >
    > MyName = "John"
    >
    > Getting MyName :
    >
    > MsgBox(MyName)
    >
    > Truthfully, at the end of it all, it doesnt matter how you do it, all
    > that matters is that you use viewstate or session to keep your value
    > alive. well, good luck!
    >
     
    sloan, May 26, 2006
    #9
  10. Adam Sandler

    Adam Sandler Guest

    Jimi200478 wrote:
    > Adam Sandler:
    >
    > You are going to have to store "MyName" in either ViewState or Session
    > if you want it to survive a PostBack. Both mine and Sloan's examples
    > are the same, just different syntax. Sloans example demonstrates how to
    > use a property in .NET, but it doesnt answer base question which is
    > "how do i make the value survive a postback". The answer to that
    > question is viewstate or session. if you do want to have a variable on
    > your page called myname, you will have to do something like this:
    >
    > in C#:
    >
    > private string _MyName;
    >
    > public string MyName
    > {
    > get
    > {
    > _MyName = ViewState["MyName"].ToString();
    > return _MyName;
    > }
    > set
    > {
    > _MyName = value;
    > ViewState["MyName"] = _MyName;
    > }
    > }
    >
    > if the property is followed by an equals sign, then it knows to "set"
    > the value. if it is called without an equals sign, the property knows
    > to "get" the value. see below:
    >
    > Setting MyName :
    >
    > MyName = "John"
    >
    > Getting MyName :
    >
    > MsgBox(MyName)
    >
    > Truthfully, at the end of it all, it doesnt matter how you do it, all
    > that matters is that you use viewstate or session to keep your value
    > alive. well, good luck!



    Jimi... thatnks for the help. Here's what I have so far...

    Private _MyName As String

    Public Property MyName() As String
    Get
    _MyName = Me.ViewState(MyName).ToString
    End Get
    Set(ByVal value As String)
    _MyName = value
    Me.ViewState(MyName) = _MyName
    End Set
    End Property

    The compiler doesn't like this code above but it will run. However,
    the first time I click the button which has this in its handler:

    MyName = someComponent.Text

    It throws an error with "The string parameter 'key' cannot be null or
    empty." as the message. This is why I assumed earlier that my
    translation from C# to VB was incorrect. I'm positing it here now so
    you can indeed see that I am very interested in what you have posted
    but for whatever reason, I haven't been able to successfully implement
    your advice.

    Thanks!
     
    Adam Sandler, May 26, 2006
    #10
  11. Adam Sandler

    Jimi200478 Guest

    you're almost there. you just need to change the code a little bit:

    change the code in the get to:
    *notice that the viewstate key is a string

    _MyName = Me.ViewState("MyName").ToString
    Return _MyName

    change the code in the set to:
    *notice that the viewstate key is a string

    _MyName = value
    Me.ViewState("MyName") = _MyName
     
    Jimi200478, May 26, 2006
    #11
  12. Adam Sandler

    Adam Sandler Guest

    That was it... Thanks gents for the help!!!!
     
    Adam Sandler, May 26, 2006
    #12
  13. Adam Sandler

    Jimi200478 Guest

    you're very welcome. Happy Programming.
     
    Jimi200478, May 26, 2006
    #13
    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. dasacc22

    getter and setter and list appends

    dasacc22, Apr 20, 2009, in forum: Python
    Replies:
    6
    Views:
    955
    Piet van Oostrum
    Apr 25, 2009
  2. Steve
    Replies:
    1
    Views:
    263
  3. Brian Bruggeman

    @property simultaneous setter and getter

    Brian Bruggeman, Dec 20, 2013, in forum: Python
    Replies:
    4
    Views:
    87
    Terry Reedy
    Dec 21, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    260
    Peter Otten
    Dec 21, 2013
  5. Devin Jeanpierre

    Re: @property simultaneous setter and getter

    Devin Jeanpierre, Dec 21, 2013, in forum: Python
    Replies:
    0
    Views:
    274
    Devin Jeanpierre
    Dec 21, 2013
Loading...

Share This Page