CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2001
    Posts
    872

    casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    hello

    I need to cast KeyValuePair<X,Y> into KeyValuePair<Object, Object> ...

    Trying to write simpler code... how can I reduce the number of the else-if blocks below?
    Code:
    if (o is DictionaryEntry)
    {
    	DictionaryEntry oEntry = (DictionaryEntry)o;
    	object oKey = oEntry.Key;
    	object oValue = oEntry.Value;
    	
    	// In this case lstAnotherCollection is a Hashtable
    	lstAnotherCollection.Add(oKey, oValue);
    	... more ...
    }
    else if (o is KeyValuePair<string, System.Drawing.Bitmap>)
    {
    	// This would give error ...
    	// KeyValuePair<object, object> oEntry = (KeyValuePair<object, object>)o;
    	
    	// To resolve this, I resorted to else-elseif blocks (That's obviously undersirable)
    	KeyValuePair<string, System.Drawing.Bitmap> oEntry = (KeyValuePair<string, System.Drawing.Bitmap>)o;
    	string oKey = oEntry.Key;
    	System.Drawing.Bitmap oValue = oEntry.Value;
    	
    	// lstAnotherCollection = Dictionary<Object, Object>
    	lstAnotherCollection.Add(oKey, oValue);
    	... more ...
    }
    ... many more else-if blocks ...

    I suppose I cannot cast KeyValuePair<X,Y> into KeyValuePair<Object, Object> ... because it defeats purpose of Generics in the first place?

    Thanks
    Last edited by THY02K; June 21st, 2009 at 05:55 AM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    It's hard to give you advice without any context. If I knew what your intention was I could help more, but you are right in thinking that writing code like that is a bad idea. Why would you expect so many different types of objects there?

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    Usually when you find yourself having to do this kind of thing, it indicates a poor design.

  4. #4
    Join Date
    Feb 2001
    Posts
    872

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    yes, this is why I stated I want to eliminate this else if blocks and was looking for a solution - this is code generator code examining obj properties wiring interceptors/fabricating proxy

    suggestion?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    Have the KeyValuePair hold onto a base class. See if you can use polymorphism to eliminate the if/else statements.

  6. #6
    Join Date
    Feb 2001
    Posts
    872

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    yes - i tried casting KeyValuePair<X,Y> into KeyValuePair<Object, Object> (X and Y are Reference Type), no luck... I think I need to think of something...

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    I'm not talking about down to the object level, I'm talking more on the lines of

    Code:
    KeyValuePair< string, MyBaseClass >
    If you have too many variations for this, then you might think about changing the way you are receiving the data.

    A 'catch-all' kind of approach works okay for very simple things, but doesn't work for slightly more complex things (as you have found out).

    We don't have much context for what you are doing, but you mentioned code generation of proxies. Rather than building a proxy that does everything, consider making multiple proxies, specifically one proxy for group of similar types.

  8. #8
    Join Date
    Feb 2001
    Posts
    872

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    Thanks... there's got to be a way... probably in arg of called method as supposed to else-if blocks as you've suggested...

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    No, usually when you find yourself checking the type of an object as a condition it means that you could replace that check with polymorphic behavior. That is what Arjay was saying. For example, (this is simplistic, I know), you may have a base class for Planes:

    Code:
    abstract class Plane
    {
        abstract void Fly( );
    }
    All types of planes would inherit from this base class. The Fly( ) method is abstract because, although all planes fly, they do not all do so in the same manner. So, a Cessna would implement the Fly method differently than a Jumbo Jet would, but you would treat them all as instances of the Plane class and simply call the Fly( ) method on each. Each inherited type knows how to fly in their own way, but as a client of the class you don't need to know what that is.

    Code:
     
    Plane cessna = new Cessna( );
    Plane jumbo = new JumboJet( );
    
    censa.Fly( );  // calls Cessna.Fly( )
    jumbo.Fly( );  // calls JumboJet.Fly( )
    I know that this is overly simplistic, but you can see how this can be very powerful. Instead of checking the type of your object, each type will be encapsulated into a class and they all will share a common interface. Again, if we knew more about your situation we could tell you whether or not this was the right approach.

  10. #10
    Join Date
    Aug 2009
    Posts
    1

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    Would this be an solution for your problem:

    lstAnotherCollection.Add(new KeyValuePair<object, object>(o.Key, o.Value));

    or

    KeyValuePair<object, object> keyValuePair = new KeyValuePair<object, object>(o.Key, o.Value);

    ?

    I dont see any reason why it should be casted, the above should work just fine.

  11. #11
    Join Date
    May 2007
    Posts
    1,546

    Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?

    .NET 4.0 allows this kind of casting via contra-variance and co-variance.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

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