problem with static member

M

Marco

When compile I get this error:

An object reference is required for the nonstatic field, method, or
property 'ConsoleApplication1.Obj.env'.

I must do a static member that return a Obj instance.

Please help me.



public class tryClass
{

tryClass()
{


}

}


public class Obj
{
public tryClass env;

public static Obj fun(String a)
{
Obj MyObj = new Obj();

MyObj.i = 9;
MyObj.env=env;
return MyObj;
}
}



static void Main(string[] args)
{
Obj f=Obj.fun("lupo");
}
 
J

Jon Skeet [C# MVP]

Marco said:
When compile I get this error:

An object reference is required for the nonstatic field, method, or
property 'ConsoleApplication1.Obj.env'.

I must do a static member that return a Obj instance.

Your design isn't particularly clear (not helped by ignoring the normal
naming conventions) but the line with the problems is:

MyObj.env = env;

Now, what value are you expecting to use for the right hand side here?
 
S

Slawek Weclewski

try this

public class static12

{

public static12()

{

}

static void Main(string[] args)

{

Obj f=Obj.fun("lupo");

}

}

public class Obj

{

public static12 env;


public static Obj fun(String a)

{

Obj MyObj = new Obj();

static12 env = new static12();

MyObj.env= env;

return MyObj;

}

}
 

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