-5

I cant access static method from new object and not allow create same name non-static method.I need to use same name method static and non-static. Foo class has some default variables. I create new object and set default variables. Sample code block

class Foo
{
    public void abc()
    {
        //...
    }
    public static string xyz(string s)
    {
        return "bla bla";
    }
}

public void btn1_click()
{
    System.Windows.Forms.MessageBox.Show(Foo.xyz("value"));
    //Works OK
}

public void btn1_click()
{
    Foo f1=new Foo();
    //f1..
    f1.xyz("value");
    //Cant access non static method.
}

Thanks in advance.

Buraktncblk
  • 31
  • 2
  • 9

1 Answers1

0

If the class has default values, the correct place to populate them is in the class constructor:

public class Foo
{
    public Foo()
    {
        // set default values here.
    }
}

If you still want to use these default values as static members - no problem:

public class Foo
{

    public static const int DEFAULT_INT_VALUE = 5;

    public Foo()
    {
        IntValue = DEFAULT_INT_VALUE;
    }

    public int IntValue {get;set;}
}
Zohar Peled
  • 76,346
  • 9
  • 62
  • 111