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

Threaded View

  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.

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