Click here to Skip to main content
15,890,123 members
Home / Discussions / C#
   

C#

 
AnswerRe: Tuples in Functions Pin
DaveyM6917-Aug-12 22:21
professionalDaveyM6917-Aug-12 22:21 
GeneralRe: Tuples in Functions Pin
BillWoodruff18-Aug-12 7:17
professionalBillWoodruff18-Aug-12 7:17 
GeneralRe: Tuples in Functions Pin
DaveyM6918-Aug-12 8:09
professionalDaveyM6918-Aug-12 8:09 
AnswerRe: Tuples in Functions Pin
WebMaster18-Aug-12 5:20
WebMaster18-Aug-12 5:20 
GeneralRe: Tuples in Functions Pin
DaveyM6918-Aug-12 8:15
professionalDaveyM6918-Aug-12 8:15 
GeneralRe: Tuples in Functions Pin
WebMaster18-Aug-12 9:53
WebMaster18-Aug-12 9:53 
AnswerRe: Tuples in Functions Pin
Keith Barrow18-Aug-12 9:38
professionalKeith Barrow18-Aug-12 9:38 
AnswerRe: Tuples in Functions Pin
BillWoodruff18-Aug-12 9:43
professionalBillWoodruff18-Aug-12 9: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:
C#
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:
C#
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 Pin
Keith Barrow18-Aug-12 10:32
professionalKeith Barrow18-Aug-12 10:32 
GeneralRe: Tuples in Functions Pin
leppie18-Aug-12 21:37
leppie18-Aug-12 21:37 
GeneralRe: Tuples in Functions Pin
BillWoodruff19-Aug-12 14:15
professionalBillWoodruff19-Aug-12 14:15 
GeneralRe: Tuples in Functions Pin
leppie19-Aug-12 19:41
leppie19-Aug-12 19:41 
AnswerLeppie's interesting suggestion to use 'Anonymous Classes' ... Re: Tuples in Functions Pin
BillWoodruff18-Aug-12 15:49
professionalBillWoodruff18-Aug-12 15:49 
GeneralRe: Leppie's interesting suggestion to use 'Anonymous Classes' ... Re: Tuples in Functions Pin
BobJanova20-Aug-12 1:00
BobJanova20-Aug-12 1:00 
QuestionTrying to Obtain Two (2) Values from Function Pin
computerpublic16-Aug-12 21:53
computerpublic16-Aug-12 21:53 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
DaveyM6916-Aug-12 22:36
professionalDaveyM6916-Aug-12 22:36 
GeneralRe: Trying to Obtain Two (2) Values from Function Pin
BobJanova16-Aug-12 22:54
BobJanova16-Aug-12 22:54 
GeneralRe: Trying to Obtain Two (2) Values from Function Pin
Keith Barrow17-Aug-12 2:12
professionalKeith Barrow17-Aug-12 2:12 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
Sangramsingh Pawar17-Aug-12 0:43
Sangramsingh Pawar17-Aug-12 0:43 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
V.17-Aug-12 3:16
professionalV.17-Aug-12 3:16 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
Kuthuparakkal17-Aug-12 16:05
Kuthuparakkal17-Aug-12 16:05 
QuestionHow to insert,update and edit data in datagridview window form Pin
ope4sure116-Aug-12 19:54
ope4sure116-Aug-12 19:54 
AnswerRe: How to insert,update and edit data in datagridview window form Pin
Calla16-Aug-12 20:53
Calla16-Aug-12 20:53 
GeneralRe: How to insert,update and edit data in datagridview window form Pin
Dave Kreskowiak17-Aug-12 3:37
mveDave Kreskowiak17-Aug-12 3:37 
AnswerRe: How to insert,update and edit data in datagridview window form Pin
uspatel17-Aug-12 21:52
professionaluspatel17-Aug-12 21:52 

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.