Saturday, March 24, 2012

passing variable value to method in .cs

I would like to set
errormessage and retrieve it in my method QWE()
It doesnt work. What am I doing wrong?

public class AppLogic
{
string errormessage ="We are sorry, DB Error try again later";

public static void QWE()
{
HttpContext.Current.Response.Write("<script type=text/javascript language=javascript>alert('" + errormessage +"');</script>");
}
}

Mate, How can we help you if you dont tell us what error message you get?

Try this though...

public string errormessage = errormessage ="We are sorry, DB Error try again later";


Embarrassed

CS0120: An object reference is required for the nonstatic field, method, or property 'AppLogic.errormessage'

public class AppLogic
{
publicstaticstring errormessage ="We are sorry, DB Error try again later";

public static void QWE()
{
HttpContext.Current.Response.Write("<script type=text/javascript language=javascript>alert('" +
AppLogic.errormessage +"');</script>");
}
}

static method can only manipulate static field.


lovely it works :)
thank you

Best to use getters and setters when passing information in vairable about your system.

//Delcare the variable as private.

#region Declerations

private

staticstring fromAddress =string.Empty;

#endregion

//Delcare the getters and setters as public

#region Properties

publicstaticstring FromAddress {get {return fromAddress; }set { fromAddress =value; } }

#endregion

0 comments:

Post a Comment