VB 2005 fixed length fields

Posted on 2006-05-26
Last Modified: 2012-06-27
I need to pass a string of fixed length fields to an AS400 from my Visual Studio 2005 / Visual Basic program.

Dim strFieldName as string * 18         receives an error for the "* 18" in 2005 VB.

How do I define a string field in VB 2005 with a fixed length value of 18?
Question by:RussBurns
    LVL 7

    Expert Comment

    .NET does NOT support Fixed length strings.

    From the help file

    "Note   The VBFixedStringAttribute is informational and cannot be used to convert a variable length string to a fixed string. The purpose of this attribute is to modify how strings in structures and non-local variables are used by methods or API calls that recognize the VBFixedStringAttribute. Keep in mind that this attribute does not change the actual length of the string itself."

    LVL 7

    Expert Comment

    LVL 44

    Accepted Solution

    Hi RussBurns,

    in addition you could use the stringbuilder class but even when declaring the capacity it will expand when you insert larger strings then 18 but it will be fixed length so if you need 18 spaces

          Dim sb As New StringBuilder(8)
          sb.Insert(0, "this") ' < the sb will be 8 in length
          sb.Insert(0, "this is a test") ' the sb will be 14 in length

    you can also pad a string like
          s.PadRight(18," ")

    but again this string will be 24 in length if you put in a string larger then 18
    share what you know, learn what you don't

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 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

    19 Experts available now in Live!

    Get 1:1 Help Now