Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Well folks it's looking like a strong possiblity that I'm finally leaving the network administration world and getting into programming full time. It's going to be VB based web programing...as some of you know who have been here a while, I'm an advid C# guy...jumped ship from VB6.

 

A few things:

I've noticed several posts from people who use CType, CString, CInt, etc... this is bad coding right because it's 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? I'm studying hard, but other people's 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)...don't think there should be.

 

Also I'm 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 I'm 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 I'm excited and terrified. I've longed to program full time, but I was always hoping for C#, but programing is programing, and .NET is great because it's 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 :)

  • Leaders
Posted

Welcome. :D

A few things:

I've noticed several posts from people who use CType, CString, CInt, etc... this is bad coding right because it's 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 object's .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 that's 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:

if(i<100)
{
i++;
}

 

is to

If i < 10 Then
i += 1
End If

as

if(i<10)
i++;

is to

If i < 10 Then i += 1

Although, I usually avoid shortcuts.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

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 it's 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?

  • Administrators
Posted

When converting from strings I often tend to use the various .Parse methods rather than the Convert. methods.

i.e. Instead of

Dim s As String = "43"
Dim i As Integer
i = Convert.ToInt32(s)

I would tend to use

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)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

Break is considered an exception in a switch statement. Otherwise you don't really need to use them.

int Total = 0;
for(int i=0;i<10;i++)
{
if(i % 5==0)
continue;   //Really, what you are saying is that you don't want the following lines to execute if i % 5 is 0.
Total += i;
}

Equivalent without continue in C#:

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

And the equivalent in VB:

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.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...