以不同的方法调用实例值

时间:2013-10-19 00:27:01

标签: c# methods

我不知道如何在另一种方法中访问我的实例。我有这组代码。

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);
                    }
                }

我试图在另一种方法中使用rorbcard.ToString(),但我无法弄清楚如何引用它。这是另一种方法。

 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);
                }

感谢您抽出时间研究这个问题。我被困住了一段时间。

2 个答案:

答案 0 :(得分:1)

你可以做任何一次

一个。作为参数传递给HighLow(),如

public void HighLow(string rorbcard)

湾创建一个类级变量。为其分配值并在HighLow()中使用该值

class MyClass
{
    private string rorbCardClassLevel;

    private void MyrorbCardMethod
    {
        //Other code lines.

        //Inside case statement
        rorbCardClassLevel = rorbcard.ToString();
    }

    private void HighLow()
    {
        //Use rorbCardClassLevel here.            
    }
}

答案 1 :(得分:0)

我需要做的就是将我的实例放在方法之外和课堂之外。咄