Вопрос

I feel kind of silly asking this because either I'm missing something increidbly easy or not understanding the tryParse function at all.

In laymans terms, what I want to do in the following code is have the code loop through all of the columns in a datagridview. If the last 2 characters of the column name are numeric, I then want to compare the numeric value of the last 2 characters (using int.TryParse) to another variable. essentially I want to make all of my columns read only excpet for those columns where the last 2 digits can be converted to an integer and that integer is greater than the vairable I'm comparing to.

My code below is probably a little mundane as I'm trying to go through step by step setting variables before the int_tryParse but I can fix that later.

C#, VS2008:

foreach (DataGridViewColumn col in grd1.Columns)
                {
                    string myCol = col.Name;
                    int myColLength = col.Name.Length;
                    string myColMonth = myCol.Substring(myColLength - 2);
                    if (int.TryParse(myColMonth, out myColMonth) <= myMostRecentActualMonth)
                    {
                        col.ReadOnly = true;
                    }
                    else
                    {
                        col.ReadOnly = false;
                    }
                }        
Это было полезно?

Решение

The TryParse method returns a Boolean value indicating if the parse was successful or not. If so, it sets the output parameter to the parsed value.

So what you'll want is something like:

int parsedMonth; // This will get set if myColMonth is a valid integer
if (int.TryParse(myColMonth, out parsedMonth) && parsedMonth <= myMostRecentActualMonth)
{
   // ...
}

Другие советы

int.TryParse has a return type of bool:

Try going from here:

       foreach (DataGridViewColumn col in grd1.Columns)
        {
            string myCol = col.Name;
            int myColLength = col.Name.Length;
            string myColMonth = myCol.Substring(myColLength - 2);
            int myIntColMonth;
            if (int.TryParse(myColMonth, out myIntColMonth) 
                && myIntColMonth <= myMostRecentActualMonth)
            {
                col.ReadOnly = true;
            }
            else
            {
                col.ReadOnly = false;
            }
        }   

TryParse returns a bool indicating whether or not the conversion was successful. You don't want to compare the result of TryParse (what you're doing) you instead want to compare with the variable you're passing into it.

So; if (int.TryParse(myColMonth, out myColMonth) <= myMostRecentActualMonth)

needs to become;

 if (int.TryParse(myColMonth, out myColMonth)
    if ( myColMonth <= myMostRecentActualMonth)

First checking that you parsed out an int, then doing the compare.

foreach (DataGridViewColumn col in grd1.Columns)
     {
         string myCol = col.Name;
         int myColLength = col.Name.Length;
         string myColMonth = myCol.Substring(myColLength - 2);
         int myColMonthInt = 0;
         if (int.TryParse(myColMonth, out myColMonthInt)) 
         {  
             if (myColMonthInt <= myMostRecentActualMonth)
             {
                 col.ReadOnly = true;
             }
             else
             {
                 col.ReadOnly = false;
             }
         } 
         else 
         {
             // what do you want to do is last two chars can't be converted to int?
             col.ReadOnly = true;
         }
     }        

You could rewrite your code like this, first you need another int value to which you should store your parsed value, then check if that value is lower than myMostRecentActualMonth

foreach (DataGridViewColumn col in grd1.Columns)
{
    string myCol = col.Name;
    int myColLength = col.Name.Length;
    string myColMonth = myCol.Substring(myColLength - 2);
    int myColMonthIntValue = int.MaxValue;
    if (int.TryParse(myColMonth, out myColMonthIntValue) && myColMonthIntValue <= myMostRecentActualMonth)
    {
        col.ReadOnly = true;
    }
    else
    {
        col.ReadOnly = false;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top