I’ve got this
public static class MyClassHelper
{
DataContex db = new DataContext();
public static Type MyMethod()
{
//Do Something with db
// such as db.myTable
}
}
I’m getting the following error: “An object reference is required for the non-static field, method or property…“
Is there anyway to get around this?
How about this. I’ve an object that contains only integers, which is fine for all the internal functionings as it allows me to link tables. But occasionaly, I need to display some information to the user. That’s why, I’d like to create a static method so it would read the integer, look up in the DB, and display a name instead of a number.
I want it to be a static method so that I can use it in my View template.
Thanks for helping
As @Oskar indicates your static method can’t reference instance variables, only static variables. Rather than making the DataContext static, though, which would mean that it would exist for the life of the program, just create the DataContext as needed within the method. DataContext are best suited to a “unit of work” pattern and recreated as needed for just the task being accomplished rather than existing as a long-lived object. Be aware, too, that the DataContext is not thread-safe; you’ll be creating some really hard to find errors unless you make all of your methods thread-safe. It’s much simpler to just recreate the data context.