-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
    `f1` => `Foo` : `Foo.xyz("value")`, but you already had that. What is your actual question? – H H Oct 26 '17 at 08:31
  • 7
    "I need to use same name method static and non-static." Why do you think you need that? Even if it was possible, it would only make your code less readable. – Zohar Peled Oct 26 '17 at 08:33
  • @HenkHolterman Foo class has some default variable. I create new object and set default variable. – Buraktncblk Oct 26 '17 at 08:38
  • No, classes don't have variables, they have fields. And your `Foo` has none. Try to be more clear when you ask for help. – H H Oct 26 '17 at 09:09

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
  • 79,642
  • 10
  • 69
  • 121