zimmer9
asked on
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?
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
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
ASKER
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(routingtran sit);
stringb.Append(bankacct);
COMMENT: specify code page of the index date
CODEPAGE:819
COMMENT: CHECK NUMBER #0
GROUP_FIELD_NAME:CheckNumb er
GROUP_FIELD_VALUE: <------- checknum
GROUP_FIELD_NAME:RoutingTr ansit
GROUP_FIELD_VALUE: <------- routingtransit
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:BANK
GROUP_FIELD_NAME:BankAccou ntNo
GROUP_FIELD_VALUE: <------- bankacct
GROUP_FIELD_NAME:CheckAmou nt
GROUP_FIELD_VALUE:
GROUP_FIELD_NAME:CpcsNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaid Date
GROUP_FIELD_VALUE:
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:
For ex: stringb.Append(checknum);
stringb.Append(routingtran
stringb.Append(bankacct);
COMMENT: specify code page of the index date
CODEPAGE:819
COMMENT: CHECK NUMBER #0
GROUP_FIELD_NAME:CheckNumb
GROUP_FIELD_VALUE: <------- checknum
GROUP_FIELD_NAME:RoutingTr
GROUP_FIELD_VALUE: <------- routingtransit
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:BANK
GROUP_FIELD_NAME:BankAccou
GROUP_FIELD_VALUE: <------- bankacct
GROUP_FIELD_NAME:CheckAmou
GROUP_FIELD_VALUE:
GROUP_FIELD_NAME:CpcsNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaid
GROUP_FIELD_VALUE:
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(CheckOrderN um);
stringb.Append(Environment .NewLine);
stringb.Append("GROUP_FIEL D_NAME:Che ckNumber") ;
stringb.Append(Environment .NewLine);
stringb.Append("GROUP_FIEL D_VALUE:") ;
checknum = s.Substring(246, 6);
stringb.Append(checknum);
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(CheckOrderN
stringb.Append(Environment
stringb.Append("GROUP_FIEL
stringb.Append(Environment
stringb.Append("GROUP_FIEL
checknum = s.Substring(246, 6);
stringb.Append(checknum);
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])
X.appendline("name:{0}")
Then once you have all the lines just do some thing like this
String.format(X.tostring()
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])
ASKER
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.
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
sw.WriteLine(stringb);
To this:
sw.WriteLine(stringb.ToStr
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.