开发者

How to change parameter value in any function in C#?

开发者 https://www.devze.com 2023-04-04 04:29 出处:网络
I have 开发者_如何学运维this function private void fun(String a, String b, String c) { } I want to do something like this

I have 开发者_如何学运维this function

private void fun(String a, String b, String c)
{
}

I want to do something like this

private void fun(String a, String b, String c)
{
    clean(a, b, c);
    // params clean
}

private void clean(ref params String[] c)
{
    foreach (String t in c)
    {
        t = t.replace("'", " ");
    }
}

Of course this not works because a params cannot be ref.

There is a way to do this? I'm goint to use it in many functions, some of them have 10 to 15 parameters.

Thanks,


No, you can't do this. To be honest, if you've got a method with 10-15 parameters, that sounds like you could use some more encapsulation to start with - and at that point it's a different ballgame, because if you pass in a reference to an instance of a mutable class, your method can change the properties within that instance.

If you could give more concrete details of your use case that would help - it may be that you want to write an extension method on string, just for convenience:

internal string ReplaceQuotes(this string text)
{
    return text.Replace("'", " ");
}

Then you can just refer to foo.ReplaceQuotes() when you need to - it won't change the value of foo, but it means whatever you pass the return value to will be appropriately trimmed.

Note that if you're doing this to try to avoid SQL injection attacks, it would be a much better idea to use parameterized SQL instead. Of course, it may be nothing to do with that :)


Obviously you want a single magic method to do this, but since that isn't possible, don't discount the simple (but tedious) solution of multiple overloads.

public static class Cleaner
{
    public static string Clean(this string s)
    {
        return s.Replace("'", " "); 
    }

    public static void Clean(ref string s1)
    {
        s1 = s1.Clean();
    }

    public static void Clean(ref string s1, ref string s2)
    {
        s1 = s1.Clean();
        s2 = s2.Clean();
    }

    public static void Clean(ref string s1, ref string s2, ref string s3)
    {
        s1 = s1.Clean();
        s2 = s2.Clean();
        s3 = s3.Clean();
    }

    // etc ad nauseum...
}


{
    string a = ...;
    string b = ...;
    string c = ...;

    Cleaner.Clean(ref a, ref b, ref c)
}

It's not pretty, but if you really want to have this function, this is how you can have it.


The problem is that you can go from arguments to an array, but not the other way around. The best you can do still involves some manual assignments.

static void Main(string[] args)
{
    test("A'1", "B**", "'C'D");
}

static void test(string a, string b, string c)
{
    string[] args = new string[] { a, b, c }; //build array
    args = args.Select((s) => s.Replace("'", " ")).ToArray(); //clean array
    a = args[0]; //assign values from array
    b = args[1];
    c = args[2];
}


I tried:

   public Form1()
    {
        InitializeComponent();

        string[] s = new string[3];
        s[0] = "a";
        s[1] = "a";
        s[2] = "a";

        clean(s);

        for (int i = 0; i < s.Length; i++) label1.Text += s[i];
    }

    void clean(params String[] c)
    {
        for(int i=0;i<c.Length;i++) c[i] = "b";
    }

And the output was: bbb

So it seems that you can simply:

A: drop the ref.

B: use a for instead of foreach because foreach doesn’t let you assign a value to its iteration variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消