Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a class as
C#
public class Customer
{
public Customer()
{
//
// TODO: Add constructor logic here
//
}

private static string custName;

public static string CustomerName
{
get { return Customer.custName; }
set { Customer.custName = value; }
}



<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;<asp:Button ID="Button1"
runat="server" Text="Set CustomerName" onclick="Button1_Click" /><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>&nbsp;
<asp:Button ID="Button2" runat="server" Text="Get CustomerName"
onclick="Button2_Click" />
</div>

on button1 i am setting CustomerName and on on button2 i am getting CustomerName

the variable is priavte static in a class

My question how it hold the value when i set a customer name to "somexyz" and get it back when i am saying get?



Is it possible to do it with non-static variable ,if yes then how?
Posted
Updated 16-Apr-12 6:10am
v2
Comments
ZurdoDev 16-Apr-12 12:11pm    
fixed pre tags.
Sergey Alexandrovich Kryukov 16-Apr-12 12:59pm    
Do you mean across HTTP requests/responses? If so, why do you think static can help you? :-)
--SA
Sergey Alexandrovich Kryukov 16-Apr-12 13:31pm    
I like the tag "oops". This is exactly the topic... :-)
--SA
Rakesh S S 17-Apr-12 12:29pm    
what about solution buddy got my problem?
Sergey Alexandrovich Kryukov 20-Apr-12 16:20pm    
To have a solution, I would need a problem, isn't that reasonable?
There is no problem here. I think you do have a problem, but you did not share it with us so far.
--SA

1 solution

Your question isn't clear.

Of course it is possible to store data without the use of static (I suspect static is wrong in any case).

Do you want to persist the value between http request/response calls. If so Google for "ASP.NET session state". This data only exists as long as the session exists, it is not suitable for long-term storage. It also takes some extra work if the system is on a load-balanced environment.

If you need long term storage, you will need to persist to database. You need to google ADO.NET for this, but you might well want to look up "LINQ To SQL" and "Entity Framework" for other persistance options that may be better suited to your needs.
[Update]
As promised in my comment:
http://msdn.microsoft.com/en-us/library/ms178581.aspx[^]
 
Share this answer
 
v2
Comments
Rakesh S S 17-Apr-12 7:50am    
on button1 click i am assigning value to static variable from textbox1
on button2 click i am assigning value to textbox2 whichever i set initially
the moment i set where it keep the value of a static variable
and get it back when i needed
Sergey Alexandrovich Kryukov 20-Apr-12 16:21pm    
Why? You motivation is completely unclear. I don't see any reason for doing so. What are you trying to achieve with that?
--SA
Keith Barrow 17-Apr-12 9:02am    
Again, what do you mean by "Get it back when I need"?
1) Within the same session (i.e. not saving on a long term basis) across requests
2) Saving long term, such as storing in a database?
Rakesh S S 17-Apr-12 12:22pm    
yes obvious not for long term but
still for some moment don't know exactly
Keith Barrow 17-Apr-12 15:21pm    
If you want to store the value between request/responses and don't want to use a DB (at least directly) then use the Session variable. I will update my answer with a link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900