|
-
Jul 7th, 2004, 08:51 AM
#1
Thread Starter
Frenzied Member
StringBuilders
trying to use a StringBuilder and it is not working out. I need to create a string that is 418 characters long for writing a flat file for input into another application. Here is the start of my loop, but as soon as I get to the second string builder insert, I get an arguement out of range exception.
...
For Each drCurrentData In dtCISToPMXVendorData.Rows
strbuildOutput = New System.Text.StringBuilder(418, 418)
strbuildOutput.Insert(0, drCurrentData.Item("ActionCode"))
strbuildOutput.Insert(15, drCurrentData.Item("CG_VEND_NBR"))
strbuildOutput.Insert(25, drCurrentData.Item("VEND_NAME"))
...
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 7th, 2004, 09:34 AM
#2
Hyperactive Member
I used your code as an example to build my own version and tested it with plain text inserts. It works fine. I am guessing the problem lies not with the stringbuilder, but your dataset.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 11:46 AM
#3
Thread Starter
Frenzied Member
Are you leaving any spaces? The problem I seem to be having is that if I try to write character 15, there has to be something, even white space " ", for it to work in all 14 characters before it. So if I want to write
0123456789
A Data
I have to write A, then cushion any space I don't use in between with spaces and then the next Insert works fine.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 7th, 2004, 11:51 AM
#4
Hyperactive Member
Good catch, I ran another test and encountered the same error, I would suggest padding the end of your strings with spaces, then you will always have something in each position in the string builder
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 12:38 PM
#5
Then why bother with the Insert and just use the .Append method?
TG
-
Jul 7th, 2004, 12:41 PM
#6
Thread Starter
Frenzied Member
Yeah, I can do that, but it is just ugly to check string lengths and then have to pad those white spaces for every less character for every field. I suppose I could prepad the whole thing, remove the max capacity as Insert adds characters, not overwrites, and then RTrim it at the end to remove anytrailling white space, but that seems like such an awful hack. Anybody have any better ideas?
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 7th, 2004, 12:43 PM
#7
Thread Starter
Frenzied Member
Then why bother with the Insert and just use the .Append method?
I'm writing a Flat File that some archaic software needs in a really particular format, so I have to have each data start at a certain character, so I'd still have to check lengths and append white space.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 7th, 2004, 12:44 PM
#8
Hyperactive Member
What 
Have you ever heard of .PadRight(18, " ")
Basically, you would do a .ToString on your field and then do the .PadRight
VB Code:
strbuildOutput.Insert(0, drCurrentData.Item("ActionCode").ToString.PadRight(18, " "))
I did not have a chance to test this on my machine because I don't have a test project that is accessing data, but I know these methods work
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 12:50 PM
#9
Originally posted by CyberHawke
What 
Have you ever heard of .PadRight(18, " ")
Basically, you would do a .ToString on your field and then do the .PadRight
VB Code:
strbuildOutput.Insert(0, drCurrentData.Item("ActionCode").ToString.PadRight(18, " "))
I did not have a chance to test this on my machine because I don't have a test project that is accessing data, but I know these methods work
The key in this case is comming up with that 18.......
I'm not sure how to do it in .NET, but in VB6 I've post pended spaces equal to the total length I needed, then took the Left X characters, where X is the max total allowable len of the string.
TG
-
Jul 7th, 2004, 12:50 PM
#10
Thread Starter
Frenzied Member
Actually I've never used PadRight. Can't say I've ever had a need to add spaces to the end of a string before. Thanks.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 7th, 2004, 12:51 PM
#11
Originally posted by SeanGrebey
I'd still have to check lengths and append white space.
Which is what you have to do now..... I was simply questioning that since you have to pad each element of the string, there is no longer an advantage to the Insert methood and that it would be more efficient to do an Append.
TG
-
Jul 7th, 2004, 12:53 PM
#12
Hyperactive Member
I would agree with you TG, I tend to lean in the direction of .Append myself, but perhaps whatever Sean is writing to expects a fixed length string which is how he came up with his defined lengths when he declares his StringBuilder object.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 01:10 PM
#13
Thread Starter
Frenzied Member
Yeah, it is a fixed length string, so the pad right would work. Haha, I think I've wasted too much time on this stupid string builder. Should have just used a good old char array to begin with....
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|