[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

What is the syntax to make a string variable fixed?

Posted on 2004-10-21
10
Medium Priority
?
280 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
Comment
Question by:veayou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 12368737
dim str as string

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

Expert Comment

by:RonaldBiemans
ID: 12368907
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
ID: 12368908
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 750 total points
ID: 12368960
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
ID: 12368963
Oeps, sorry arthur_wood I should learn to refresh before I post
0
 

Author Comment

by:veayou
ID: 12369093

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
ID: 12369158
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
checoo earned 150 total points
ID: 12369718
0
 
LVL 13

Assisted Solution

by:Corey Scheich
Corey Scheich earned 600 total points
ID: 12369847
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:Corey Scheich
ID: 12369871
my bad it should be

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

Corey
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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