Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
So i have this method right
C#
public static void poop(String hi){
 String hiBoss = hi;
 hiBoss.TrimStart();
}


and I want to make a string that is equal to the parameter but I can edit it?
how can i do that, it gives me an error that it is read only
Posted
Comments
PIEBALDconsult 19-Sep-15 12:35pm    
Strings are immutable, but StringBuilders aren't, maybe you want one of those?

1 solution

There are a couple of ways you might want to look at.
The first is to return the new value as the result of the method:
C#
public static string poop(string hi)
   {
   return hi.TrimStart();
   }
You would then use it like this:
C#
string trimmed = poop("   Hello World!   ");
Console.WriteLine(trimmed);

The second is to pass a reference to the parameter:
C#
public static void poop(ref string hi)
   {
   hi = hi.TrimStart();
   }
And you then use it like this:
C#
string untrimmed = "   Hello World!   ";
poop(ref untrimmed);
Console.WriteLine(untrimmed);
In both cases, it will print "Hello World! " on the console.
 
Share this answer
 
Comments
Member 10812829 19-Sep-15 13:25pm    
Thank you!!!
OriginalGriff 20-Sep-15 4:21am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900