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
530 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
[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
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

626 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