Link to home
Start Free TrialLog in
Avatar of joshkerr
joshkerr

asked on

Invalid casting when going from Object to HyperLink

I'm trying to cast from an Object to a HyperLink but I get the invalid cast error message.  Here is my code:

Object newObject = new Object();                  
HyperLink Control1 = (HyperLink)newObject;

Why doesn't that work?  Shouldn't I be able to go from an Object to a Control?  This makes no sense.

Avatar of praneetha
praneetha

Object newObject = new HyperLink()    
HyperLink Control1 = (HyperLink)newObject;

that will work
Avatar of joshkerr

ASKER

I'm asking a question about casting.  I am using the code above to try and demonstrate the problem in the simplest form.  I need to cast from an object to a hyperlink.
ASKER CERTIFIED SOLUTION
Avatar of blueforce
blueforce

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i don't think you can do that...

it's liks saying object without hyperlink properties/methods cannot be cast back to hyperlink...

say object newobj="1"
Hyperlink control1=(hyperlink)newobj

since it does not know if newobj has any characteristics of hyperlink..




Okay, this is what I'm trying to do.  Maybe I should ask this as a separate question.  

When I initialize a control, I use the FindControl function to return a handle on the control.  If the control doesn't exist in the aspx page, then the FindControl returns null.  I want the FindControl function to return an empty control instead.  So I wrote a skinnedWebControl class and overrode the FindControl function to this:

public override Control FindControl(string id) {
           Control myControl = base.FindControl(id);
            if(myControl == null){
                Control newControl = new Control();
                return (Control)newControl;
            }
            else                  
                return myControl;
}

Unfortunately, this doesn't work because I get that casting error.   How else could I accomplish this?
which line do u get the error
// setup the objects
HyperLink hPMlink = (HyperLink)this.FindControl("PrivateMessagesLink");   ***Error
hPMlink.NavigateUrl = globals.messagePage;
hPMlink.Text = language.oRM.GetString("profile_privatemessages");
                  
   Control myControl = base.FindControl(id);
            if(myControl == null){
                Control newControl = new Control();
                return (Control)newControl;
            }
            else              
                return myControl;
instead of Control can u do HyperLink...and see what it yields

This compiles... right?

You are getting a runtime exception.

This should work
            HyperLink h = HyperLink();
            object o = h;
            HyperLink h1 = (HyperLink)o;

This will not work because the object in question cannot be cast to a hyperlink...

Object newObject = new Object();              
HyperLink Control1 = (HyperLink)newObject;

To be able to cast between object they must have that kind of relationship...

Explicit Reference Conversions
The possible explicit reference conversions are

From object to any reference type.
From any class type B to any class type D, provided B is the base class of D
From any class type A to any interface type I, provided S is not sealed and do not implement I.
From any interface type I to any class type A, provided A is not sealed and implement I.
From any interface type I2 to any interface type I1, provided I2 is not derived from I1.
From System.Array to any array type.
From System.Delegate type to any delegate type.
From System.ICloneable to any array or delegate type.
Because of any reasons, if an explicit reference conversion fails, an InvalidCastException is thrown.



http://www.howtodothings.com/showarticle.asp?article=636

There's nothing wrong with this code:

public override Control FindControl(string id)
{
    Control myControl = base.FindControl(id);
    if(myControl == null)
    {
       Control newControl = new Control();
       return (Control)newControl;
     }
    else              
      return myControl;
}

I assume that you have the problem in the calling fn that expects a Control as a return value and then tries to cast that to  a HyperLink?

Without knowing what you're ultimately trying to accomplish, I can think of a couple of ways to handle this:

Ultimately, if you need a HyperLink, this:

       Control newControl = new Control();
       return (Control)newControl;

will never work.

If you're always looking for and always expecting a HyperLink, why not create a HyperLink to pass back in the FindControl method?  Otherwise, you could check the type of the control in the calling fn with myControl.GetType() to avoid casting from a Control to a HyperLink.