CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: type casting

  1. #1
    Join Date
    May 2000
    Location
    Somewhere very near by...
    Posts
    2

    type casting

    Hi. I was wondering if there is a way to make the following line of code work.

    if( ( CoefficientTerm) terms.firstElement().isZero() ) {...do something..}

    In the program, term.firstElement() returns an Object that is typecast to (CoefficientTerm). Then, isZero returns a boolean. As it is the expression gives me a compile error saying that I can't convert a CoefficientTerm to a boolean. I could fix the problem by breaking it into two lines like:

    newCoef = (CoefficientTerm) terms.firstElement();
    if ( newCoef.isZero() ){ ... }

    However, there must be a better way. So is there anyway to typecast just the first part of an expression, but not the rest?

    Thanks a lot!!



  2. #2
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: type casting

    By default typecasting works for last returned value...

    If you try follwing, it should work...

    <javacode>
    if( ((CoefficientTerm)terms.firstElement()).isZero() ) {...do something..}
    </javacode>

    Basically you put parathensis for explicitly specifying which returned value you want to type-cast.

    - UnicMan
    http://members.tripod.com/unicman

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

Featured