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
527 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 44

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 44

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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 44

Accepted Solution

by:
AndyAinscow earned 300 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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

861 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