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
520 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 45
Asp.net mvc 5 5 22
Getfiles in vb.net 28 24
C# TextBox 11 15
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

20 Experts available now in Live!

Get 1:1 Help Now