0

I am having trouble with a method that is called within main and is contained in the same class. The method is outputting a string value and my problem is trying to include references to arrays within the string message. I am getting the message

Only assignment, call, increment, decrement, and new object expressions can be used as a statement"

Along with invalid expression in regards to the comma and a whole bunch of ; expected all in regards to the following line:

searchResult = "Account #" + accountsarr[i] + " has a balance of {0:c}" + " for customer " + namesarr[i], balancesarr[i]";

all within the following method:

public static string searchAccounts(ref int AccountNumber, int[] accountsarr, double[] balancesarr, string[] namesarr)
{
    bool isValidAccount = false;
    int i = 0;

    while (i < accountsarr.Length && AccountNumber != accountsarr[i])
    {
        ++i;
    }

    if (i != accountsarr.Length)
    {
        isValidAccount = true;
    }

    string searchResult;

    if (isValidAccount)
    {
        searchResult = "Account #" + accountsarr[i] + " has a balance of {0:c}" + " for customer " + namesarr[i], balancesarr[i]";
    }
    else
        searchResult = "You entered an invalid account";

    return searchResult;
}

So how do you return a string from a method that has references to array positions within the text that should be the string?

2
  • 2
    Look up String.Format. Commented Apr 4, 2012 at 4:45
  • 1
    This is a simple syntax error at " + namesarr[i], balancesarr[i]"; Commented Apr 4, 2012 at 4:47

2 Answers 2

4

You should use string.Format like this:

 searchResult = string
        .Format("Account # {0} has a balance of {1:c} for customer {2}",
         accountsarr[i], balancesarr[i], namesarr[i]);

The error you get is syntactic, you have a comma instead of a + and an extra "

Just you know why your code isn't compiling:

searchResult = "Account #" + accountsarr[i] + " has a balance of {0:c}" 
     + " for customer " + namesarr[i], balancesarr[i]"; << this is an extra "
                                   //^ you cannot put a comma here
Sign up to request clarification or add additional context in comments.

6 Comments

small typo - the values are out of position to their placeholders, ie. {0} should be namesarr[i], not balancesarr[i], etc
@Will yep updated it. Was a little distracted while typing :S
The 2nd part doesn't make sense - it won't format the balance, but concatenate the raw value after the name. Your initial recommendation is much clearer.
@Will I just added that so the OP knows what he did wrong. I updated my answer to clarify this.
Thanks for the help, I used the string.Format method and that seem to remove the error. My problem now is the call to the method within main. Console.WriteLine(searchAccounts(tmpAccountNumber, accounts[0], balances[0], names[0])); is the call from main and the message I am getting is "The best overloaded method match for searchAccounts(int, int[], double[], string[])' has some invalid arguments" I don't get it though as all my data types are lined up correctly
|
2

You can simply do the following:

searchResult = "Account #" + accountsarr[i] + " has a balance of "+String.Format("{0:c}",balancesarr[i]) + " for customer " + namesarr[i];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.