Results 1 to 9 of 9

Thread: casting difference?

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    casting difference?

    whats the differece between:

    ((TextBox)(e.TabPage.Container)).Text
    (e.TabPage.Control as RichTextBox).Text
    \m/\m/

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    The first cast is using an explicit cast and will throw an exception if an invalid cast is attempted. Using the 'as' keyword, it will return a null reference if an invalid cast is attempted, otherwise, will return the object reference.

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm so which one should i use?!
    \m/\m/

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    It depends on the situation in which you plan on casting...
    If you do not know ahead of time the exact type(s) of objects your are dealing with, it would be safe to use the 'as' construct.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Or not, because you then might get a NullPointerException instead of a InvalidCastException, which tells you less about the source of the problem.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Or not, because you then might get a NullPointerException instead of a InvalidCastException, which tells you less about the source of the problem.
    I assumed from my explanation that he would check after the cast for a null reference..

    Code:
        BaseClass b = SomeClass as BaseClass;
        if(b != null)
            Console.WriteLine("Not Null...");

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yeah, of course, but that doesn't necessarily make it easier to use.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    It's a cleaner solution than relying on a try...catch block....
    That's why they included it into the language.

  9. #9
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Also note that you may use "as" only for reference types like strings, objects, etc. not value types such as int, Color, etc...
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

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