-2

I am trying to make a simple string extension that assigns the changes to the original string (like toUpper).In this case, the method is assigning the contents to a second argument if it is neither white space nor null...otherwise, it leaves the current value in place, or assigns a null value to "". So, I would like to have it go:

somerecord.Property1.func(someobj.property);
somerecord.Property2.func(someobj.otherproperty);
somerecord.Property3.func(someobj.anotherproperty);

while my code looks like

    public static string func(this String str, string replacement)
    {
        if (!String.IsNullOrWhiteSpace(replacement)) {
            str = replacement;
            return replacement;
        }
        else
        {
            if(str == null)
              str = "";
            return "";
        }
    }

I wanted to set this to ref but I can't. Any ideas on how to implement this cleanly?

user3856804
  • 65
  • 1
  • 9
  • 1
    I'm fairly certain there is no way to do that. You can't `ref this` (i.e., you can't modify the variable) and you can't modify the string instance. – Michael Gunter Feb 06 '17 at 22:59
  • 1
    @ThomasD. Extension methods will work perfectly fine with `null`, Not sure what you are trying to say. – InBetween Feb 06 '17 at 23:03
  • Thomas D., If I create an object obj that has an undefined property string s, then obj.s.func(something) will show string as null. Michael Gunter, how do methods like ToLower and ToUpper function then? – user3856804 Feb 06 '17 at 23:05
  • 1
    They return a copy, but don't modify the original string. See here (https://msdn.microsoft.com/en-us/library/ewdd6aed(v=vs.110).aspx) for an example with ToUpper. In particular, from that link "This method does not modify the value of the current instance. Instead, it returns a new string in which all characters in the current instance are converted to uppercase.". – Kolichikov Feb 06 '17 at 23:06
  • @InBetween Thanks for pointing that out! In fact, i did not know that you can call extension methods for null. I deleted my wrong comment. – Thomas D. Feb 06 '17 at 23:08
  • It is well known fact that [string replace does not work](http://stackoverflow.com/questions/13277667/c-sharp-string-replace-does-not-work)... you may want to learn from that :) – Alexei Levenkov Feb 06 '17 at 23:15
  • 1
    Side note: @user3856804 I would [edit] out that "changes to the original string (like toUpper)" part of the question - there is no good reason to include statements that can be immediately verified as false by glancing at documentation. I'd not be surprised that to work as downvote magnet. – Alexei Levenkov Feb 06 '17 at 23:18
  • I interpreted the sentence differently, though it is admittedly not that clear. I think the OP was saying that toUpper(sic) is an example of a string method that they'd like to change the behavior of, not that it's one that currently behaves that way. – itsme86 Feb 07 '17 at 00:22
  • See also e.g. https://stackoverflow.com/questions/39855976/change-value-using-extension-method-in-c-sharp-as-allowed-in-vb-net and https://stackoverflow.com/questions/2618597/impossible-to-use-ref-and-out-for-first-this-parameter-in-extension-methods – Peter Duniho Feb 07 '17 at 01:40

1 Answers1

1

Don't make the method an extension method, implement a regular static method:

public static void func(ref string str, string replacement)
{
    if (!String.IsNullOrWhiteSpace(replacement)) {
        str = replacement;
    }
    else
    {
        if(str == null)
          str = "";
    }
}

Do notice however that your use case will still not work, you can not pass a property as a ref argument to begin with:

class Foo
{
    public string someVariable;
    public string SomeProperty { get; }
}

var foo = new Foo();
func(ref foo.someVariable, ""); //ok
func(ref foo.SomeProperty, ""); //compile time error
InBetween
  • 32,319
  • 3
  • 50
  • 90