Reputation: 37
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("'", "\"");
}
Upvotes: 1
Views: 162
Reputation: 11408
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.
Upvotes: 1
Reputation: 5636
As others have mentioned, I'd first suggest creating a class to encapsulate all of your parameters for a couple of reasons:
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).
Upvotes: 1
Reputation:
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.
Upvotes: 3
Reputation: 19151
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.
Upvotes: 1