开发者

C# String.Format and object as argument

开发者 https://www.devze.com 2023-02-09 10:09 出处:网络
Here is my idea I am reading a string from my .resx file And here is a sample of such string :\"I am writing this from {}\"

Here is my idea I am reading a string from my .resx file

And here is a sample of such string :"I am writing this from {}"

I wrote a function to pass values to those arguments. I don't know the number of arguments expect开发者_开发技巧ed by the string

    public string MyFormattedString (string resourceName, object param=null)
    {
        string fStr= Resources.ResourceManager.GetString(resourceName);

        fStr= string.Format(fStr, param);

        return fStr;
    }

If I call my function with MyFormattedString ("resourceName", "noWhere"), I do not get what I am expecting What's wrong?


I found a solution to my issue with using params object[] I just discovered

public string MyFormattedString (string resourceName,  params object[] param)
{
    string fStr= Resources.ResourceManager.GetString(resourceName);

    fStr= string.Format(fStr, param);

    return fStr;
}


The resource string should be "I am writing this from {0}" with a numeric position in it.

0

精彩评论

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

关注公众号