1

I need to replace all quotes in my string to double quotes using a function, but I don't know how to return replaced versions of the strings. I'm new at c# so any help will be appreciated.

here is my code

    private void ReplaceAllQuotes(string Name, string Contact, string    CallType, 
                                  string Reason, string Notes, string State, 
                                  string Consultant, string CustNo, string Time, 
                                  string SubReason, string AddedBy, string AddedOn, 
                                  string UpdatedBy, string UpdatedOn)
    {
        Name = Name.Replace("'", "\"");
        Contact = Contact.Replace("'", "\"");
        CallType = CallType.Replace("'", "\"");
        Reason = Reason.Replace("'", "\"");
        Notes = Notes.Replace("'", "\"");
        State = State.Replace("'", "\"");
        Consultant = Consultant.Replace("'", "\"");
        CustNo = CustNo.Replace("'", "\"");
        Time = Time.Replace("'", "\"");
        SubReason = SubReason.Replace("'", "\"");
        AddedBy = AddedBy.Replace("'", "\"");
        AddedOn = AddedOn.Replace("'", "\"");
        UpdatedBy = UpdatedBy.Replace("'", "\"");
        UpdatedOn = UpdatedOn.Replace("'", "\"");
    }
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
Ben J
  • 37
  • 3
  • Either make the variables `ref` or better yet don't do this in a method, just do the replacements in the scope you need them. – juharr Jul 16 '15 at 13:47
  • It seems like all these parameters are related so you could create a class to group them together and move this logic to the class implementation – vc 74 Jul 16 '15 at 13:48
  • Thank you everyone for great help – Ben J Jul 20 '15 at 15:37

4 Answers4

3

Notice all the copypasta you did? Every time you ctrl-v'd

.Replace("'", "\"");

you should have felt an overwhelming sense of guilt. If not, you'll never be a true programmer.

A true programmer does things only once, because we're lazy.

public string YerSingleQuotesSuck(string incorrectString)
{
    if(string.IsNullOrWhiteSpace(incorrectString)
        return ""; // or throw, or do whatever
    return incorrectString.Replace("'", "\"");
}

And now you can easily fix your errors

something.Name = YerSingleQuotesSuck(something.Name);
something.Contact = YerSingleQuotesSuck(something.Contact);

Some things to remember here--methods encapsulate logic that can be used any number of times, thus keeping you from having to copypaste that logic everywhere.

Also, strings are immutable, which means you can't change them. String methods always take a string and transform it into another string. You need to replace the first string with the transformed string.

Other solutions to your question, such as using the ref keyword aren't optimal in this situation. Method signatures are better short than long.

Last, go grab CLR Via C#, skip the first two chapters, and read.

  • He is transforming the original strings reassigning them to the new values. He is just making the changes in a context that is short lived (local to the function). – Veverke Jul 16 '15 at 13:51
  • @Veverke not sure what you're saying. What he has is obviously not working for him, and shows a few new programmer mistakes. –  Jul 16 '15 at 14:03
  • I will not call them "programmer mistakes" - specially when he introduced himself as a beginner - he is unaware of Software design fundamentals. In any case, the reason his code was not working is not because he was expecting the strings themselves to change to other values - but because the resulting values he got were all lost upon finishing the function's execution. – Veverke Jul 16 '15 at 14:18
1

I'm assuming you want to replace all those strings in a certain context. Don`t do all of them together like that; make a simpler and more general method and use that several times instead:

private string ReplaceAllQuotes(string anyWhichEverString){
    return anyWhichEverString.Replace("'", "\"");
}

This will return each updates string after editing it.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
1

You can indeed use an extension method to centralize your replace in one place, but since your problem is making the changes to the local parameter variables inside your function persist onwards, you could add the "ref" keyword to each of your parameter variables, so the changes you make inside your function reflect in the function caller.

Since you have so many parameters, I'd suggest you create a class with all these fields, like

public class MyObject

{
    public string Name;
    public string Contact;

}

and then have your function receive a reference to this object, such as

private void ReplaceAllQuotes(MyObject obj)
{
   ...
}

this way any changes to your obj, inside the function, will persist, because "obj" is Reference Type variable, meaning, it holds a reference instead of the real data value(s) your object is supposed to keep. It points to somewhere in memory where space for all MyObjects's fields were allocated. Thus, any changes to obj.Name and so will persist, because all other references to this instance of MyObject you just passed into the method - they all (unless you changed them on intent) point to the same memory address.

Veverke
  • 9,208
  • 4
  • 51
  • 95
  • Passing by reference and a type being a reference type are two different things. – juharr Jul 16 '15 at 13:48
  • I meant variables of ReferenceType are always passed by reference. – Veverke Jul 16 '15 at 13:50
  • No they are not. For instance `string` is a reference type and it is not passed by reference. A reference is passed, but it's not passed by reference. – juharr Jul 16 '15 at 13:50
  • hmm... thanks for the insight. I thought it was as simple as I put it. I knew strings were a "special case", however. Don't know if playing a bit with it will help me, but I will do it now. – Veverke Jul 16 '15 at 13:52
  • "There's a big difference between passing a reference by value and passing an object by reference." J.Skeet Here's the full post: http://stackoverflow.com/a/1096456/1432385 – Lucaci Andrei Jul 16 '15 at 14:01
  • Yes, I just refreshed the topic... (after running a simple test). It honestly almost never was an issue in the day-to-day work, but yes, passing an object reference to a method, means passing a reference by value, which means that any changes to the reference's attributes will reflect outside the method because the objects references all point to the same memory address, anywhere. But in assigning a new instance to such reference, locally in the function, will have no effect outside, since the outside reference is independent from the method's parameter, which was a copy of the reference. – Veverke Jul 16 '15 at 14:10
1

As others have mentioned, I'd first suggest creating a class to encapsulate all of your parameters for a couple of reasons:

  1. Messing up the order of the parameters when you call your method
  2. Being able to add values without having to change the interface for your method

The reason your updated values aren't available to you after you call the method has to do with context. Others have provided detail on handling that, but I'd suggest a different approach to make this easier to understand: return a new instance of your class from your method.

The extension method to handle replacing quotes:

public static class Extensions
{
    public static string ReplaceQuotes(this string value)
    {
        return value.Replace("'", "\"");
    }
}

A simplified version of your values encapsulated in a class:

public class Record
{
    public string Name { get; set; }

    public string Contact { get; set; }

    //...
    public Record(string name, string contact)
    {
        this.Name = name;
        this.Contact = contact;
    }

    public Record(Record r)
    {
        this.Name = r.Name.ReplaceQuotes();
        this.Contact = r.Contact.ReplaceQuotes();
    }
}

Then finally, your method to update your record:

private Record ReplaceAllQuotes(Record r)
{
    return new Record(r));
}

This way when you return from ReplaceAllQuotes() all of the quotes are correct and you don't have to worry about parameter types, although you certainly will need to familiarize yourself with those things if you plan on continuing with C# (or any other C-family language, basically).

Sven Grosen
  • 5,616
  • 3
  • 30
  • 52