Fixed length string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scotty

    Fixed length string

    Hi everyone,

    Is there an equivalent to vb6 Dim test as String * 40 ?

    I have been looking for a solution but no luck until now.

    Any help would be greatly appreciated.
  • Armin Zingler

    #2
    Re: Fixed length string

    "Scotty" <matthieu.sarth ou@googlemail.c omschrieb
    Hi everyone,
    >
    Is there an equivalent to vb6 Dim test as String * 40 ?
    >
    I have been looking for a solution but no luck until now.
    >
    Any help would be greatly appreciated.
    For which purpose? Trailing blanks in a String cost memory and have no
    advantage.

    I like the detailled chapters for VB6 users. Maybe you, too:


    Especially this sub topic:


    See also the MarshalAsAttrib ute.


    Armin

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Fixed length string

      "Scotty" <matthieu.sarth ou@googlemail.c omschrieb:
      Is there an equivalent to vb6 Dim test as String * 40 ?
      >
      I have been looking for a solution but no luck until now.
      There is no 1:1 equivalent available out of the box in VB.NET. I suggest to
      describe the scenario in more detail -- maybe there even an easier solution
      exists.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • =?Utf-8?B?U3VydHVyWg==?=

        #4
        RE: Fixed length string

        Dim Test(39) As Char

        --
        David Streeter
        Synchrotech Software
        Sydney Australia


        "Scotty" wrote:
        Hi everyone,
        >
        Is there an equivalent to vb6 Dim test as String * 40 ?
        >
        I have been looking for a solution but no luck until now.
        >
        Any help would be greatly appreciated.
        >

        Comment

        • =?Utf-8?B?U3VydHVyWg==?=

          #5
          Re: Fixed length string

          "Armin Zingler" wrote:
          Is there an equivalent to vb6 Dim test as String * 40 ?
          For which purpose? Trailing blanks in a String cost memory and have no
          advantage.
          I imagine the OP is reading or writing to a file that has fixed length
          fields, rather than delimiters.

          Unicode has screwed up the String data type as us old time BASIC programmers
          know it. It used to be that Strings were not only used as a sequence of ASCII
          characters, but also as a way of storing binary data.

          ASCII was 8-bit, but Unicode is (at least) 16-bit, so now instead of the
          one-size-fits-all String, we need to use:

          String for text
          Byte() for binary data
          Char() for fixed length strings

          However, the syntax is all different for Char() compared to the old
          "String*40" . You need to use Array.Copy() instead of MID()="value"

          So instead of

          Dim strFixed as String*40
          strFixed = "This is a fun test"
          Mid(strFixed,9, 4) = "not a"
          MsgBox(strFixed )

          we do this

          Dim chrFixed() As Char = Space(40).ToCha rArray
          Array.Copy("Thi s is a fun test".ToCharArr ay, 0, chrFixed, 0, 18)
          Array.Copy("not a".ToCharArr ay, 0, chrFixed, 8, 5)
          MsgBox(CStr(chr Fixed))

          --
          David Streeter
          Synchrotech Software
          Sydney Australia



          Comment

          Working...