我有一个名为FindSpecificRowValue的函数,它接受一个数据表并返回包含特定值的行号.如果找不到该值,我想向调用函数指示.
是最好的方法:
编写一个函数,如果找不到则返回false,如果找到则返回true,找到的行号作为byref/output参数,或者
编写一个返回int的函数,如果找不到行值则返回-999,如果是行号则返回行号?
angry person.. 13
就个人而言,我不会使用该方法名称.
我会改为两种方法:
TryFindSpecificRow FindSpecificRow
这将遵循Int32.Parse/TryParse的模式,在C#中它们可能如下所示:
public static Boolean TryFindSpecificRow(DataTable table, out Int32 rowNumber) { if (row-can-be-found) { rowNumber = index-of-row-that-was-found; return true; } else { rowNumber = 0; // this value will not be used anyway return false; } } public static Int32 FindSpecificRow(DataTable table) { Int32 rowNumber; if (TryFindSpecificRow(table, out rowNumber)) return rowNumber; else throw new RowNotFoundException(String.Format("Row {0} was not found", rowNumber)); }
编辑:更改为更适合问题.
就个人而言,我不会使用该方法名称.
我会改为两种方法:
TryFindSpecificRow FindSpecificRow
这将遵循Int32.Parse/TryParse的模式,在C#中它们可能如下所示:
public static Boolean TryFindSpecificRow(DataTable table, out Int32 rowNumber) { if (row-can-be-found) { rowNumber = index-of-row-that-was-found; return true; } else { rowNumber = 0; // this value will not be used anyway return false; } } public static Int32 FindSpecificRow(DataTable table) { Int32 rowNumber; if (TryFindSpecificRow(table, out rowNumber)) return rowNumber; else throw new RowNotFoundException(String.Format("Row {0} was not found", rowNumber)); }
编辑:更改为更适合问题.