Click here to Skip to main content
15,885,782 members
Home / Discussions / C#
   

C#

 
AnswerRe: Change Combobox Look in WPF Pin
Abhinav S25-Aug-12 18:31
Abhinav S25-Aug-12 18:31 
QuestionIndexers Pin
ripples24-Aug-12 23:59
ripples24-Aug-12 23:59 
AnswerRe: Indexers Pin
Wes Aday25-Aug-12 0:12
professionalWes Aday25-Aug-12 0:12 
AnswerRe: Indexers Pin
DaveyM6925-Aug-12 0:37
professionalDaveyM6925-Aug-12 0:37 
AnswerRe: Indexers Pin
PIEBALDconsult25-Aug-12 4:40
mvePIEBALDconsult25-Aug-12 4:40 
AnswerRe: Indexers Pin
Richard MacCutchan25-Aug-12 7:07
mveRichard MacCutchan25-Aug-12 7:07 
GeneralRe: Indexers Pin
ripples25-Aug-12 8:52
ripples25-Aug-12 8:52 
Questioncan this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff24-Aug-12 20:31
professionalBillWoodruff24-Aug-12 20:31 
Note: this "experiment" was actually touched-off by Bob Janova's comment [^] on the recent thread on Tuples, "Tuples in Functions" [^], here, on this forum.

I'm not clear exactly why, but, that got me interested in what it would take to build a generic List of KeyValuePairs, where the 'Key would be a 'Type, and the 'Value an instance of that 'Type, and the 'Type for each KeyValuePair would be inferred from the 'Value.

The idea was that to to call a function that added items to the List<KeyValuePair> like this, passing in only the actual 'Value for each new KeyValuePair item:
C#
AddToKVPList("tweet");

AddToKVPList(1.567893);

AddToKVPList(new TextBox()); // WinForms dependent ?
I struggled to find a way to do this without using any "higher" .NET facilities, but could not achieve what I wanted: my final solution was
private List<KeyValuePair<Type, dynamic>> _listOkvp = new List<KeyValuePair<Type, dynamic>>();

private void AddToKVPList(object oIn)
{
    workingType = oIn.GetType();

    KeyValuePair<Type, dynamic> newKVP = new KeyValuePair<Type, dynamic>(workingType, oIn);
    
    _listOkvp.Add(newKVP);
}
If we execute the above code, and examine what _listOkvp is after the three calls to AddToKVPList in a 'Command' window in Visual Studio:
C#
> ? _listOkvp
Count = 3
    [0]: {[System.String, tweet]}
    [1]: {[System.Double, 1.567893]}
    [2]: {[System.Windows.Forms.TextBox, System.Windows.Forms.TextBox, Text: ]}
Well, yes, that's what I was trying to get to: and, if you add this to iterate and print to the Console everything in the KeyValuePairList:
C#
foreach(var kvp in _listOkvp)
 {
     Console.WriteLine(kvp.Value.GetType());
 }
You do get:
VB
System.String
System.Double
System.Windows.Forms.TextBox
So what's wrong ? Smile | :) Two things:

1. I believe I could have achieved this without using 'Dynamic, but I could not find a way to do that.

2. I cannot understand how I did transform the value of the (edit: 'Key)'Value, which was passed in as an 'object' parameter to the method 'AddToKVPList, to an actual instance of the 'Type of the passed in 'Value.

Which is worse: code you think should work, but does not, and you can't understand what's wrong, or: code that works, but you can't understand why ? Smile | :)

Appreciate your response: could this be done without 'dynamic ?

thanks, Bill
"One of the few good things about modern times: If you die horribly on television, you will not have died in vain. You will have entertained us." Kurt Vonnegut


modified 25-Aug-12 17:20pm.

AnswerRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
DaveyM6925-Aug-12 0:13
professionalDaveyM6925-Aug-12 0:13 
GeneralRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff25-Aug-12 11:25
professionalBillWoodruff25-Aug-12 11:25 
AnswerRe: could this be done without dynamic Pin
Martijn Kok25-Aug-12 0:59
Martijn Kok25-Aug-12 0:59 
GeneralRe: could this be done without dynamic Pin
BillWoodruff25-Aug-12 11:33
professionalBillWoodruff25-Aug-12 11:33 
Questionasp.net Pin
Abubakar Shaikh .24-Aug-12 20:05
Abubakar Shaikh .24-Aug-12 20:05 
AnswerRe: asp.net Pin
Richard MacCutchan24-Aug-12 21:57
mveRichard MacCutchan24-Aug-12 21:57 
AnswerRe: asp.net Pin
Abhinav S25-Aug-12 2:38
Abhinav S25-Aug-12 2:38 
AnswerRe: asp.net Pin
Dave Kreskowiak25-Aug-12 4:23
mveDave Kreskowiak25-Aug-12 4:23 
QuestionProblem in datagridviewDeleteColumn Pin
Member 925960624-Aug-12 19:44
Member 925960624-Aug-12 19:44 
QuestionHow To Binding A List Of Frame? Pin
Bardiamarzbani24-Aug-12 9:42
Bardiamarzbani24-Aug-12 9:42 
AnswerRe: How To Binding A List Of Frame? Pin
Wes Aday24-Aug-12 10:46
professionalWes Aday24-Aug-12 10:46 
AnswerRe: How To Binding A List Of Frame? Pin
Dave Kreskowiak25-Aug-12 4:21
mveDave Kreskowiak25-Aug-12 4:21 
QuestionLearning C# for a beginner Pin
Diego Carrion24-Aug-12 8:16
Diego Carrion24-Aug-12 8:16 
AnswerRe: Learning C# for a beginner Pin
Richard Andrew x6424-Aug-12 9:10
professionalRichard Andrew x6424-Aug-12 9:10 
GeneralRe: Learning C# for a beginner Pin
Diego Carrion24-Aug-12 9:16
Diego Carrion24-Aug-12 9:16 
GeneralRe: Learning C# for a beginner Pin
Richard Andrew x6424-Aug-12 9:20
professionalRichard Andrew x6424-Aug-12 9:20 
GeneralRe: Learning C# for a beginner Pin
Diego Carrion24-Aug-12 9:29
Diego Carrion24-Aug-12 9:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.