Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing a value in a static method

Posted on 2007-11-20
11
Medium Priority
?
343 Views
Last Modified: 2013-12-17
Hi,

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);
            client.DownloadStringAsync(uri);
        }

        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;

                Check(Convert.ToInt32(textString));
            }
        }

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.
0
Comment
Question by:EEssam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 20324384
make them not static ....
0
 
LVL 8

Expert Comment

by:static-void
ID: 20324517
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.
0
 

Author Comment

by:EEssam
ID: 20324943
There should be a solution.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:static-void
ID: 20325055
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?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 20325072
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
0
 

Author Comment

by:EEssam
ID: 20325078
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.
0
 
LVL 8

Expert Comment

by:static-void
ID: 20325106
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.
0
 

Author Comment

by:EEssam
ID: 20325114
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.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 900 total points
ID: 20325122
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 ...

Cheers,

Greg
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 20325128
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.
0
 
LVL 8

Assisted Solution

by:static-void
static-void earned 600 total points
ID: 20325131
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.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question