# match and assign to variable according to the matched 2D array element. [Answered]RSS

## 3 replies

Last post May 27, 2017 08:09 AM by Jean Sun

• zyanedwards

Member

9 Points

28 Posts

### match and assign to variable according to the matched 2D array element.

I'm having trouble how can I match and assign to variable according to the matched 2D array element.

I don't know the logic in doing this.

```string level = "";

dynamic[,] levelsWithLives = new dynamic[,]
{    { "easy", 15 },
{ "medium", 10 },
{ "hard", 5 },
};

// Iterate through the array
for (int i = 0; i < levelsWithLives.Length / 2; i++)
{
// Check if the entered value matched any of the values from the array
while (!level.Contains(levelsWithLives[i, 0]))
{
Write("What level? Easy, Medium, Hard, Advanced");
}

break;
}

uint life = 0;

// Iterate through the array
for (int i = 0; i < levelsWithLives.Length / 2; i++)
{
if (level.Contains(levelsWithLives[i, 0]))
{
// Here gives me: cannot convert type int into uint
life = levelsWithLives[i, 1]; // How can I assign a value according to the given level
break;
}
• Eric Du

Contributor

6710 Points

2715 Posts

### Re: match and assign to variable according to the matched 2D array element.

May 18, 2017 10:29 AM|Eric Du|LINK

Hi zyanedwards,

According to your description and this error message(cannot convert type int into uint), I only find this error, I am not clear your other needs and other issue.

About this error, if it is int type, you could use this method to convert the type.

```uint u = (uint) (int) obj;

or this easier method:

uint u = Convert.ToUInt32(x);
```

If you have other needs, please post your description of the needs.

Best Regards,

Eric Du

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• zyanedwards

Member

9 Points

28 Posts

### Re: match and assign to variable according to the matched 2D array element.

I'm having trouble with my code above as such when the user enters a certain level and compared it to the dynamic array, the loop only searched for the first dimension of the first loop occurrence. If no matches, he/she will be asked again for the level, and so forth.

• Jean Sun

Contributor

6450 Points

2525 Posts

### Re: match and assign to variable according to the matched 2D array element.

May 27, 2017 08:09 AM|Jean Sun|LINK

Hi zyanedwards,

#### zyanedwards

I'm having trouble with my code above as such when the user enters a certain level and compared it to the dynamic array, the loop only searched for the first dimension of the first loop occurrence. If no matches, he/she will be asked again for the level, and so forth.

I modified your code on my side and it works fine now, please take the following code as reference.

```            string level = "";

dynamic[,] levelsWithLives = new dynamic[,]
{
{ "easy", 15 },
{ "medium", 10 },
{ "hard", 5 },
};

// Iterate through the array

//define a value that represents that the input is a valid level
bool levelfind = false;

//keep looping until the user inputs a valid level
while(!levelfind)
{
Console.Write("What level? Easy, Medium, Hard, Advanced");

for (int i = 0; i < levelsWithLives.Length / 2; i++)
{
// Check if the entered value matched any of the values from the array, it it matches set the levelfind to ture and break the loop
if(level.Contains(levelsWithLives[i, 0]))
{
levelfind = true;
break;
}
}
}

uint life = 0;

// Iterate through the array
for (int i = 0; i < levelsWithLives.Length / 2; i++)
{
if (level.Contains(levelsWithLives[i, 0]))
{
// Here gives me: cannot convert type int into uint
life = (uint)levelsWithLives[i, 1]; // How can I assign a value according to the given level
break;
}
}```

Best Regards,

Jean

MSDN Community Support