Click here to Skip to main content
       

C#

 
AnswerRe: Tuples in Functions PinmentorDaveyM6917-Aug-12 23:21 
GeneralRe: Tuples in Functions PinmemberBillWoodruff18-Aug-12 8:17 
GeneralRe: Tuples in Functions PinmentorDaveyM6918-Aug-12 9:09 
AnswerRe: Tuples in Functions PinmemberMeysam Tolouee18-Aug-12 6:20 
GeneralRe: Tuples in Functions PinmentorDaveyM6918-Aug-12 9:15 
GeneralRe: Tuples in Functions PinmemberMeysam Tolouee18-Aug-12 10:53 
AnswerRe: Tuples in Functions [modified] PinmentorKeith Barrow18-Aug-12 10:38 
AnswerRe: Tuples in Functions PinmemberBillWoodruff18-Aug-12 10:43 
Between the responses to your first post, the responses to this post, and the (strangely out of place) discussion on the Lounge[^], you have a of ideas, and alternate strategies for using Tuples.
 
I want to respond directly to your question about using Tuples. In my opinion, they can best be described as a "bag of pairs" where, for every pair in the bag: the first element, of the pair, is a Type; and, the second element is a value of that Type.
 
And, do keep in mind that Tuples were introduced in .NET 4.0.
 
So, they are most appropriate, imho, if you need to return a bunch of different types: if every Type in your Tuple elements is the same: then use one of the other strategies, or a generic List<Type>, or another method.
 
Let me try and make this concrete for you: you can download this project (with source) compiled against .NET 4.0 here:[^].
 
1. create a simple 'WinForms application: drop a 'CheckBox, a 'DateTimePicker, a 'TextBox, and a 'NumericUpDown[1] on the main Form.
 
2. put two 'Buttons on the Form: title the first 'Button "Save Tuple," and the second "Restore Tuple."
 
Now let's look at what happens using a real Tuple in action: (attention: code-style obsessive-compulsives: I am using extra indentation, and line-breaks, here for clarity for the OP, it would not be my standard style):
 
1. first you declare a Form-scoped variable of Type Tuple for re-use:
private Tuple<CheckState, DateTime, string, decimal> _fourTuple;
1. You run the application, change the values of any of the four controls any way you like, and click the "Save Tuple" button: this code executes:
private void SaveTupleButtonClick(object sender, EventArgs e)
{
    _fourTuple = Tuple.Create
    (
        checkBox1.CheckState,
        dateTimePicker1.Value,
        textBox1.Text, 
        numericUpDown1.Value
    );
}
Here I am using the static Tuple.Create method, which was introduced in .NET FrameWork 4.0, to save some typing; it's exactly the same as if I wrote the fuller version:
private void SaveTupleButton_Click(object sender, EventArgs e)
{
    _fourTuple = new Tuple<CheckState, DateTime, string, decimal>
    (
        checkBox1.CheckState,
        dateTimePicker1.Value,
        textBox1.Text, 
        numericUpDown1.Value
    );
}
And now: what ? Well, you've saved the current values of these diverse types of Controls in your tuple. So, go ahead and change the values of all the Controls, and hit the "Restore Tuple" 'Button:
private void RestoreTupleClick(object sender, EventArgs e)
{
    checkBox1.CheckState = _fourTuple.Item1;
    dateTimePicker1.Value = _fourTuple.Item2;
    textBox1.Text = _fourTuple.Item3;
    numericUpDown1.Value = _fourTuple.Item4;
}
I hope you will see the controls' values reset to the exact value they were: when you pressed the "Save Tuple" 'Button.
 
Hope this example gives you some ideas.
 
best, Bill
 
[1] Keep in mind that 'NumericUpDownControls have 'Minimum and 'Maximum value settings, as well as a 'DecimalPlaces property: if you type in a value out of range: it will revert to the nearest min or max value: this can have a confusing run-time effect for the end user.
"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

GeneralMy Vote of 5 PinmentorKeith Barrow18-Aug-12 11:32 
GeneralRe: Tuples in Functions Pinmemberleppie18-Aug-12 22:37 
GeneralRe: Tuples in Functions PinmemberBillWoodruff19-Aug-12 15:15 
GeneralRe: Tuples in Functions Pinmemberleppie19-Aug-12 20:41 
AnswerLeppie's interesting suggestion to use 'Anonymous Classes' ... Re: Tuples in Functions PinmemberBillWoodruff18-Aug-12 16:49 
GeneralRe: Leppie's interesting suggestion to use 'Anonymous Classes' ... Re: Tuples in Functions PinmemberBobJanova20-Aug-12 2:00 
QuestionTrying to Obtain Two (2) Values from Function Pinmembercomputerpublic16-Aug-12 22:53 
AnswerRe: Trying to Obtain Two (2) Values from Function PinmentorDaveyM6916-Aug-12 23:36 
GeneralRe: Trying to Obtain Two (2) Values from Function PinmemberBobJanova16-Aug-12 23:54 
GeneralRe: Trying to Obtain Two (2) Values from Function PinmentorKeith Barrow17-Aug-12 3:12 
AnswerRe: Trying to Obtain Two (2) Values from Function PinmemberSangramsingh Pawar17-Aug-12 1:43 
AnswerRe: Trying to Obtain Two (2) Values from Function PinmemberV.17-Aug-12 4:16 
AnswerRe: Trying to Obtain Two (2) Values from Function PinmemberKuthuparakkal17-Aug-12 17:05 
QuestionHow to insert,update and edit data in datagridview window form PinmemberMember 912701916-Aug-12 20:54 
AnswerRe: How to insert,update and edit data in datagridview window form PinmemberCalla16-Aug-12 21:53 
GeneralRe: How to insert,update and edit data in datagridview window form PinmvpDave Kreskowiak17-Aug-12 4:37 
AnswerRe: How to insert,update and edit data in datagridview window form PinmemberUma Shankar Patel17-Aug-12 22:52 

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

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


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 26 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid