[英]How to output an integer from another method via Console.WriteLine()?
So, I have a method where I set a date and store it in a variable called gameTime.所以,我有一个方法,我可以设置一个日期并将其存储在一个名为 gameTime 的变量中。 That works.
那个有效。 But now I created another method where I want to show that variable in the console and wrote Console.WriteLine(gameTime);
但是现在我创建了另一种方法,我想在控制台中显示该变量并编写Console.WriteLine(gameTime); but this is not accepted.
但这不被接受。 It says, it does not excist in the current context.
它说,它在当前上下文中不存在。 What do I need to do to make that accessable in the other method?
我需要做什么才能在其他方法中访问它? Here is the code...
这是代码...
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);
}
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);
}
}
This should work now as "gameTime" is now a class level variable rather than created inside TimeSettings method.这现在应该可以工作,因为“gameTime”现在是一个类级别的变量,而不是在 TimeSettings 方法中创建。
Hope you make sure that you will be calling ShowTime after TimeSettings method.希望您确保在 TimeSettings 方法之后调用 ShowTime。
FYI - This is not related to the concept of the question, but would like to tell that the naming convention for TimeSettings method is not correct.仅供参考 - 这与问题的概念无关,但想说明 TimeSettings 方法的命名约定不正确。 I prefer to change it to "SetTimings()" or "InitializeTimings()".
我更喜欢将其更改为“SetTimings()”或“InitializeTimings()”。
Because your variable gameTime
only exists in the TimeSettings
method.因为你的变量
gameTime
只存在于TimeSettings
方法中。
Try taking it outside the method and declare it as a field like this:尝试将其带出方法并将其声明为这样的字段:
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);
}
}
EDIT: My guess is that there is a misplaced bracket somewhere because you're creating another instance of your TimeSimulation
class from within itself..编辑:我的猜测是某处有一个错位的括号,因为您正在从自身内部创建
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);
}
}
This is because you created gameTime
within your first method, so it is only accessable within those scopes ( the {
and }
).这是因为您在第一个方法中创建了
gameTime
,因此它只能在这些范围( {
和}
)内访问。 If you want to access it from ShowTime()
you have to put it outside of the scopes.如果你想从
ShowTime()
访问它,你必须把它放在范围之外。 You can do it like this :你可以这样做:
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);
}
Note that your methods can't be static this way.请注意,您的方法不能以这种方式保持静态。 If they have to be, you can call gameTime like this:
如果必须这样做,您可以像这样调用 gameTime:
CLASSNAME.gameTime
You must to return value in your function when you want to use it in another当你想在另一个函数中使用它时,你必须在你的函数中返回值
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.