EEssam
asked on
Accessing a value in a static method
Hi,
I have this code:
public static void DownloadLastPostStringInBa ckground(s tring address)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Specify that the DownloadStringCallback method gets called
// when the download completes.
client.DownloadStringCompl eted += new DownloadStringCompletedEve ntHandler( DownloadLa stPostStri ngCallback );
client.DownloadStringAsync (uri);
}
private static void DownloadLastPostStringCall back(Objec t sender, DownloadStringCompletedEve ntArgs e)
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if (!e.Cancelled && e.Error == null)
{
string textString = (string)e.Result;
Check(Convert.ToInt32(text String));
}
}
Now Check() method called from DownloadLastPostStringCall back is not a static method (I can't make it static because I'm changing the properties of some control from it) and therefore I'm getting an error message saying:
Error 1 An object reference is required for the nonstatic field, method, or property '....FormStartup.Check(int )' ... 225 17 ...
Your help would be appreciated.
I have this code:
public static void DownloadLastPostStringInBa
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Specify that the DownloadStringCallback method gets called
// when the download completes.
client.DownloadStringCompl
client.DownloadStringAsync
}
private static void DownloadLastPostStringCall
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if (!e.Cancelled && e.Error == null)
{
string textString = (string)e.Result;
Check(Convert.ToInt32(text
}
}
Now Check() method called from DownloadLastPostStringCall
Error 1 An object reference is required for the nonstatic field, method, or property '....FormStartup.Check(int
Your help would be appreciated.
make them not static ....
what static means is that it doesnt need anything to be instanstiated before you run the code. Ie it only relies on static members and constants. If you need to use soemthing non static you cant declare the function as static.
ASKER
There should be a solution.
not a tidy one. You could staticly instanstiate your class somewhere and call the function on the static version of itself but why on earth would you do that?
Why is it important that these methods are static? there is almost no overhead for making this an instance method unless you have millions of instances
ASKER
Good question. Check here:
http://msdn2.microsoft.com/en-us/library/system.net.downloadstringcompletedeventargs(VS.80).aspx
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
http://msdn2.microsoft.com/en-us/library/system.net.downloadstringcompletedeventargs(VS.80).aspx
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
ummmm thread safety is a completely seperate issue, if your trying to break into a non-thread safe meathod from something you want to be thread safe then your going to have issues. The only thing i can think of for you is to instansiate your class and call tmp.Check(). You cant have a static member access a non static one, it just wont work.
ASKER
I just removed "static" keyword from the two methods and they still work.
What may be the side effect?
I'm not experienced in C# or .NET.
Please advise.
What may be the side effect?
I'm not experienced in C# or .NET.
Please advise.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
both of your methods look fine (they don't access internal state). The one I would have worries about is your Check() method ... Does it change internal state in a non-atomic way (i.e. is it ok to be re-entrant?). You can put up code for it if you like and I can probablytell you but if it calls other methods then I would need to see them too most likely.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.