Error passing a variable value to another class or method

  • Thread starter Thread starter CSharp Enthusiast
  • Start date Start date
C

CSharp Enthusiast

Guest
Hi, I am trying to do something basic and run into errors. I am trying to assign the value of a variable "test" in main method to a private class Test. I get error: A field initializer cannot reference the non-static field, method, or property. Member cannot be accessed with an instance reference; qualify it with a type name instead


public void Main()
{
string test = 'This is a test string';

Test t = new Test(ctest);
}

private class Test
{
string mtest;

public Test(string teststr)
{
mtest = teststr;
}


public string newTest = mtest; //Error: A field initializer cannot reference the non-static field, method, or property. Member cannot be accessed with an instance reference; qualify it with a type name instead
}



Not sure, Where I am doing it wrong. Could you please guide me.

Thank you in advance.


SQLEnthusiast

Continue reading...
 
Back
Top