VB Gotcha's and General Advice needed

bri189a

Well-known member
Joined
Sep 11, 2003
Messages
1,004
Location
VA
Well folks its looking like a strong possiblity that Im finally leaving the network administration world and getting into programming full time. Its going to be VB based web programing...as some of you know who have been here a while, Im an advid C# guy...jumped ship from VB6.

A few things:
Ive noticed several posts from people who use CType, CString, CInt, etc... this is bad coding right because its VB6, I should be using Convert.ToInt32, Convert.ToString (or object.ToString()), Convert.ToWhatever, right? How about boxing (might have my terms wrong):

int i = (int) someObject;

Is that going to be:
Dim i as Integer = (Integer) someObject

or do I have that wrong, how would you do it? What are some other things I need to watch out (that might surface from my old VB6 days that are degraded in VB.NET) such as On Error GoTo I also know is a no-no, and other syntax differances / short-cuts that may not be well publicized? Im studying hard, but other peoples experiance can help a lot, at least I know .NET at an proficient level in C#, and know VB6 basic syntax. Are there any ASP.NET differances I should be aware of (like Page Attributes)...dont think there should be.

Also Im going from an environment of be the sole programmer as a second hat doing a lot of adhoc tools and things at my own pace and my own time, or working from home on side-work, to a full time contract. What kind of enviroment can I expect, in otherwords I am going to be expected to come up with 6000 lines of code a day? Do they understand that some things need to be researched? Will they cut me some slack since Im switching languages and though experianced, professionally a newbie? Do they usually have the DAL (DAC) already set up so all you have to do is bring it in your code and start going?

To be honest Im excited and terrified. Ive longed to program full time, but I was always hoping for C#, but programing is programing, and .NET is great because its just a matter of syntax differances and a few key items (operator overloading, continue/break keywords). So initially there will be a learning curve while I break my C# syntax habbits and remember everything has an End to it instead of opening and closing braces... speaking of:

C# you can:
if(i<10)
i++;
instead of:
if(i<100)
{
i++;
}

can I short-cut the below to save space as I can C#?
If i < 10 Then
i += 1
End If

Thanks everyone for help / comments :)
 
Welcome. :D
A few things:
Ive noticed several posts from people who use CType, CString, CInt, etc... this is bad coding right because its VB6, I should be using Convert.ToInt32, Convert.ToString (or object.ToString()), Convert.ToWhatever, right? How about boxing (might have my terms wrong):
Well, kind of yes, kind of no.
CType is .NET, CInt is legacy VB, CString is not a function :-\ unless you were talking about CStr(), which is also legacy VB.
You would use Convert.ToInt32() and Convert.ToString() or just use the objects .ToString method.
How about boxing (might have my terms wrong):
Throw two quick right punches and then a left hook. ;)
Dim i as Integer = (Integer) someObject
Ah... that would be casting... and yes thats incorrect.
Dim i As Integer = CType(someobject, Integer)
or
Dim i As Integer = DirectCast(someobject, Integer)
I generally use CType for numeric values, like enumerations, and DirectCast for everything else.

What are some other things I need to watch out (that might surface from my old VB6 days that are degraded in VB.NET) such as On Error GoTo I also know is a no-no, and other syntax differances / short-cuts that may not be well publicized?
Of course On Error Goto can be replaced with a Try Catch block.
You should also avoid those other legacy functions, such as Trim(), Split(), Replace(), Fileopen(ugh), and use the appropriate .NET equivalent.
Since you have used C#, you already have a decent advantage in classes and OOPS (Object oriented programming and stuff) ;)

Also, VB has the With block, so instead of
MyHouse.Border = Borders.Brick
MyHouse.Address = "12345 Street St."
MyHouse.Area = 200.0F

You can do:
With MyHouse
.Border = Borders.Brick
.Address = "12345 Street St."
.Area = 200.0F
End With
(operator overloading, continue/break keywords).
You can do operator overloading in 2005, but I doubt that you are using that :p just FYI. That would just have to be done in a function call.
Continue and break keywords are considered bad programming in C languages, like the Goto and Exit ... statements in VB.
C# you can:
if(i<10)
i++;
instead of:
if(i<100)
{
i++;
}

can I short-cut the below to save space as I can C#?
If i < 10 Then
i += 1
End If
Absolutely:
Code:
if(i<100)
{
i++;
}

is to
Code:
If i < 10 Then
i += 1
End If
as
Code:
if(i<10)
i++;
is to
Code:
If i < 10 Then i += 1
Although, I usually avoid shortcuts.
 
Thanks, that helps a lot, a few follow-up questions.... continue and break are considered bad practice in C languages... but they are used so often (bad example...but):

int Total = 0;
for(int i=0;i<10;i++)
{
if(i % 5==0)
continue;
Total += i;
}

yes, horrible, horrible, example, but there are case where you really use that and its great... so for learning purposes, why do C based consider continue and break horrible...especially when you have to have break in switch statements in order for them to compile?
 
When converting from strings I often tend to use the various .Parse methods rather than the Convert. methods.
i.e. Instead of
Code:
Dim s As String = "43"
Dim i As Integer
i = Convert.ToInt32(s)
I would tend to use
Code:
Dim s As String = "43"
Dim i As Integer
i = Integer.Parse(s)
or depending on my needs one of the overloaded versions like
i = Integer.Parse(s, Globalization.NumberStyles.Currency)
 
Break is considered an exception in a switch statement. Otherwise you dont really need to use them.
Code:
int Total = 0;
for(int i=0;i<10;i++)
{
if(i % 5==0)
continue;   //Really, what you are saying is that you dont want the following lines to execute if i % 5 is 0.
Total += i;
}
Equivalent without continue in C#:
Code:
int Total = 0;
for(int i=0;i<10;i++)
{
  if(i % 5 > 0) {
    Total += i;
  }
}
And the equivalent in VB:
Code:
Dim Total As Integer = 0
For I As Integer = 0 To 9   Note that you can only do As Integer in VB.NET 2003 or later.
  If I Mod 5 > 0 Then
    Total += I
  End If
Next
Solving a break can be done by setting the appropriate exit condition yourself.
In a For Loop, you can change the loop variable and wrap the remainder of code in an If block. In a Do Loop, you use AndAlso to add another condition to the check.
 
I love it when you senior guys give out good coding practices.... thanks! :) PS...see my Q about VB Interface implementations, some senior coding practices there would be much appreciated.
 
Back
Top