An object reference is required

S

simonZ

I have class, which returns dataTable to populate my list box on some other
pages:

public class dataClass

{
private Int16 _lvlRights=1

public static DataTable dtAdv()
{
if (_lvlRights == 1)
{
//my code......
}

}

}

I get an error:

Error 1 An object reference is required for the nonstatic field, method, or
property 'dataClass._lvlRights'

If I remove static keyword from dtAdv method than it works, but than I can't
call my class from other pages:

lstBox.DataSource=dataClass.dtAdv()

How can I use variable _lvlRights in my static methods?

Regards,S
 
M

Morten Wennevik

Hi Simon,

You must make _lvlRights static.

private static Int16 _lvlRights = 1;
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi ,

You can either make lvlRights static or remove the static from the method.
If you make static lvlRights you have to be careful with concurrency in case
that variable could be changed from an external property.

Additionally you could make your class a singleton.

Another route is declare your class as static (if you are using 2.0 ) this
will prevent you from declaring any instance member
 

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