Jump to content
Xtreme .Net Talk

Recommended Posts

  • Administrators
Posted

Good stuff, thanks PD! To review... don't use CType because it's VB6 legacy reach-back (as us net-admins would say :)). For my traditional C# casting I need to:

 

Dim x as ObjectType

x = DirectCast(y, ObjectType)

x.DoWhatever()

 

C# equiv (why I really like C#):

((ObjectType)x).DoWhatever();

 

And if I need numbers

Dim i as Integer = Integer.Parse(str)

-or-

Dim i as Interger = Convert.ToInt32(str)

 

Is one preferred over the other?

  • Leaders
Posted

Hmm... didn't know CType() is for Legacy Code. (Erases CType from the keywords list).

((ObjectType)x).DoWhatever();

That single line of code looks fairly hard to read (to me anyway). But the C# equivalent to the top line :

Dim x as ObjectType
x = DirectCast(y, ObjectType)
x.DoWhatever()

is:

ObjectType x;
x = (ObjectType) y;
x.DoWhatever();

And the VB equivalent to:

((ObjectType)x).DoWhatever();

is

DirectCast(x, ObjectType).DoWhatever()

Just as difficult to read as that C# line of code. :(

You should try to keep things simple instead of having it crumpled together on one line.

 

If you were converting a string to an integer, I would say to use Integer.Parse, but that's just my opinion.

Iceplug, USN

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

Posted

Code:

DirectCast(x, ObjectType).DoWhatever()

Just as difficult to read as that C# line of code.

I actually found that very simple to read, a lot cleaner than 3 lines of code; direct and to the point. Maybe it's because I'm use to do it that way, which from what I've seen in code examples is the preferred C# way, that's how I got into doing it. But IMHO (in my humble opinion) I think that's easier (one line crumpled together instead of three lines). Guess it's really semantics, but I'm curious as to others thoughts on what coding style they prefer and if anyone knows what M$ recommends. Thanks for the help though, I did learn something from it.

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