kathleenie.xx
kathleenie.xx

Reputation: 127

Call a string method from one class to another

I have this assignment based on coin and card games which is very simplified. We are given some complete and some incomplete files. What I am trying to do is invoke a method (which is really a string) from one class (card.cs) in another (hand.cs).

Here is the string method from 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;
    }

and from hand.cs (the ToString string/method only, there are other functions in this file that deal with creating a hand (list named cards) and adding cards to it.)

/// <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

They are the unedited files I got for the assignment. What I want to know is how to use the TwoString(shortFormat, displaySuit) in DisplayHand(shortFormat, displaySuit). At one stage I had a separate list to put the string values in, but it since got deleted trying to revert the files back to the original. I am not quite sure how this is going to be used later in the game, but I figured if I could get it functioning with a list, then changing the list to a string or an array or whatever could be done quite easily later. Once I know how to call this string I should be able to modify the code for all the other strings and integers I have to call.

Upvotes: 1

Views: 429

Answers (1)

Kendall Frey
Kendall Frey

Reputation: 44384

You need a Card to call ToString on. I assume you would do it something like this:

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

I can't tell you exactly how without seing the code.

Once you have a Card (in the card variable), call ToString like this:

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

Upvotes: 4

Related Questions