views:

781

answers:

5

I need a variable that is available from any function in my code, but is different to all users.

so i wrote:

public class classedelcappero
{
    private string frasehiragana = String.Empty;
    public string ohiragana
    {
        get
        {
            return frasehiragana;
        }
        set
        {
            frasehiragana = value;
        }
    }
}

and then i put this in my function:

protected void Page_Load(object sender, EventArgs e)
{
    classedelcappero chepalle = new classedelcappero();
    string frasehiragana = chepalle.ohiragana;
}

and it works... but if i access it from another function, like

protected void ButtonRiprova_Click(object sender, EventArgs e)

the value is empty... is that ok?

what to do? Use Session variables?

+2  A: 

For variables you want tied to a specific user, yes, use Session variables.

In your example, you're creating an object with a property that is lost immediately. You would need to store it somewhere, and reuse it to access its value in the ButtonRiprova_Click method.

Nader Shirazie
You can write a static class to access / set known session variables with strong-typing.
ck
+2  A: 

What do you mean access it from another function? What exactly are you accessing? Do you make a new 'classdelcappero'?

You have only declared 'chepalle' and 'frasehiragana' within your Page_Load so their scope ends at the end of that method. Finally you aren't changing the 'frasehiragana' of your 'chepalle' instance (you are just setting a new string) and even if you were you would just be setting it to what it is already by default (String.empty).

One possibility is to have a 'classdelcappero' instance within your page code-behind class. This is set on Page_Load based on a session property (a cookie, the IP, something belonging to your user that you are using to distinguish them). Then any function can just refer to this instance knowing it is already initialised correctly.

For example:

class CodeBehind : Page
{
    private classdelcappero chepalle;
    protected void Page_Load(object sender, EventArgs e)
    {
        chepalle = new classedelcappero();
        chepalle.frasehiragana = ""; /* value based on user */
    }
}
Graphain
yes, i make a new class declaration. maybe that's it, because i make a NEW class! I didn't think about that... thank you!!!
Magnetic_dud
btw... how i can reuse a class in another function...?
Magnetic_dud
if your variable is a field (not a local variable), ie defined with class scope, not inside the method, as it is in this example, then you can access it in any method in the class.
Nader Shirazie
A: 

Yes use session variable.

 string frasehiragana = chepalle.ohiragana;

in Page_Load method create string variable in that method which is not accesible to ButtonRiprova_Click. If you want to give user same value on all pages the store it in session object

Session["frasehiragana"]=chepalle.ohiragana;

or if you are setting it in every page then declare that vairable at class level not in function:

 string frasehiragana;
 protected void Page_Load(object sender, EventArgs e)
 {
     classedelcappero chepalle = new classedelcappero();
     string frasehiragana = chepalle.ohiragana;
 }
TheVillageIdiot
+2  A: 

Hi,

This is one way you can do it,

Create a class that derives from System.Web.UI.Page and add one public property to the class, it will look like.

    public class MyPage : System.Web.UI.Page
    {
            public string Frasehiragana
            {
                get
                {
                    try{
                    return HttpContext.Current.Application.Contents["_Frasehiragana"] as string;
                    }
                    catch
                    {
                          return null;
                    }
                }
                set
                {
                    HttpContext.Current.Application.Add("_Frasehiragana", value);
                }
            }
    }

Then derive all your asp.net pages from MyPage class instead of System.Web.UI.Page, your asp.net page code behind class may look like this after you do this.

public partial class _Default : MyPage
.
.
.

Now you can use it as following in all your pages.

this.Frasehiragana;

Thanks,

this. __curious_geek
i tried that way, but in my experiments I put "public class MyPage : mainclassname", so I got an error on run. I will investigate further, thank you!!!
Magnetic_dud
mainclassname must be System.Web.UI.Page. This solution is about injecting code in inheritance hierarchy. You can do all what you want in the injected class [MyPage]. The only condition is that it must derive from System.Web.UI.Page.
this. __curious_geek
+1  A: 

You may want to take a look at this series of blog posts:

Mark Seemann