Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    Good stuff on DirectCast

    Paul Vick posts up some great stuff for anyone who has ever been confused about DirectCast and when to use it compared to CType.

    Personally, I tend to go the opposite direction that Paul recommends and use DirectCast all the time, unless I know that the type might possibly be a different type and actually need converted, that way I can never blame it on casting for any performance issues that may arise, although very unlikely.  As apposed to Paul's suggestion of using CType more often.  There really shouldn't be much of a different, just a "coding preference".

    Posted: Jul 15 2003, 11:55 PM by HumanCompiler | with 1 comment(s)
    Filed under:

    Comments

    Mel Grubb II said:

    Yup, me too.

    I use DirectCast all the time, especially when a parameter had BETTER be of a certain type. For example. I have a treeview in which the Tag property of every node holds an ArrayList of information about that node. In my code, I DirectCast the node.Tag property to an ArrayList because if it's NOT an ArrayList, then I've got serious problems. I WANT the program to crash if there is not an ArrayList in that Tag because it's a fundamental assumption I've made about the way my TreeView is going to work.
    # July 16, 2003 2:31 PM