Solved

What is the syntax to make a string variable fixed?

Posted on 2004-10-21
270 Views
Last Modified: 2010-04-23

Please help!

The String data type in vb.net can store up to 2 GBs and we can just create a variable like this:
   
     dim str as string

But now I would like to make this str to be fixed. I got the syntax like this:

    <vbFixedstring(10)> dim str as string
    str = "This is for testing."
    msgbox (str)

By writing like this I can still assign value more than 10 characters and can call it back as well without losing any character.

What is the problem? and do you have any syntax?

Thank you.
BM
0
Question by:veayou
    10 Comments
     
    LVL 33

    Expert Comment

    by:hongjun
    dim str as string

    str = "This is for testing."
    str = str & "More added"
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    maybe look here

    http://www.mentalis.org/vbtutor/vbdotnet.shtml

    if you need the fixed lenght string as a parameter for an Api call, you could use this

    Dim sBuf As String = Space(128)
    0
     
    LVL 44

    Expert Comment

    by:Arthur_Wood
    this is from the Help system overfied on vbFIxedString:

    "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."

    The last sentence seems to be quite clear that this attribute DOES NOT do what you seem to think it does.

    .NET does NOT support Fixed length strings.

    AW
    0
     
    LVL 25

    Accepted Solution

    by:
    this is what the help says about it

    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.
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    Oeps, sorry arthur_wood I should learn to refresh before I post
    0
     

    Author Comment

    by:veayou

    I can see now. <vbFixedString> can not be used.
    I have tried the code below but it's not work again:
     
          dim str as string = space(5)
          str = "1234567 56 33 4 5 6 4 2 3 3 4 45 "
          Msgbox (str)

          Result:
          1234567 56 33 4 5 6 4 2 3 3 4 45

    Maybe in VB.Net we can not make a string variable fixed....????

    BM.
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    No you can't make a fixed lenght string it is not supported by .net, the example I gave is for if you need to simulate a fixed lenght string for use in an API. You will still be able to change the lenght afterwards
    0
     
    LVL 9

    Assisted Solution

    by:checoo
    0
     
    LVL 13

    Assisted Solution

    by:Corey2
    Why not do something like this

    Class FixedString

    Private StrippedString as string
    Private OrigString as string
    Private MaxLength as long

    Private Sub Class_Initialize()
         Maxlength = 0    
    End Sub

    Private Sub UpdateString ()
      if MaxLength > 0 then
        StrippedString = OrigString.substring(0,MaxLength)
      else
        StrippedString = OrigString
     end if
    end sub

    Public Sub SetMaxLength (byref Length as long)
       MaxLength = Length
       UpdateString
    end sub

    Public Sub SetString (byref S as String)
       OrigString = S
       UpdateString
    end sub


    Public Property Get GetMaxLength () as long
      GetMaxLength = MaxLength
    end Property

    Public Property Get GetOrigString () as string
      GetOrigString  = OrigString
    end Property

    Public Property Get GetString () as string
      GetStrippedString = StrippedString
    end Property

    End Class

    Sub TestString ()
        Dim Str as New FixedString
       with str
             .SetMaxLength = 8
             .SetString = "This Is Longer than eight characters"
       end with

    msgbox (str.getstring)
    end sub

    Regards,
    Corey
    0
     
    LVL 13

    Expert Comment

    by:Corey2
    my bad it should be

       with str
             .SetMaxLength  8
             .SetString "This Is Longer than eight characters"
       end with

    Corey
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    875 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

    12 Experts available now in Live!

    Get 1:1 Help Now