Click here to Skip to main content
15,030,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am migrating code from VB to VB.net, and in VB its possible to set strings like you can see below:

VB
Dim i As String * 3
i = "abcdefghijklmnop"
MsgBox (i)


And the message box will show the first three digits of the string.
Is there any option in vb.net for that functionality?

My code is like this:

VB
Private Type IPC

    a  As String * 1
    b  As String * 2
    c  As String * 35
    d  As String * 2
    e  As String * 26

End Type


I want to convert it to vb.net/C# please suggest how can I do

Thanks
Posted
Updated 10-May-12 5:21am
v3

I found this: How to declare a fixed length string in vb.net[^]

Use the VBFixedString attribute. See the MSDN info here[^]

<VBFixedString(3)>Dim i As String
   
v3
Comments
Sandeep Mewara 10-May-12 11:20am
   
Nice one! 5!

Didn't knew myself. Looks like it's only for VB.NET though!
Maciej Los 10-May-12 11:25am
   
Great answer, my 5!
Perhaps back off a step and consider how awful that method of creating a substring.

You'd be better off using something more concrete/transportable.

In pseudo-code:
stringType old = "abcdefghijklmnop"
stringType new = substring_Function(print-n-characters, starting-at-postion-0)
PopUpAMessage(new)


With the above, you can print the first three characters by using n=3. You also have the added flexibility (i.e., look to the future) of grabbing any contiguous group of characters.

It's a type compatible with most 'modern' languages and you thereby even use the now ubiquitous string container types for storing your result.



A word of advice: don't try to program C# (Or any other language) as though it were a different language (VB). It may forced to work, but it's sort of defeating the point of converting to the new language.
   
Not necessarily the answer you're looking for, but I don't see the actual reason to limit the string variable itself.

I would believe that in most cases you would just define the string and if needed you can always take a substring from it (and if necessary store it into another variable).

One way to achieve what you're asking (or quite close) is to create an array (or list) of characters, but again I would believe that this would cause extra trouble in the code.
   
As stated by others, there are no fixed length strings in VB.NET. See http://forums.devx.com/showthread.php?t=62758[^].

What you can do is use the Substring method of string:

public string Substring(int startIndex, int length);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900