VB6 VS VB.Net 2005 Syntax

tracky

New member
Joined
Jan 6, 2008
Messages
2
Programming Experience
Beginner
VB6 VS VB.Net 2005 Syntax
e.g.
VB6 => VB.Net
left(val1,2) => ???
Mid(Val1,2,2) => ???
Please help

i tried the code "left(textbox11.text,2)" in VB.Net...but it DUN Work >_<
:confused:

Please Help
TQ
 
VB.NET:
dim s as string = tb.Text.SubString(....)
 
Tq

TQ boss
let's say i want to capture the last 2 text from right?
e.g.
TESTING
so i wanted to capture the "NG"
in VB6 i will code like
Right("TESTING",2)
but it is NOT working in VB.NET
Please help

TQ Very Much!!
 
VB.NET:
Dim s As String = "testing"
s = s.Substring(s.Length - 2)
 
er... actually there's a much easier way. At the top of the code window, type "Import Microsoft.VisualBasic.Strings". Or, in your code you can reference the namespace directly with;
VB.NET:
Dim subText As String = Strings.Mid("this is another piece of text", 5, 6)
Same "Strings." prefix for Left(), Right(), Reverse() etc...
 
true but for some of the functions, the .net implementation (if any) doesn't quite have the same level of functionality. Even after using .net (and c# so I'm familiar with the .net way) for a few years, using the vb functions is easier. Besides, they wrote it into the language namespace for a reason too - and extended it. It wasn't entirely for legacy purposes I think. Still, it's just my opinion, don't take this too seriously :)
 
If you are serious about moving from vb6 to .Net I would suggest you stay away from the legacy stuff as there is no garentee it will still be supported in future versions.

Once you use it often enough, the .substring method becomes more intuitive to use. Also, it is simpler to migrate your code over to C# if you ever need to as the .substring method is pure .Net where as using Left, Right, & Mid is VB6.

It comes down to personal choice in the end but I recommend you stick with .substring
 
One other nasty surprise with .Net for VB6 programmers is that arrays in .NET are 0-based whereas, VB6 functions such as Left, Mid & Right are 1-based. That is, Mid(myString,1,1) starts from the first character in myString. myString.substring(1,1) actually returns the second character, as the first character is position 0 in the string array.

Also, myString.Length returns the number of characters in your string. If you want the last character is your string you need to subtract one from length.

For example:

dim myString as string = "hello"

' Character positions
' 0 1 2 3 4
' h e l l o


console.writeline(myString.length()) ' = 5 -- equivelent to Lent(myString)
console.writeline(myString.substring(0,2) ' = he -- equivelent to left(myString, 2)
console.writeline(myString.substring(1,1) ' = e -- equivelent to mid(mystring, 2,1)
console.writeline(myString.substring(myString.Length -1 - 2) ' lo -- equivelent to right(myString, 2)
 
Back
Top