2

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!

4
  • 3
    You are on the right track. Why doesn't that work for you? Commented Aug 3, 2011 at 21:11
  • 1
    "I have tried the following without much luck getting them to work they way I expected or wanted." Explain "without much luck" and what you "expected" and "wanted". Commented Aug 3, 2011 at 21:12
  • 1
    Why an Array? A much better solution may be to use a Dictionary with a unique client identifier as a key, and a StringBuilder as a value. You could even use a Dictionary<int, StringBuilder> to mimic, to some extent, your array solution. Commented Aug 3, 2011 at 21:12
  • @KirkWoll It would some time works depending on how it ran, I didn't even realize that I didn't initialize it properly. Commented Aug 3, 2011 at 21:20

5 Answers 5

10

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());
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I didn't realize that I didn't initialize it properly. It's already been a long day. I hadn't found much about using string builder arrays and was just thinking it might not be possible.
That's what we're here for buzzzzjay :-)
Thanks a lot. I like all the system programmers. :)
3

Creating the array is the first step, but this only creates places to store StringBuilders. You haven't actually instantiated any StringBuilders at this point. You'll need to do something like this....

StringBuilder[] sbFileError = new StringBuilder[clientCount];
for (int i = 0; i < sbFileError.Length; i++)
    sbFileError[i] = new StringBuilder();

Comments

2

I think you are missing instantiation of array elements. This code works.

int clientCount = 3;
StringBuilder[] sbFileError = new StringBuilder[clientCount];
for(int i=0; i<clientCount; i++)
{
    sbFileError[i] = new StringBuilder();
}

sbFileError[1].Append("Hello World!");

Comments

1
List<StringBuilder> sbFileError = new List<StringBuilder>();

Looks OK, but you have to fill it:

for (int i = 0; i < numClients; i++)
   sbFileError.Add(new StringBuilder());

1 Comment

is the sbFileError.Ass a typo?
1

You could also get fancy:

var items = Enumerable.Range(0, clientCount)
                      .Select(i => new StringBuilder());

var list = new List<StringBuilder>(items);

or shorten it to:

var list = Enumerable.Range(0, clientCount)
                     .Select(i => new StringBuilder())
                     .ToList();

Just another egg in the basket.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.