An object reference is required for the nonstatic field, method or

G

Guest

I am getting the above error with this Class Library code:

namespace ClassLibrary1
{
public class Class1
{
private string FoundPathKey;
public string LastError
{
get { return FoundPathKey; }
}
public static string Read(string path, string key)
{
string keyval = "";
try
{
FoundPathKey = "";
keyval = Microsoft.Win32.Registry.GetValue(path, key,
"Missing").ToString();
if (keyval == "Missing")
{
throw new Exception("Either the path <" + path + "> OR
the key <" + key + "> does not exist");
}
}
catch (Exception MyExcep)
{
FoundPathKey = MyExcep.Message.ToString();
}
return keyval;
}
}
}

on the two lines where I assign a value to FoundPathKey and I do not have a
clue why.

Could somene please point out what it is that I am missing?

Thank you.
 
M

Miroslav Stampar

FoundPathKey does not have a static modifier, while function Read has.
You can only access static fields from static methods, which has
perfect sense - static methods don't know each and every class
instance to access their instance's fields.

AA2e72E je napisao/la:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top