У меня есть строка, которая определена, затем изменяется в методе, вызываемом после ее определения, значение по-прежнемуC#

Место общения программистов C#
Ответить
Anonymous
 У меня есть строка, которая определена, затем изменяется в методе, вызываемом после ее определения, значение по-прежнему

Сообщение Anonymous »

Я создаю консольное приложение на C#, у меня есть несколько переменных, которые определяются в начале программы, а затем сразу после вызова метода. Метод ссылается на эти переменные, поскольку изменяет некоторые из них. Немного позже я вызываю метод, использующий эти переменные, и он делает это с исходными определениями, а не с теми, которые определены в предыдущем методе.
Это фрагменты кода, относящиеся к вопрос:

Код: Выделить всё

string foregroundColor = "White";
string backgroundColor = "Black";
testMap1(ref map, foregroundColor, backgroundColor);

static void testMap1(ref string[,] map, string foregroundColor, string backgroundColor)
{
resetMap(ref map, foregroundColor, backgroundColor);
foregroundColor = "Red";
backgroundColor = "Black";

static void resetMap(ref string[,] map, string foregroundColor, string backgroundColor)
{
foregroundColor = "White";
backgroundColor = "Black";

static void displayPosition2(ref string[,] map, int[] place, string foregroundColor, string backgroundColor)
{
if (foregroundColor == "Red")
{
Console.ForegroundColor = ConsoleColor.Red;
}
else if (foregroundColor == "Green")
{
Console.ForegroundColor = ConsoleColor.Green;
}
else if (foregroundColor == "Yellow")
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
else if (foregroundColor == "Magenta")
{
Console.ForegroundColor = ConsoleColor.Magenta;
}
else if (foregroundColor == "DarkGreen")
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
}
else if (foregroundColor == "Black")
{
Console.ForegroundColor = ConsoleColor.Black;
}
else if (foregroundColor == "Blue")
{
Console.ForegroundColor = ConsoleColor.Blue;
}
else if (foregroundColor == "Cyan")
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
else if (foregroundColor == "DarkBlue")
{
Console.ForegroundColor = ConsoleColor.DarkBlue;
}
else if (foregroundColor == "DarkCyan")
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
}
else if (foregroundColor == "DarkGrey")
{
Console.ForegroundColor = ConsoleColor.DarkGray;
}
else if (foregroundColor == "DarkMagenta")
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
}
else if (foregroundColor == "DarkRed")
{
Console.ForegroundColor = ConsoleColor.DarkRed;
}
else if (foregroundColor == "DarkYellow")
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
}
else if (foregroundColor == "Gray")
{
Console.ForegroundColor = ConsoleColor.Gray;
}
else
{
Console.ForegroundColor = ConsoleColor.White;
}
После этого он должен выводить текст красным, но он выводит белым. После нескольких тестов проблема заключается в первом определении, используемом в методе displayposition2.
Я хотелось бы знать, почему это не работает так, как задумано, все, с кем я разговаривал в реальной жизни, говорили, что это должно работать.

Подробнее здесь: https://stackoverflow.com/questions/783 ... fter-its-d
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»