Solved

How to recode a FOR Loop so that the variables in the FOR Loop are assigned to a StringBuilder object that is created outside of the FOR Loop?

Posted on 2014-01-20
7
439 Views
Last Modified: 2014-01-22
I am trying to rewrite a C# console application to employ StringBuilder.

Do you know how I can recode the contents of my FOR loop so that the variables within the FOR loop can be assigned to the StrinBuilder object created outside of the FOR Loop?

My code is in the attached file.
stringbuilder.txt
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39795970
Well if you want to write the contents of the StringBuilder to the StreamWriter you need to change this:

sw.WriteLine(stringb);

To this:

sw.WriteLine(stringb.ToString());


To prepare the StringBuilder for the next iteration through the loop you need to clear the context of the StringBuilder by doing this:

stringb.Length = 0;

That should do it.
0
 

Author Comment

by:zimmer9
ID: 39795985
I don't seem to be generating any of the variable values in my output, all the values are BLANK:

For ex:       stringb.Append(checknum);
                  stringb.Append(routingtransit);
                  stringb.Append(bankacct);
             

COMMENT: specify code page of the index date
CODEPAGE:819
COMMENT: CHECK NUMBER #0
GROUP_FIELD_NAME:CheckNumber
GROUP_FIELD_VALUE:                            <-------    checknum
GROUP_FIELD_NAME:RoutingTransit
GROUP_FIELD_VALUE:                            <-------    routingtransit  
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:BANK
GROUP_FIELD_NAME:BankAccountNo
GROUP_FIELD_VALUE:                            <-------    bankacct
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:
GROUP_FIELD_NAME:CpcsNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39796005
Hi zimmer9;

That is because from what I see you write to the StringBuilder at the begining of the subroutine where all the values of the variables have been initialized to an empty string or in the case of CheckOrderNum to the value of 0. What you need to do is move the individual StringBuilder Appends to where you set the value of the variables. For example you would need to move this line from the top of the code :

stringb.Append(CheckOrderNum);

to after this line in your code:

CheckOrderNum = CheckOrderNum + 1;
stringb.Append(CheckOrderNum);

So first place all the column header text into StringBuilder and write that out to the StreamWriter then after you moved the other StringBuilder statement to the right locations the loop will write the data underneath it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zimmer9
ID: 39796056
Then the way I understand your observation is that I cannot build the entire StringBuilder structure beforehand due to the variable values.

Only the constant header values can be contained in the StringBuilder before entering the FOR LOOP.

As you can see, virtually every other record contains a variable value, so how could I place all the FIXED column header text into the StringBuilder before entering the FOR LOOP while leaving alternate records to be appended inside the FOR LOOP.  

For ex: It seems to me that I can only place the following 2 lines in the StringBuilder and then the rest of the records would be appended inside the FOR LOOP.


2 lines in the StringBuilder:
StringBuilder stringb = new StringBuilder();  
stringb.Append("COMMENT: CHECK NUMBER #");
----------------------------------------------------------------------------

then in the FOR LOOP
----------------------------
CheckOrderNum = CheckOrderNum + 1;
stringb.Append(CheckOrderNum);
stringb.Append(Environment.NewLine);

stringb.Append("GROUP_FIELD_NAME:CheckNumber");
stringb.Append(Environment.NewLine);
stringb.Append("GROUP_FIELD_VALUE:");
checknum = s.Substring(246, 6);

stringb.Append(checknum);
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39796295
What is the source of the data is it a single database row? If it is and you are iterating through the columns and know the column names but for some reason can't get the column names at runtime eg you are using ordinal position, you could use string builder to build a string pattern e.g
X.appendline("name:{0}")

Then once you have all the lines just do some thing like this
String.format(X.tostring(), row[0], row[1])

This does seem a bit clunky though your better being more oo create an object like a view model approach with matching properties and override to string with something injected so you can unit test it easily and the data isn't couple to your view or string representation, think about if this is plain text for and email what happens I you are asked to make it look good and send it as HTML?

Another route is your dealing with key values here? Why not use a collection iterate like so
Full collection from data source column name as key, field as value
Foreach key in collection. Keys
X.appendline(key + collection[key])
0
 

Author Comment

by:zimmer9
ID: 39796997
I am reading a binary check image file and I know the field values that I need to retrieve based on their displacement after a specific record type field value. For each check, there is another record of this specific record type.
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39802226
I would create an object that contains those fields with the exact names, populate the object and you could be clever using reflection and output the property name and value in an overriden tostring
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Get Current Exchange Rate from XE 9 42
Adjust the codes 3 63
What are MicroServices? 4 68
Need a quick overview of Selenium testing 2 36
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question