0

I am making a C# bot and what it basically does is send messages to a discord webhook. To my bot I added the question "How many links does the message need to have" For example: 1 line = "Message" 3 lines = "Message" "Message" "Message"

I know how to do that. But, there is multiple things someone can choose from. I have set the limit to 10 I dont know how to make the string as much as needed.

Do I need to use switch cases, or if else statements?

AmountOfLines is user input how many lines MessageToSend it the message that will be sent Message is the user input for the message

So I did

String MessageToSend
if(AmountOfLines == 1)
{
    MessageToSend = Message;
}
if(AmountOfLines == 2)
{
    MessageToSend = Message + Environment.NewLine + Message;
}

And so on. But the MessageToSend in the lines after the code shown before just acts like it is never seen before. It shows Error use of unassigned local variable "MessageToSend"

I want to know what way i need to use to give the string different text depending on an input.

If anything is wrong about question, tell me in comments and dont close it becausr it has happened many times.. I am new.

  • So, you want the `MessageToSend` to be a string that consists of whatever is in the variable `Message` repeated `AmountOfLines` time, separated by _newLines_. Did I get that right? Create an array `var messages = new string[AmountOfLines]`. Use a `for` loop to assign each entry in the array to `Message`, and then use `string.Join` to glue everything together – Flydog57 May 11 '21 at 23:46
  • 1
    @Letsgoyea For the code provided, what do you think about to replace all by `MessageToSend = string.Join(Environment.NewLine, Enumerable.Repeat(Message, AmountOfLines))` –  May 11 '21 at 23:58
  • 1
    Is it really right to close this question as a dup of _"Error: Use of unassigned local variable_"? Yeah, the OP mentions that, and the dup answers that tiny part of the question, but the overall question is how to build up the `MessageToSend` string (answered in the comments) – Flydog57 May 12 '21 at 00:06

2 Answers2

1

Am I missing something? Isn't this as simple as doing:

string msg2Send = "";
// Caps the iteration count at 10 as per problem definition...
for( int i = 0; i < Math.Min(AmountOfLines, 10); i++ )
    msg2Send += ( (msg2Send.Length > 0) ? Environment.Newline : "" ) + Message;

What's the complexity?

NetXpert
  • 511
  • 5
  • 14
  • 1
    Doing string concatenation in a `for` loop is often a bad idea. Look into `StringBuilder` instead. When you concatenate a string like that, you produce _garbage_ (temporary strings with very short lifetimes). Doing it a few times is OK, but if you loop a lot, you'll be putting the _Garbage Collector_ to work more than you should – Flydog57 May 12 '21 at 00:03
  • @Flydog57 -- The OP _literally_ said in the problem definition that the cap was 10 iterations... – NetXpert May 12 '21 at 00:16
  • 1
    @Flydog57, even if you “loop a lot” the garbage collector is probably [more efficient than you realize](https://blog.codinghorror.com/the-sad-tragedy-of-micro-optimization-theater/). – Dour High Arch May 12 '21 at 00:21
1

Use StringBuilder for better memory usage

using System.Text;

...

int AmountOfLines = 3;
string Message= "Hello";

StringBuilder MessageToSend = new StringBuilder(AmountOfLines);
for (int i = 0; i < AmountOfLines; i++)
{
   MessageToSend.AppendFormat("{0}{1}", Message, Environment.NewLine);
}

MessageToSend.Length--; // this will remove the last new line
Console.WriteLine(MessageToSend);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116