Click here to Skip to main content
15,891,645 members
Home / Discussions / C#
   

C#

 
QuestionConverting textbox into textarea. Pin
Diego Carrion25-Aug-12 16:16
Diego Carrion25-Aug-12 16:16 
AnswerRe: Converting textbox into textarea. Pin
OriginalGriff25-Aug-12 22:26
mveOriginalGriff25-Aug-12 22:26 
GeneralRe: Converting textbox into textarea. Pin
Diego Carrion25-Aug-12 22:36
Diego Carrion25-Aug-12 22:36 
GeneralRe: Converting textbox into textarea. Pin
BillWoodruff26-Aug-12 15:36
professionalBillWoodruff26-Aug-12 15:36 
GeneralRe: Converting textbox into textarea. Pin
Diego Carrion27-Aug-12 16:31
Diego Carrion27-Aug-12 16:31 
QuestionStoring File Path on Database Pin
ASPnoob25-Aug-12 14:34
ASPnoob25-Aug-12 14:34 
AnswerRe: Storing File Path on Database Pin
AmitGajjar25-Aug-12 22:20
professionalAmitGajjar25-Aug-12 22:20 
AnswerRe: Storing File Path on Database Pin
OriginalGriff25-Aug-12 22:34
mveOriginalGriff25-Aug-12 22:34 
QuestionRe: Storing File Path on Database Pin
Eddy Vluggen25-Aug-12 23:34
professionalEddy Vluggen25-Aug-12 23:34 
AnswerRe: Storing File Path on Database Pin
ASPnoob26-Aug-12 0:27
ASPnoob26-Aug-12 0:27 
GeneralRe: Storing File Path on Database Pin
Eddy Vluggen26-Aug-12 0:40
professionalEddy Vluggen26-Aug-12 0:40 
QuestionChange Combobox Look in WPF Pin
a.fatemeh25-Aug-12 9:24
a.fatemeh25-Aug-12 9:24 
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 

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.