bri189a Posted September 28, 2004 Posted September 28, 2004 Which is better and why if they both do the same thing? Quote
Administrators PlausiblyDamp Posted September 28, 2004 Administrators Posted September 28, 2004 http://www.xtremedotnettalk.com/showthread.php?t=80277&highlight=directcast+ctype http://www.xtremedotnettalk.com/showthread.php?s=&threadid=73095&highlight=ctype+directcast http://www.xtremedotnettalk.com/showthread.php?s=&threadid=70566&highlight=ctype+directcast http://www.xtremedotnettalk.com/showthread.php?s=&threadid=80277&highlight=ctype+directcast Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bri189a Posted September 28, 2004 Author Posted September 28, 2004 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? Quote
Leaders Iceplug Posted September 28, 2004 Leaders Posted September 28, 2004 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. Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
bri189a Posted September 29, 2004 Author Posted September 29, 2004 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. Quote
*Gurus* divil Posted September 29, 2004 *Gurus* Posted September 29, 2004 A more direct c# equivalent would be (x as ObjectType).DoWhatever(); Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
bri189a Posted September 29, 2004 Author Posted September 29, 2004 Good to know! Thanks divil! :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.