views:

144

answers:

3

I am creating an extension method in C# to retrieve some value from datagridview. Here if a user gives column name that doesnot exists then i want this function to throw an exception that can be handled at the place where this function will be called. How can i achieve this.

   public static T Value<T>(this DataGridView dgv, int RowNo, string ColName)
    {
            if (!dgv.Columns.Contains(ColName))
                throw new ArgumentException("Column Name " + ColName + " doesnot exists in DataGridView.");
            return (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
    }
+1  A: 

Is hard to understand your question, but it sounds like you are wanting to throw an exception and handle it where you call your extension method. If so, you are almost there. You are already throwing the exception, just put a try/catch block around the callsite.

public static T Value<T>(this DataGridView dgv, int RowNo, string ColName)
{
    if (!dgv.Columns.Contains(ColName))
        throw new ArgumentException("Column Name " + ColName + " doesnot exists in DataGridView.");
    return (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
}

// Wherever you call the method:
try
{
    dataGridView.Value(rowNumber, columnName);
}
catch (ArgumentException)
{
    // caught the exception
}

Is this what you are looking for?

Zach Johnson
@Zach: you understood what I meant to say. But I am little confused here. Suppose I creates an assembly for this and then includes it into some other project. I i don't use any exception handler over their then where will exception occur ? I mean in my dll or user code. I am feeling as if my dll will hang up over that point. And user using dll will trap their and all his application will close abnorymally. Is it so ?
Shantanu Gupta
@Shantanu: As long as you make your custom exception classes `public`, your other project will be able to catch the custom exceptions you throw. If not, the other project will crash (unless it catches all `Exception`s, but doing so is considered bad practice).
Zach Johnson
+2  A: 

This is not the right way to do this. First off, there is nothing exceptional about a user mis-typing something. Secondly, this extension method is liable to be called deeply nested in some kind of code that works with, say, the database. You have to catch the exception, since a typing mistake is normal. But you now also have the burden of writing the exception handler and a bunch of code that properly restores the state of the program.

Validating user input should happen early, before you set off a train of code that is hard to stop. And there's no reason to use exceptions in user input validation, a simple if() statement gets the job done.

You can now leave the throw statement in place, if you wish, it does make for a better diagnostic. But you should never handle that exception because now it diagnoses a bug in your code. And you can't fix bugs with catch clauses.

Hans Passant
+1  A: 

Though the question is already answered, i would just suggest a second solution. The idea of throwing exception should be the last option. You can achieve the same using the below approach.

public static bool TryGetValue<T>(this DataGridView dgv, int RowNo, 
    string ColName, out T cellValue)
{
    cellValue = default(T);
    if (!dgv.Columns.Contains(ColName))
        return false;
    cellValue = (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
    return true;
}

public static void Main(){
int desiredValue;
if(dataGridView.TryGetValue<int>(rowNumber, columnName, out desiredValue)){
    //Use the value
}
else{
    //Value can not be retrieved.
}
}

PS: i haven't typed this code on editor, so please excuse any typos.

Amby