[英]Calling a instance value in a different method
I don't know how to access my instance in another method. 我不知道如何通过另一种方法访问我的实例。 I have this set of code.
我有这组代码。
Card rorbcard = new Card();
Deck deck = new Deck();
deck.Shuffle();
rorbcard = deck.TakeCard();
Start0:
Console.Clear();
Console.WriteLine("Allright lets play! Red or black?");
string userValue0 = Console.ReadLine();
switch (userValue0.ToLower())
{
case "red":
{
if (rorbcard.Suit.Equals(Suit.Diamonds))
{
Console.WriteLine("{0},\n Correct give 2 drinks",
rorbcard.ToString());
Thread.Sleep(2000);
}
else if (rorbcard.Suit.Equals(Suit.Hearts))
{
Console.WriteLine("{0},\n Correct give 2 drinks",
rorbcard.ToString());
Thread.Sleep(2000);
}
else
{
Console.WriteLine("{0},\n Wrong sucka take 2 drinks",
rorbcard.ToString());
Thread.Sleep(2000);
}
}
I am trying to use that rorbcard.ToString() in another method, but I cant figure out how to reference it. 我试图在另一种方法中使用该rorbcard.ToString(),但我不知道如何引用它。 Here is the other method.
这是另一种方法。
public void HighLow()
{
//highlow part of the game
Deck deck = new Deck();
deck.Shuffle();
Card highLow = new Card();
highLow = deck.TakeCard();
Start1:
Console.Clear();
Console.WriteLine("Your Hand:");
Grid.WriteAt(rorbcard.ToString(), 0, 1);
Console.WriteLine("\n\nDo you think that the next card will higher,lower,\n or
equal to the {0}. Enter high, low, or equal\n", rorbcard);
string uservalue1 = Console.ReadLine();
switch (uservalue1.ToLower())
{
case "high":
if (highLow.CardNumber > rorbcard.CardNumber) <-----Issue
{
Console.WriteLine("{0},\n Correct give 4 drinks",
highLow.ToString());
Thread.Sleep(2000);
}
else
{
Console.WriteLine("{0},\n Wrong drink 4\n", highLow.ToString());
Thread.Sleep(2000);
}
Thanks for your time in looking into this. 感谢您抽出宝贵的时间对此进行调查。 I am stuck and have been for a bit.
我被困住了,已经有一段时间了。
You can do either 你可以做
a. 一种。 Pass as parameter to
HighLow()
like 作为参数传递给
HighLow()
public void HighLow(string rorbcard)
b. b。 Create a class level variable.
创建一个类级变量。 Assign value to it and use that value in
HighLow()
like 给它分配值,并在
HighLow()
使用该值,例如
class MyClass
{
private string rorbCardClassLevel;
private void MyrorbCardMethod
{
//Other code lines.
//Inside case statement
rorbCardClassLevel = rorbcard.ToString();
}
private void HighLow()
{
//Use rorbCardClassLevel here.
}
}
All I needed to do was place my instances outside the methods and inside my class. 我要做的就是将实例放在方法之外和类中。 Duh
h
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.