简体   繁体   中英

Passing a string as multiple parameters?

Hey guys I'm fairly new to C# and I'm wondering if there's an easy way to pass a string as multiple parameters. Here's an example:

I want to pass to a function that takes these parameters:

DoStuff(int a, string b, string c, string d)

I have a string, say "string e" that contains the following: 1,a,b,c

So I'd like to call the function like so, DoStuff(e). But of course this results in errors because it expects more parameters. Is there a simple way to pass my string of parameters to the function?

EDIT: Thanks for all the advice on function overloads. This function is a class constructor, can it have overloads? Here's the code

arrayvariable[count] = new DoStuff(e);

You would need to make an overload of the method which takes a single string. It could then split the string and create the appropriate parameters.

For example:

void DoStuff(int a, string b, string c, string d)
{
    // Do your stuff...
}

void DoStuff(string parameters)
{
    var split = parameters.Split(',');
    if (split.Length != 4)
          throw new ArgumentException("Wrong number of parameters in input string");

    int a;
    if (!int.TryParse(split[0], out a)
          throw new ArgumentException("First parameter in input string is not an integer");

    // Call the original
    this.DoStuff(a, split[1], split[2], split[3]);
}

Granted, it would be possible to refactor this into a method which could make the string parsing more generic, and reusable, if this is something you'll be doing often.

public void DoStuff( int a, string b, string c, string d )
{
    //your code here
}

public void DoStuff( string e )
{
   string[] splitE = e.Split( ',' );

   int a;
   int.TryParse( splitE[0], out a );

   DoStuff( a, splitE[1], splitE[2], splitE[3] );
}

You'll need additional error checking for the splitting and parsing of the int, but that should do it for you

public void DoStuff(int a, params String[] strings)
{
    foreach (String s in strings)
    {
        do something else;
    }
}

The 'params' attribute indicates that DoStuff can have 0 or more strings as parameters, and the compiler automatically stuffs them in an array for you.

Just create an overloaded method that contains only one parameter:

DoStuff(string a){
    DoStuff(0, a, "","");
}

(pseudocode)

You have a couple options here, depending on what you are actually trying to do. One is that you could split the string up and send each part to the particular parameter that it should be assigned to (or send the full string to each parameter if that is what you want).

The other option would be to overload the method and either accept just one parameter (if you only need one) or split your string into the parts and then call the original method. I would recommend against splitting the string in an overloaded method unless you know for sure what the order is going to be of the values inside the string. My fear is that down the road you use the overload but get the order of the values wrong inside the string. This would cause unexpected results. As a result, I would recommend doing the first option unless you have a clear case for creating an overloaded method.

If you own the DoStuff method, create an overload with one parameter DoStuff(string e)

Then pass the string and split the string in the method using the delimiter.

You could also make them optional parameters. http://msdn.microsoft.com/en-us/library/dd264739.aspx

void DoStuff(string a, string b = null, string c = null, string d = null)
{

}
Public void DoStuff(String e){
    string[] params = s.Split(",");
    int a = int.Parse(params[i]);
    string b = params[1];
    string c = params[2];
    string d = params[3];
    DoStuff(int a, string b, string c, string d);
}

If these represent cards, then I'd move away from strings, and create yourself a real type. Perhaps something like this:

public class Card
{
    public int a;
    public string b, c, d;

    public Card(int a, string b, string c, string d)
    {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    public static Card Parse(string input)
    {
        int a;
        string b, c, d;
        // There are several string splitting methods shown in other answers, pick the one you like.
        return new Card(a, b, c, d);
    }
}

Make your methods take Card as parameter, rather than a string. You can also use Card for all your storage, the arrayvariable[] you showed above.

I would recommend that you don't use the names a, b, c, d in your class, use real names. Rank, Suit, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM