2012-06-04 105 views
1

我有這個任務是基於硬幣和紙牌遊戲,這是非常簡化的。我們給了一些完整的和一些不完整的文件。我想要做的是從另一個類(card.cs)調用另一個類(hand.cs)中的方法(這實際上是一個字符串)。調用字符串方法從一個類到另一個

這裏是card.cs字符串方法:

public string ToString(bool shortFormat, bool displaySuit) 
    { 
     string returnString; 

     // Describe the FaceValue. 
     FaceValue faceValue = GetFaceValue(); 
     string faceValueAsString = faceValue.ToString(); 
     if (shortFormat) { 
      if (faceValue <= FaceValue.Ten) { 
       faceValueAsString = (faceValue - FaceValue.Two + 2).ToString(); 
      } else { 
       faceValueAsString = faceValueAsString.Substring(0, 1); 
      } 
     } 

     returnString = faceValueAsString; 

     // Describe the Suit. 
     if (displaySuit) { 
      string suit = GetSuit().ToString(); 
      if (shortFormat) { 
       suit = suit.Substring(0, 1); 
       returnString += suit; 
      } else { 
       returnString += " of " + suit; 
      } 
     } 

     return returnString; 
    } 

和hand.cs(只有字符串的ToString /方法,有在這個文件等功能,與建立一個手成交(列表命名卡),並添加卡。)

/// <summary> 
    /// Outputs the hand of cards. 
    /// See the ToString method in the Card class for a description of 
    /// the two parameters: shortFormat and displaySuit. 
    /// Pre: true 
    /// Post: Displayed the hand of cards. 
    /// </summary> 
    public void DisplayHand(bool shortFormat, bool displaySuit) { 

     // 
     //**************** CODE NEEDS TO BE ADDED********************** 
     // Should be able to call the ToString method in the Card class, 
     // as part of this. 
     // 

    } // end DisplayHand 

他們是未分配的文件,我得到了作業。我想知道的是如何在DisplayHand(shortFormat, displaySuit)中使用TwoString(shortFormat, displaySuit)。在一個階段,我有一個單獨的列表來放置字符串值,但它自從被刪除後試圖將文件還原爲原始文件。我不太清楚遊戲後期會如何使用它,但我想我是否可以使用列表來實現它,然後將列表更改爲字符串或數組,或者稍後可以很容易地完成的任何事情。一旦我知道如何調用這個字符串,我應該能夠修改我必須調用的所有其他字符串和整數的代碼。

+0

你做一個錯字與TwoString(...)方法? (在最後一段) – Pacane

+0

如果你的教授發現了這個問題,並且可以將它與你的工作聯繫起來,那麼你會錯過「你的態度A」。 –

+0

@Pacane - 對,對不起。整個上午我一直在這樣做...我的壞。 AlexiLevenkov&Austin Salonen - 我認爲他們不會因爲我們的態度而降低我們的評分。我看了一下標準表,知道我真的需要做多少事情。它沒有提到「態度」,在這個階段,我懷疑我會完成這項任務,所以有什麼用......我只是試圖爲考試努力學習。 –

回答

4

您需要撥打Card才能撥打ToString。我想你會這樣做:

foreach (Card card in this.Cards) { ... } // Loop through cards in this hand. 

我不能告訴你究竟如何沒有代碼。

一旦你有一個Card(在card變量),調用ToString這樣的:

string str = card.ToString(true, true); 
+0

好吧,它沒有給我錯誤,所以希望這是我的大腦無法自行提出的答案。謝謝你的幫助 :) –

相關問題