Click here to Skip to main content
11,575,635 members (63,396 online)
   

C#

 
AnswerRe: Tuples in Functions Pin
DaveyM6917-Aug-12 22:21
mentorDaveyM6917-Aug-12 22:21 
GeneralRe: Tuples in Functions Pin
BillWoodruff18-Aug-12 7:17
memberBillWoodruff18-Aug-12 7:17 
GeneralRe: Tuples in Functions Pin
DaveyM6918-Aug-12 8:09
mentorDaveyM6918-Aug-12 8:09 
AnswerRe: Tuples in Functions Pin
Meysam Tolouee18-Aug-12 5:20
memberMeysam Tolouee18-Aug-12 5:20 
GeneralRe: Tuples in Functions Pin
DaveyM6918-Aug-12 8:15
mentorDaveyM6918-Aug-12 8:15 
GeneralRe: Tuples in Functions Pin
Meysam Tolouee18-Aug-12 9:53
memberMeysam Tolouee18-Aug-12 9:53 
AnswerRe: Tuples in Functions Pin
Keith Barrow18-Aug-12 9:38
mentorKeith Barrow18-Aug-12 9:38 
AnswerRe: Tuples in Functions Pin
BillWoodruff18-Aug-12 9:43
memberBillWoodruff18-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:
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 Pin
Keith Barrow18-Aug-12 10:32
mentorKeith Barrow18-Aug-12 10:32 
GeneralRe: Tuples in Functions Pin
leppie18-Aug-12 21:37
memberleppie18-Aug-12 21:37 
GeneralRe: Tuples in Functions Pin
BillWoodruff19-Aug-12 14:15
memberBillWoodruff19-Aug-12 14:15 
GeneralRe: Tuples in Functions Pin
leppie19-Aug-12 19:41
memberleppie19-Aug-12 19:41 
AnswerLeppie's interesting suggestion to use 'Anonymous Classes' ... Re: Tuples in Functions Pin
BillWoodruff18-Aug-12 15:49
memberBillWoodruff18-Aug-12 15:49 
GeneralRe: Leppie's interesting suggestion to use 'Anonymous Classes' ... Re: Tuples in Functions Pin
BobJanova20-Aug-12 1:00
memberBobJanova20-Aug-12 1:00 
QuestionTrying to Obtain Two (2) Values from Function Pin
computerpublic16-Aug-12 21:53
membercomputerpublic16-Aug-12 21:53 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
DaveyM6916-Aug-12 22:36
mentorDaveyM6916-Aug-12 22:36 
GeneralRe: Trying to Obtain Two (2) Values from Function Pin
BobJanova16-Aug-12 22:54
memberBobJanova16-Aug-12 22:54 
GeneralRe: Trying to Obtain Two (2) Values from Function Pin
Keith Barrow17-Aug-12 2:12
mentorKeith Barrow17-Aug-12 2:12 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
Sangramsingh Pawar17-Aug-12 0:43
memberSangramsingh Pawar17-Aug-12 0:43 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
V.17-Aug-12 3:16
memberV.17-Aug-12 3:16 
AnswerRe: Trying to Obtain Two (2) Values from Function Pin
Kuthuparakkal17-Aug-12 16:05
memberKuthuparakkal17-Aug-12 16:05 
QuestionHow to insert,update and edit data in datagridview window form Pin
Member 912701916-Aug-12 19:54
memberMember 912701916-Aug-12 19:54 
AnswerRe: How to insert,update and edit data in datagridview window form Pin
Calla16-Aug-12 20:53
memberCalla16-Aug-12 20:53 
GeneralRe: How to insert,update and edit data in datagridview window form Pin
Dave Kreskowiak17-Aug-12 3:37
mvpDave Kreskowiak17-Aug-12 3:37 
AnswerRe: How to insert,update and edit data in datagridview window form Pin
Uma Shankar Patel17-Aug-12 21:52
memberUma Shankar Patel17-Aug-12 21: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.150624.2 | Last Updated 3 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid