Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Format fixed length fields with leading zeros or spaces

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rowan - 14 May 2008 19:30 GMT
Hi,

I am somewhat new to .net and c#. (What I learned in previous co has to
be unlearned).  I am doing something that seems simple but I think there
is a better way than how I learned to do it.  I am creating a fixed
length file with fixed length fields.  Each field either requires
leading zeros or added on spaces.  I learned to create a helper class
and for every field do this:
      public static string FormatFieldA(string str12)
       {
           if (str12 == null || str12.Trim() == "")
           //Default spaces required length = 12
           {
               return "            ";
           }
           else
           {
               Int32 l = 12 - str12.Trim().Length;
               if (l != 0)
                   for (int i = 1; i <= l; i++)
                   {
                       str12 = str12 + " ";
                   }
               return str12;

           }    
       }
I think there must be a better way like specifying in the Header or
Detail class for each field the field length and whether leadingzero is
t/f and then pass each field with those values to a simple format
function.  But I don't know if that is the best way and I don't know how
to assign those constants. Could someone help?  I want to do this well.
Jon Skeet [C# MVP] - 14 May 2008 19:48 GMT
> I am somewhat new to .net and c#. (What I learned in previous co has to
> be unlearned).  I am doing something that seems simple but I think there
> is a better way than how I learned to do it.  I am creating a fixed
> length file with fixed length fields.  Each field either requires
> leading zeros or added on spaces.  I learned to create a helper class
> and for every field do this:

<snip>

> I think there must be a better way like specifying in the Header or
> Detail class for each field the field length and whether leadingzero is
> t/f and then pass each field with those values to a simple format
> function.  But I don't know if that is the best way and I don't know how
> to assign those constants. Could someone help?  I want to do this well.

One way would be to have a custom attribute describing each field.
Fetch the attributes at execution time and call formatting methods
appropriately. Alternatively you could use an interface to specify (on
each class) what the field length should be.

One thing to point out though: String.PadRight and String.PadLeft are
your friends :)

Signature

Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet   
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

zacks@construction-imaging.com - 14 May 2008 21:47 GMT
> Hi,
>
[quoted text clipped - 28 lines]
> function.  But I don't know if that is the best way and I don't know how
> to assign those constants. Could someone help?  I want to do this well.

Look into the String.Format method. One thing, you will need to change
the datatype of the value to be padded to a numeric datatype.

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
Ignacio Machin ( .NET/ C# MVP ) - 14 May 2008 21:56 GMT
> Hi,
>
[quoted text clipped - 30 lines]
>
> *** Sent via Developersdexhttp://www.developersdex.com***

Take a look at PadLeft/PadRight.

In your case can be  myFieldValue.ToString().PadLeft( sizeOfField,
charToFillWith);
Rowan - 14 May 2008 22:50 GMT
Okay, the padding part makes sense.  I am a little lost when it comes to
the custom attributes.  I think I would need

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]  

Where does this go?  Does it get it's own class like public class
MyFieldsAttribute or because it is field level does it go in the class I
am using the attributes for? I need an attribute for requiredlength
(int) and leadingzeros (bool)...where does it go?  I am finding a lot to
read about it but not quite understanding where everything goes...help
is very greatly appreciated.  
Jon Skeet [C# MVP] - 15 May 2008 06:18 GMT
> Okay, the padding part makes sense.  I am a little lost when it comes to
> the custom attributes.  I think I would need
[quoted text clipped - 4 lines]
> MyFieldsAttribute or because it is field level does it go in the class I
> am using the attributes for?

The attribute type itself is just a class, even though it's applied to
fields. I wouldn't make it a nested class.

> I need an attribute for requiredlength
> (int) and leadingzeros (bool)...where does it go?  I am finding a lot to
> read about it but not quite understanding where everything goes...help
> is very greatly appreciated.  

See if http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx 
helps you.

Signature

Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet   
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.