Results 1 to 13 of 13

Thread: StringBuilders

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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.

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Then why bother with the Insert and just use the .Append method?

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  8. #8
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    What

    Have you ever heard of .PadRight(18, " ")
    Basically, you would do a .ToString on your field and then do the .PadRight

    VB Code:
    1. 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!

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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:
    1. 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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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
  •  



Click Here to Expand Forum to Full Width