Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Is Creating a Dynamic StringBuilder Array Possible?

I am trying to dynamically create a StringBuilder array in C# and I haven't had much luck.

I want to add information to a StringBuilder (sb) based off of a given client digit that will change dynamically depending on how many clients I might have. I might have 2 or I might have 20. The sb will be used to build a report based per client later in my program.

For example I have the following 3 clients:

    Client A = 0 
    Client B = 1
    Client C = 2

If Client A is reporting he has 8 apples I want to add the string "8 apples" to sb[0].

If Client B is reporting he has 3 oranges I want to add the string "3 oranges" to sb[1].

The above is just a simple example of the idea I am trying to accomplish. In reality I am going to be adding a lot of information to the sb.

I have tried the following without much luck getting them to work they way I expected or wanted.

StringBuilder[] sbFileError = new StringBuilder[clientCount];
List<StringBuilder> sbFileError = new List<StringBuilder>();

Any thoughts? Is it possible to create a StringBuilder array?

Thanks!

like image 848
buzzzzjay Avatar asked Mar 08 '26 09:03

buzzzzjay


1 Answers

You've created the containers above, but you need to fill them with something. For example:

StringBuilder[] sbFileError = new StringBuilder[clientCount];
for (int ix = 0;  ix < clientCount;  ++ix)
    sbFileError[ix] = new StringBuilder();

Or

List<StringBuilder> sbFileError = new List<StringBuilder>();
for (int ix = 0;  ix < clientCount;  ++ix)
    sbFileError.Add(new StringBuilder());
like image 151
Ed Bayiates Avatar answered Mar 10 '26 23:03

Ed Bayiates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!