Private, Public, ... ?

S

shapper

Hello,

I have the following class:

public class TagHelper {
private MyAppDataContext database = new MyAppDataContext();
public static bool Find(string name) {
bool found = database.Tags.Where(t => t.Name ==
this.Tag.Name).Any();
return !found;
}
}

I get the following error:
An object reference is required for the non-static field, method, or
property

On code line:
bool found = database.Tags.Where(t => t.Name == this.Tag.Name).Any();

I tried to change to the database instance to public:
private MyAppDataContext database = new MyAppDataContext();

But the problem happens.

What am I doing wrong?

Thank You,
Miguel
 
M

Martin Honnen

shapper said:
Hello,

I have the following class:

public class TagHelper {
private MyAppDataContext database = new MyAppDataContext();
public static bool Find(string name) {
bool found = database.Tags.Where(t => t.Name ==
this.Tag.Name).Any();
return !found;
}
}

I get the following error:
An object reference is required for the non-static field, method, or
property

On code line:
bool found = database.Tags.Where(t => t.Name == this.Tag.Name).Any();

It is a static method yet you try to use
this.Tag
the Tag property or field of an instance. Inside of the static method
you don't have an instance.
 
B

Ben Voigt [C++ MVP]

shapper said:
Hello,

I have the following class:

public class TagHelper {
private MyAppDataContext database = new MyAppDataContext();
public static bool Find(string name) {
bool found = database.Tags.Where(t => t.Name ==
this.Tag.Name).Any();
return !found;
}
}

I get the following error:
An object reference is required for the non-static field, method, or
property

On code line:
bool found = database.Tags.Where(t => t.Name == this.Tag.Name).Any();

I tried to change to the database instance to public:
private MyAppDataContext database = new MyAppDataContext();

But the problem happens.

What am I doing wrong?

You cannot use the keyword "this" inside a static method. Probably the Find
method should not be static.
 
S

shapper

You cannot use the keyword "this" inside a static method.  Probably theFind
method should not be static.

I was able to make it this way:

public class TagHelper {
private static MyAppDataContext database = new MyAppDataContext();

public static bool Find(string name) {
return database.Tags.Any(t => t.Name == name);
}
}

I tried a few versions of my code and I needed to add static to
MyAppDataContext.

Then I use this as follows:

bool a = TagHelper.Find("Some Name");

Thanks,
Miguel
 
C

Charles Calvert

I was able to make it this way:

public class TagHelper {
private static MyAppDataContext database = new MyAppDataContext();

public static bool Find(string name) {
return database.Tags.Any(t => t.Name == name);
}
}

I tried a few versions of my code and I needed to add static to
MyAppDataContext.

Be aware that this means that all instances of TagHelper will share an
instance of MyAppDataContext. If you need to use different instances
of MyAppDataContext in your application, you'll want to convert the
variable database and the method Find to instance members rather than
class members.
 
S

shapper

Be aware that this means that all instances of TagHelper will share an
instance of MyAppDataContext.  If you need to use different instances
of MyAppDataContext in your application, you'll want to convert the
variable database and the method Find to instance members rather than
class members.

I think in this case is ok since I will always be using something
like:

if (TagHelper.Find("Some Name")) { ... }

This is the only way I will use it. So I suppose it is ok, or not?

Thanks,
Miguel
 
C

Charles Calvert

I think in this case is ok since I will always be using something
like:

if (TagHelper.Find("Some Name")) { ... }

This is the only way I will use it. So I suppose it is ok, or not?

Yes, so long as you only need one instance of it in the application,
using it as an effective singleton is fine.
 

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