I am currently trying to write a piece of code that will increment letters. I have written the below function and this works perfectly fine. (ignore the test function name and any missing outer required parts, just wanted to show the relevant parts)
string s1;
void TestFunction()
{
s1 = IncrementString(s1);
}
string IncrementString(string s)
{
return ((char)(s[0] + 1)).ToString();
}
What I would prefer to write is the following one liner to use this.
s1.IncrementString();
I however, am unsure on what this is actually called and such unable to find the correct words to google to find out more info on this. I am not looking for a complete solution, just the name and area to look into to learn more about this.
Hopefully I have provided enough information here, if not, please let me know and I will try to update it.
Thanks
If you want to add a method to an existing class like this its called an Extension Method.
it looks like this:
Many thanks, couldn’t beleive how close I actually was to this. Much appreciated.
If you were wondering, I took this into another script in my project to handle methods such as this going forward. I did get a message about the class not being static when using this, but that was fixed up in the new script anyway. I’ve still a lot to learn.
Thanks again
Look up extension methods
Thanks, checking out some info on this now and a video on youtube. I have also managed to solve my problem alreaady with the help of the previous commenter. Thanks again.