I have a method that draws a triangle using a loop that prints to the console each line. I require to change the modifiers to public static string instead of public static void so I require a return type of string. How could I manipulate my code so that the method returns a multi-lined string of the drawn shape?
1 Answer
Investigate System.Text.StringBuilder. Create one at the start of your method, then instead of Console.Write call yourStringBuilderInstance.Append(). At the end of your method, return yourStringBuilderInstance.ToString().
7 Comments
mattappdev
Thanks for your reply! I'm receiving: there is no argument given that corresponds to the required formal parameter 'length' from the
stringBuilder.ToString(" ");Dour High Arch
I said
yourStringBuilderInstance.ToString() not .ToString(" "). If you get an error you have to add your code to your question.Dour High Arch
You should use a descriptive name like
triangle, not stringBuilder, and .AppendLine() instead of WriteLine(). And throw an ArgumentException instead of an error message so your caller will know it got an error instead of a triangle.mattappdev
Understood. It's currently printing one big singular line as opposed to the shape. I suppose the string should be in some sort of multi-lined format?
Dour High Arch
I don't know what “singular lines” are; you have to post what you have to your question because I can't see your screen. Your question still has
.Append() where I told you to use .AppendLine(). |
string[]? If so I can help you.