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
523 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 62

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now