Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to recode my C# console application to reformat the StringBuilder object which is comprised of both fixed and variable data?

Posted on 2014-01-21
6
Medium Priority
?
533 Views
Last Modified: 2014-01-26
I am writing a C# console application in VS2010.

I read a binary check image file and within this file, everytime I read a particular record type, I retrieve the following variable field values based on their displacement AFTER the record type field value.

Do you know how I could rewrite my attached code to populate the variable whose values are assigned in a FOR LOOP?

For each check, I need to write out the following records that are comprised of Fixed
Columns coupled with variable values that are retrieved by reading the binary check image file whose logic is contained in the FOR LOOP.

COMMENT: CHECK NUMBER #                  (variable CheckOrderNum)
GROUP_FIELD_NAME:CheckNumber
GROUP_FIELD_VALUE:                             (variable checknum)
GROUP_FIELD_NAME:RoutingTransit
GROUP_FIELD_VALUE:                             (variable routingtransit)
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:BANK
GROUP_FIELD_NAME:BankAccountNo
GROUP_FIELD_VALUE:                             (variable bankacct)
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:                             (variable checkamt)
GROUP_FIELD_NAME:CpcsNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:                             (variable checkpaiddate)
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:                             (variable officenumber)
GROUP_OFFSET:                                      (variable checkOffset)
GROUP_LENGTH:                                     (variable checkimagelength)
GROUP_FILENAME:/clientdoc/Z10.ard.out

I created the following StringBuilder object:

StringBuilder stringb = new StringBuilder();  
            stringb.Append("COMMENT: CHECK NUMBER #");
            stringb.Append(CheckOrderNum);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:CheckNumber");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:");
            stringb.Append(checknum);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:RoutingTransit");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:");
            stringb.Append(routingtransit);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:BankName");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:BANK");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:BankAccountNo");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:");
            stringb.Append(bankacct);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:CheckAmount");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:");
            stringb.Append(checkamt);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:CpcsNo");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:00000000");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:CheckPaidDate");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:");
            stringb.Append(checkpaiddate);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_NAME:OfficeNo");
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FIELD_VALUE:");
            stringb.Append(officenumber);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_OFFSET:");
            stringb.Append(checkOffset);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_LENGTH:");
            stringb.Append(checkimagelength);
            stringb.Append(Environment.NewLine);
            stringb.Append("GROUP_FILENAME:/clientdoc/" + System.IO.Path.Combine(MyGlobals.BASE_FILE_Name + ".ard.out"));
stringbuilder.txt
0
Comment
Question by:zimmer9
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39797225
I don't understand your problem.  Just keep appending to the string builder until you have done everything you need to.  Then you have the resulting string.

eg.
//your current code
for(....)
{
if(something)
  stringb.Append("something was true");
else
  stringB.Append("something else to be appended");
}
0
 

Author Comment

by:zimmer9
ID: 39797248
I was just wondering if I could somehow create all the stringb appends BEFORE entering the FOR LOOP.

If I can't, then I would have to create all the appends inside the FOR LOOP AFTER
creating the following 2 statements before entering the FOR LOOP.

StringBuilder stringb = new StringBuilder();  
stringb.Append("COMMENT: CHECK NUMBER #");.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39797263
You do what you want with the string builder.  As long as you use Append it does NOT replace/remove anything you put into it earlier.  There are no problems.

example
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: 39797303
What I'm trying to state is that I start out with the following 2 statements BEFORE the FOR LOOP:

StringBuilder stringb = new StringBuilder();  
stringb.Append("COMMENT: CHECK NUMBER #");

then I need to perform            stringb.Append(CheckOrderNum);
inside the FOR LOOP because I don't know the value for CheckOrderNum ahead of time, until I read it's value.
So would I need to perform all subsequent Append statments inside the FOR LOOP?

Is there a way to peform this process so that all Appends occur BEFORE entering the FOR LOOP where I retrieve the variable values?
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 1200 total points
ID: 39797330
>>So would I need to perform all subsequent Append statments inside the FOR LOOP?
and
>>Is there a way to peform this process so that all Appends occur BEFORE entering the FOR LOOP

Put that way - there is no way both statements can be correct.  (Not without some sort of time machine).


ps.  There is nothing to stop you calling a function inside the for loop and nothing to stop you passing the stringbuilder object into a function.

eg.
for(....)
{
   Foo(sb, x);
}

and
private void Foo(StringBuilder sb, X x)
{
  sb.Append("hello ");
sb.Append(x.ToString());
}
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 800 total points
ID: 39797464
Hi zimmer9;

The following code snippet is using the Northwind database to create a file using StringBuilder. The important part of the code is where I format the strings to have a specific width so that all the columns line up.

Use this web page Composite Formatting to find out meaning of {0,-20} and others.

// Code to get data from database to show how to format data using StringBuilder
var db = new NorthwindEntities();
var results = (from e in db.Employees
               select new
               {
                   e.LastName,
                   e.FirstName,
                   e.City,
                   e.Country,
                   e.BirthDate
               }).ToList();

// String builder used to format the data from the database and used to write to a file
var sb = new StringBuilder();
// Create a column header for each of the fields to be shown.
sb.Append(String.Format("{0,-20}  {1,-20}  {2,-10}  {3,-15}  {4,-10}\n", "Last Name", "First Name", "City", "Country", "Birthdate"));
// Create the StreamWriter
var sw = new StreamWriter("C:/Working Directory/Test.txt");
// Write the header to the file
sw.WriteLine(sb.ToString());
// Clear the StringBuilder of any data
sb.Length = 0;
foreach(var rec in results)
{
    // Build all the records formatted to the header spacing
    sb.Append(String.Format("{0,-20}  {1,-20}  {2,-10}  {3,-15}  {4,-10}\n",
        rec.LastName, rec.FirstName, rec.City, rec.Country, (rec.BirthDate == null) ? "" : rec.BirthDate.Value.ToShortDateString()));
}
// Write all the records to the file
sw.Write(sb.ToString());
// Close the file
sw.Close();

Open in new window

Test.txt
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

972 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