Results 1 to 16 of 16

Thread: Ctype in VB.Net

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32

    Ctype in VB.Net

    Is there a function in the system.convert namespace that equals the ctype function ?
    For example the Cdbl function is equal to the system.convert.todouble function.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    System.Convert.ChangeType(object, typetochangeto)
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32
    Give me an example.
    I have this piece of code:

    Dim objControl As Control
    Dim objPicBox As PictureBox
    objPicBox = CType(objControl, PictureBox)

    Show me how to do ....

  4. #4
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    You can still use ctype

    you can still use ctype.

    Dim objControl As Control
    Dim objPicBox As PictureBox
    objPicBox = CType(objControl, PictureBox)

    i believe that will work!?!

    But i could be wrong, i do use it in my current application though

  5. #5
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    What's wrong with just using CType?

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    you use it the EXACT same way as CType


    one reason not to use CType is that it is a part of the Microsoft.VisualBasic namespace that was added for backwards compatability. I myself try to avoid it becuase it is 1 less reference I need since that dll provides me nothing I cant do with the standard .NET classes.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32
    Hey Cander,

    did you ever try this ?

    Check it out and you will see that it can't be used in the same way.
    system.convert.changetype(objControl, PictureBox) does not work !
    So try it and tell me how to do ... ( if you can )

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    hmm no I didnt try it, but according to the win class viewer, that is all you should have to do. Ill have to try it tomorrow when I get back to work.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ohh.try this

    System.Convert.ChangeType(objcontrol, TypeOf(PictureBox))


    maybe that will work..I dont have .NET where I am now, so I cant test.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  10. #10
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    Originally posted by Cander
    one reason not to use CType is that it is a part of the Microsoft.VisualBasic namespace that was added for backwards compatability.
    That's not true. There's absolutely nothing wrong with using that namespace. The one to avoid that's there for backwards compatibility is the Microsoft.VisualBasic.Compatibility namespace.

    The Microsoft.VisualBasic namespace is an integral part of the VB.NET language. Check the documentation. Nowhere does it say that it's only for backwards compatibility. There's also a Microsoft.CSharp namespace. Is that one supposed to be for backwards compatibility, too?

  11. #11
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    Also, CType isn't even found in Microsoft.VisualBasic to begin with. This quote is straight out of Microsoft's documentation:
    CType is compiled inline, meaning the conversion code is part of the code that evaluates the expression. Execution is faster because there is no call to a procedure to accomplish the conversion.
    This means that all these substitutes you people are cooking up are actually slower.

    CType is just VB.NET's implementation of casting. There's no reason to avoid it, just like there's nothing wrong with this line in C#:
    int i=(int)longVariable;
    Last edited by Tygur; Aug 6th, 2002 at 02:29 PM.

  12. #12

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32
    TypeOf(PictureBox) does not work

  13. #13
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by Tygur
    Also, CType isn't even found in Microsoft.VisualBasic to begin with. This quote is straight out of Microsoft's documentation:

    This means that all these substitutes you people are cooking up are actually slower.

    CType is just VB.NET's implementation of casting. There's no reason to avoid it, just like there's nothing wrong with this line in C#:
    int i=(int)longVariable;
    Ok Im a retard!

    I had assumed it was part of the backwards compat namespace....
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    is there any option to put off the visual basic namespace so i just can't use its functions? it would be a good way to me get used to dont use it anymore

  15. #15
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I found out something interesting today. The compiler already refs that namespace built into it, so it raelly doesnt matter.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16
    Member
    Join Date
    Mar 2002
    Posts
    40
    Originally posted by PT Exorcist
    is there any option to put off the visual basic namespace so i just can't use its functions? it would be a good way to me get used to dont use it anymore
    Yes! Just Right-Click on your project name in the Solution Explorer, then select Properties - Imports. In the “Project Imports:” list, delete Microsoft.VisualBasic. I strongly recommend adding the Microsoft namespace so that you don't have to include "Microsoft." in front of everything from the VisualBasic namespace. I did this early on in the program that I'm working on and I've NEVER regretted it. No more accidental MsgBox's for this guy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width