2

I want to use Tuples in my code dynamically and have to assign the values to tuple according to the if statement. I have the following code:

if(check != null)
  var scoreTmpTuple = new Tuple<Guid, string, double>(
    (Guid)row["sampleGuid"],
     Convert.ToString(row["sampleName"]), 
     Convert.ToDouble(row["sampleScore"]));
else
  var scoreTmpTuple = new Tuple<Guid, string, double>(
    (Guid)row["exampleGuid"],
     Convert.ToString(row["exampleName"]), 
     Convert.ToDouble(row["exampleScore"]));

In the code, the tuple is declared inside the if and else statements. I want to declare that outside and initialize the tuple accordingly.

2
  • That might work but i had the scope issue for the tuple .. because alot of things are being done after initializing and i want the same tuple for that . Commented Oct 31, 2019 at 10:26
  • Above c#4, instead of Tuple, you could use dynamic datatype. Commented Oct 31, 2019 at 10:56

4 Answers 4

4

Declare the tuple before the if statement.

Tuple<Guid, string, double> scoreTmpTuple;

if(check != null)
   scoreTmpTuple = new Tuple<Guid, string, double>((Guid)row["sampleGuid"],Convert.ToString(row["sampleName"]), Convert.ToDouble(row["sampleScore"]));

else
   scoreTmpTuple = new Tuple<Guid, string, double>((Guid)row["exampleGuid"],Convert.ToString(row["exampleName"]), Convert.ToDouble(row["exampleScore"]));
Sign up to request clarification or add additional context in comments.

Comments

4

Just specify the type explicity instead of using var:

Tuple<Guid, string, double> scoreTmpTuple;

if (check != null)
    scoreTmpTuple = Tuple.Create<Guid, string, double>(Guid.NewGuid(), "hello", 3.14);

Comments

2

You can try ternary operator and push ramification within tuple creation:

  var scoreTmpTuple = Tuple.Create(
                (Guid)row[check != null ? "sampleGuid" : "exampleGuid"],
     Convert.ToString(row[check != null ? "sampleName" : "exampleName"]),
     Convert.ToDouble(row[check != null ? "sampleScore" : "exampleScore"])
  );

Or even (if we actually should switch between "sample" and "example" prefixes):

  string prefix = check != null 
    ? "sample"
    : "example";

  var scoreTmpTuple = Tuple.Create(
                (Guid)row[$"{prefix}Guid"],
     Convert.ToString(row[$"{prefix}Name"]),
     Convert.ToDouble(row[$"{prefix}Score"])
  );

Comments

1

You can do this:

bool isChecked = check != null;

var scoreTmpTuple = new Tuple<Guid, string, double>(
                      isChecked ? (Guid)row["sampleGuid"] : (Guid)row["exampleGuid"],
                      isChecked ? Convert.ToString(row["sampleName"]) : Convert.ToString(row["exampleName"]), 
                      isChecked ? Convert.ToDouble(row["sampleScore"]) : Convert.ToDouble(row["exampleScore"]));

1 Comment

bool isChecked = check != null; is a shorter code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.