问题描述:

Possible Duplicate:

Why do I get “cannot be accessed with an instance reference” when using teststring.Join but not teststring.Split? (c#)

I am not sure what is wrong with the code. I tried googling but didn't got relevant info.

This is my for loop.

 for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 5; j++)

{

slide.Move(s, i, j); // this is the problem.

}

}

and this is my move function. According to me I think I'm calling the function the way it is suppose to but I am not able to figure out what is wrong. I am fairly new the language.

 protected static void Move(string s, int x, int y)

{

Console.WriteLine("I am in Move function");

try

{

Console.SetCursorPosition(origCol + x, origRow + y);

Console.Write(s);

}

catch (ArgumentOutOfRangeException e)

{

Console.Clear();

Console.WriteLine(e.Message);

}

for (int i = 0; i < DEFAULT_SIZE; ++i)

{

for (int j = 0; j < DEFAULT_SIZE; ++j)

{

Console.Write("#");

Console.Write("\r{0}% ", i,j);

//Console.WriteLine(slider[i, j]);

}

Console.WriteLine();

Console.ReadKey();

}

}

网友答案:

Move is a staic method so you can call it using ClassName.Move.

So, you have 2 options here.

  1. Change slide.Move(s, i, j); to use the class name, not an instance variable. For example, if the class name Slide then you should use Slide.Move(s, i, j);.

  2. Change Move to be an instance method.

I think option 2 is the way to go. As Jon Skeet pointed out. It make more sense that Move should be an instance method.

网友答案:

protected static void Move(string s, int x, int y)

static members belong to the class not an instance of the class. To access them, you precede them with the name of the class

[ClassName].Move(s, x, y);  //Slide.Move(x, y, z); assuming your class name is Slide

To use it like that you need to remove the static modifier

protected void Move(string s, int x, int y)
相关阅读:
Top