How to include variables such as references to arrays within a string in C#

Go To StackoverFlow.com

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?

2012-04-04 04:38
by programmerNOOB
Look up String.Format - John Saunders 2012-04-04 04:45
This is a simple syntax error at " + namesarr[i], balancesarr[i]";Paul Bellora 2012-04-04 04:47


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
2012-04-04 04:49
by gideon
small typo - the values are out of position to their placeholders, ie. {0} should be namesarr[i], not balancesarr[i], et - Will 2012-04-04 04:54
@Will yep updated it. Was a little distracted while typing : - gideon 2012-04-04 04:55
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 2012-04-04 05:00
@Will I just added that so the OP knows what he did wrong. I updated my answer to clarify this - gideon 2012-04-04 05:06
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 correctl - programmerNOOB 2012-04-04 05:13
From what I can see, the arguments you're passing would be valid for a function like searchAccounts(int,int,double,string). Your function expects arrays but you are passing single elements of the array into the function - gideon 2012-04-04 05:50


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];
2012-04-04 04:48
by Akash KC
Ads