所以,我有一个方法,我设置一个日期并将其存储在一个名为gameTime的变量中。这样可行。但是现在我创建了另一种方法,我希望在控制台中显示该变量并编写 Console.WriteLine(gameTime); ,但这不被接受。它说,它在目前的背景下并不令人兴奋。我需要做什么才能在其他方法中使其可访问?这是代码......
public class TimeSimulation
{
public int Second { get; set; }
public int Minute { get; set; }
public int Hour { get; set; }
public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public static void TimeSettings()
{
TimeSimulation startTime = new TimeSimulation();
startTime.Second = 1;
startTime.Minute = 1;
startTime.Hour = 1;
startTime.Day = 1;
startTime.Month = 1;
startTime.Year = 2000;
DateTime gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
}
public static void ShowTime()
{
//DateTime gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
Console.WriteLine(gameTime);
}
答案 0 :(得分:0)
因为您的变量gameTime
仅存在于TimeSettings
方法中。
尝试在方法之外取出它并将其声明为如下字段:
DateTime gameTime;
public static void TimeSettings()
{
TimeSimulation startTime = new TimeSimulation();
startTime.Second = 1;
startTime.Minute = 1;
startTime.Hour = 1;
startTime.Day = 1;
startTime.Month = 1;
startTime.Year = 2000;
DateTime gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
}
public static void ShowTime()
{
//DateTime gameTime = new DateTime(startTime.Year, startTime.Month,
if(gameTime != null)
{
startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
Console.WriteLine(gameTime);
}
}
编辑:
我的猜测是,某个地方有一个错位的括号,因为您正在从其内部创建另一个TimeSimulation
类的实例。
public class TimeSimulation
{
public int Second { get; set; }
public int Minute { get; set; }
public int Hour { get; set; }
public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }
} // I think this bracket was missing..
static DateTime gameTime;
public static void TimeSettings()
{
TimeSimulation startTime = new TimeSimulation();
startTime.Second = 1;
startTime.Minute = 1;
startTime.Hour = 1;
startTime.Day = 1;
startTime.Month = 1;
startTime.Year = 2000;
gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
}
public static void ShowTime()
{
//DateTime gameTime = new DateTime(startTime.Year, startTime.Month,
if(gameTime != null)
{
startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
Console.WriteLine(gameTime);
}
}
答案 1 :(得分:0)
这是因为您在第一个方法中创建了gameTime
,因此只能在这些范围内访问{
和}
。
如果要从ShowTime()
访问它,则必须将其置于范围之外。你可以这样做:
public static DateTime gameTime { get; set; }
public void TimeSettings()
{
TimeSimulation startTime = new TimeSimulation();
startTime.Second = 1;
startTime.Minute = 1;
startTime.Hour = 1;
startTime.Day = 1;
startTime.Month = 1;
startTime.Year = 2000;
gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
}
public void ShowTime()
{
//DateTime gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
Console.WriteLine(gameTime);
}
请注意,您的方法不能以这种方式保持静态。如果必须这样,你可以这样调用gameTime:
CLASSNAME.gameTime
答案 2 :(得分:0)
如果要在另一个
中使用它,必须在函数中返回值public static DateTime TimeSettings()
{
TimeSimulation startTime = new TimeSimulation();
startTime.Second = 1;
startTime.Minute = 1;
startTime.Hour = 1;
startTime.Day = 1;
startTime.Month = 1;
startTime.Year = 2000;
return gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
}
public static void ShowTime()
{
DateTime gameTime = TimeSettings()
Console.WriteLine(gameTime);
}
答案 3 :(得分:0)
public class TimeSimulation
{
public int Second { get; set; }
public int Minute { get; set; }
public int Hour { get; set; }
public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }
private static DateTime gameTime;
public static void TimeSettings()
{
TimeSimulation startTime = new TimeSimulation();
startTime.Second = 1;
startTime.Minute = 1;
startTime.Hour = 1;
startTime.Day = 1;
startTime.Month = 1;
startTime.Year = 2000;
gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
}
public static void ShowTime()
{
//DateTime gameTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
Console.WriteLine(gameTime);
}
}
现在应该可以正常工作,因为“gameTime”现在是一个类级变量,而不是在TimeSettings方法中创建。
希望您确保在TimeSettings方法之后调用ShowTime。
仅供参考 - 这与问题的概念无关,但我想告诉TimeSettings方法的命名约定不正确。我更喜欢将其更改为“SetTimings()”或“InitializeTimings()”。