Accessing a value in a static method


I have this code:

        public static void DownloadLastPostStringInBackground(string address)
            WebClient client = new WebClient();
            Uri uri = new Uri(address);

            // Specify that the DownloadStringCallback method gets called
            // when the download completes.
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadLastPostStringCallback);

        private static void DownloadLastPostStringCallback(Object sender, DownloadStringCompletedEventArgs 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;


Now Check() method called from DownloadLastPostStringCallback 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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
EEssamAuthor Commented:
There should be a solution.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
EEssamAuthor Commented:
Good question. Check here:

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.
EEssamAuthor Commented:
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.
EEssam you are missing the concept of static methods and thread safety there ...

It is saying that if *IT* has a method that is static you can assume it to be thread-safe but instance methods it defines are to be assumed to be non-thread-safe ...

This is a disclaimer on every page in MSDN ... It does not apply to your code ...



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
potential issue with thread safety is that you get a concurrent access problem if two things try to change an object at the same time. You need to make sure that your code wont do this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.