473,597 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with getter and setter not working

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.P age

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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button2.Click
Try
setName(Me.Grid View1.SelectedR ow.Cells(2).Tex t)
Catch nre As NullReferenceEx ception
MsgBox("Please select a row.")
End Try
End Sub

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

Protected Sub Button4_Click(B yVal sender As Object, ByVal e As
System.EventArg s) 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!

May 25 '06 #1
12 3723
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.

May 25 '06 #2
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" <co****@excite. com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
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.P age

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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button2.Click
Try
setName(Me.Grid View1.SelectedR ow.Cells(2).Tex t)
Catch nre As NullReferenceEx ception
MsgBox("Please select a row.")
End Try
End Sub

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

Protected Sub Button4_Click(B yVal sender As Object, ByVal e As
System.EventArg s) 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!

May 25 '06 #3
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.

May 25 '06 #4
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/...le.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)?

May 25 '06 #5

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" <co****@excite. com> wrote in message
news:11******** *************@g 10g2000cwb.goog legroups.com...
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/...le.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)?

May 25 '06 #6
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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button2.Click
MyName = Me.GridView1.Se lectedRow.Cells (2).Text
End Sub

Protected Sub Button4_Click(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button4.Click
MsgBox(MyName)
End Sub

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

Thanks for your help thus far!!!

May 25 '06 #7
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!

May 26 '06 #8

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(My Name).ToString
End Get
Set(ByVal value As String)
_MyName = value
Me.ViewState(My Name) = _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.T ext

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!

May 26 '06 #9

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>ViewStat eTest</title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="C#" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P><asp:textb ox id="TextBox1" runat="server"> </asp:textbox>&nb sp;(Put a
Number In
Here, and click the Button Below)</P>
<P><asp:butto n id="Button1" runat="server"
Text="ButtonONE "></asp:button></P>
<P><asp:butto n id="Button2" runat="server" Text="ButtonTWO "
Visible="False" ></asp:button></P>

</form>
</body>
</HTML>
..cs code behind:
public int SelectedRecordI D

{

get

{

if (ViewState["SelectedRecord ID"] != null)

return Convert.ToInt32 (ViewState["SelectedRecord ID"]);

else

return -1;

}

set

{

ViewState["SelectedRecord ID"] = value;

}

}

private void Button1_Click(o bject sender, System.EventArg s e)

{
int whatsMyValue = this.SelectedRe cordID;

this.SelectedRe cordID = int.Parse(this. TextBox1.Text);

this.Button1.Vi sible = false;

this.Button2.Vi sible = true;

}

private void Button2_Click(o bject sender, System.EventArg s e)

{

int whatsMyValue = this.SelectedRe cordID;

this.SelectedRe cordID = int.Parse(this. TextBox1.Text);

}

private void Page_Load(objec t sender, System.EventArg s e)

{

// Put user code to initialize the page here

if (!Page.IsPostBa ck)

{

this.SelectedRe cordID = -999; // use -999 for the PageLoad

}

}
"Jimi200478 " <JM*********@gm ail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
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!

May 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2148
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions to store customers data within an object. It works. Furthermore, it includes a synchronize method, which calls the individual setXXX function and takes the data from the $_POST or $_GET var.
3
18081
by: Kiwi | last post by:
Hello. I know a getter can return other thing than a field. I know a setter can do more things than setting a field. I know there are "setter only" cases and "getter only" cases. I do use getters and setters when needed. But most of getter/setter pairs I have seen are just like below; > private int x; > public int getX() { return x; } > public void setX(int x) { this.x = x; }
4
5530
by: Jimbo | last post by:
I am sort of new to C#. Currently have a private property called "_name" in a class. I have written a public getter and setter routine for it called "Name". Currently, the getter for the property does some data manipulation before it returns the value. I wanted to add another getter to this property that would returnt the "Raw" value (what is stored in _name). Example:
7
1484
by: none | last post by:
I'm trying to implement a simple repeateable property mechansism so I don't have to write accessors for every single instance variable I have. ------------ classMyObject: def __init__ (self): self.initialize() def initialize(self): self._value=None
1
1916
by: Steve | last post by:
I generate C# webservices proxy code from WSDL file, it turns out the classes generated have public member variables and no getter/setter methods as follows, and I am able to get data when running the client. public class MyFeeResponse {
3
99382
by: Martin Pöpping | last post by:
Hello, I´m coming from the Java World. Here Programmers often use (like in C++?) getter and setter methods. F.e.: class Mirror{ private int width_;
2
1402
by: Amie | last post by:
Hi, I have an atlas related question.. I have a web form that submits the information to a web service method, and it's done thru Atlas by binding the web methods to client functions. It seems to be working fine, except for when the class has a property with only getter (no setter), it freaks out, and returns the following
0
1187
by: shyamg | last post by:
Hi all i am newly add new Attribute "name" in strutshtml- tld file but its asking for setter method for attribute. where can add the setter and getter. Thanks. ss.
13
3034
by: globalrev | last post by:
wassup here? 7 Traceback (most recent call last): File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in <module> print cust1.getID() AttributeError: 'NoneType' object has no attribute 'getID'
0
7959
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5842
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2393
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.