[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

What is the syntax to make a string variable fixed?


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
veayou
Asked:
veayou
3 Solutions
 
hongjunCommented:
dim str as string

str = "This is for testing."
str = str & "More added"
0
 
RonaldBiemansCommented:
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
 
Arthur_WoodCommented:
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
RonaldBiemansCommented:
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
 
RonaldBiemansCommented:
Oeps, sorry arthur_wood I should learn to refresh before I post
0
 
veayouAuthor Commented:

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
 
RonaldBiemansCommented:
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
 
Corey ScheichDeveloperCommented:
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
 
Corey ScheichDeveloperCommented:
my bad it should be

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

Corey
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now