I am trying to overload in my static method, I am getting an error that method already exist, how to fix this issue?
{
return 0;
}
public static double GetValue(this DataTable datatable, int rowindex, string columnName)
{
return 0.0;
}
public static decimal GetValue(this DataTable datatable, int rowindex, string columnName)
{
return (decimal)0.0;
}
public static float GetValue(this DataTable datatable, int rowindex, string columnName)
{
return (float)0.0;
}
David SmithPosted Dec 10, 2014, 9:59 AM
VulpesPosted Dec 10, 2014, 9:33 AM
If you don't like 'var' (a lot of developers don't and I have reservations about it myself) then just replace it with DataView.
David SmithPosted Dec 10, 2014, 9:27 AM
VulpesPosted Dec 10, 2014, 9:22 AM
David SmithPosted Dec 10, 2014, 9:07 AM
VulpesPosted Dec 10, 2014, 8:39 AM
David SmithPosted Dec 10, 2014, 8:15 AM
VulpesPosted Dec 10, 2014, 5:59 AM
So are you trying to create a duplicate DataView for the same table or for a clone of that table? Currently, the code is doing the latter.
David SmithPosted Dec 9, 2014, 10:09 PM
public static DataView CreateDuplicateDataView(this DataTable datatable)
{
try
{
if (datatable == null)
throw new ArgumentNullException("source");
DataTable datatableCopy = datatable.Copy();
datatableCopy.DefaultView.RowFilter = datatable.DefaultView.RowFilter;
return datatableCopy.DefaultView;
}
catch (Exception ex)
{
throw new Exception("CreateDuplicateDataView: \n" + ex.Message);
}
}
VulpesPosted Dec 9, 2014, 11:59 AM
Personally, I prefer to always use the C# alias for this and the other basic types though there are developers who prefer to do the opposite.
David SmithPosted Dec 9, 2014, 11:54 AM
David SmithPosted Dec 9, 2014, 11:54 AM
VulpesPosted Dec 9, 2014, 11:29 AM
BTW, if you're wondering why there doesn't seem to be a Convert.ToFloat method, it's because it's actually called Convert.ToSingle given that C#'s float type is equivalent to the System.Single structure.
Although in theory datatable.Rows[rowindex][columnName] should never be null (only DBNullValue) it might be better to use the Convert.ToSingle method rather than a cast because, if it were null, the former would return 0 rather than throw an exception.
David SmithPosted Dec 9, 2014, 11:22 AM
public static bool GetBoolValue(this DataTable datatable, int rowindex, string columnName)
{
try
{
return Convert.ToBoolean(datatable.Rows[rowindex][columnName]);
}
catch (Exception ex)
{
throw new Exception("GetBoolValue: \n" + ex.Message);
}
}
public static float GetFloatValue(this DataTable datatable, int rowindex, string columnName)
{
try
{
return (float)datatable.Rows[rowindex][columnName];
}
catch (Exception ex)
{
throw new Exception("GetFloatValue: \n" + ex.Message);
}
}
VulpesPosted Dec 9, 2014, 11:18 AM
In fact it automatically caters for DBNull.Value because when you apply ToString() to that expression you get String.Empty which is a reasonable default.
David SmithPosted Dec 9, 2014, 11:11 AM
public static string GetStringValue(this DataTable datatable, int rowindex, string columnName)
{
try
{
return (datatable.Rows[rowindex][columnName]).ToString();
}
catch (Exception ex)
{
throw new Exception("GetStringValue: \n" + ex.Message);
}
}
VulpesPosted Dec 9, 2014, 10:58 AM
David SmithPosted Dec 9, 2014, 10:52 AM
VulpesPosted Dec 9, 2014, 10:44 AM
public static int GetIntValue(this DataTable datatable, int rowindex, string columnName)
{
try
{
object obj = datatable.Rows[rowindex][columnName];
if(Convert.IsDBNull(obj))
{
return 0; // or perhaps some other value such as -1
}
else
{
return Convert.ToInt32(obj);
}
}
catch (Exception ex)
{
throw new Exception("GetIntValue: \n" + ex.Message);
}
}
David SmithPosted Dec 9, 2014, 10:36 AM
VulpesPosted Dec 9, 2014, 10:27 AM
The only question is whether it's over-robust in relation to DBNull values. Are you happy to thrown an exception in such cases or would you rather return a default value?
David SmithPosted Dec 9, 2014, 10:15 AM
public static int GetIntValue(this DataTable datatable, int rowindex, string columnName)
{
try
{
return Convert.ToInt32(datatable.Rows[rowindex][columnName]);
}
catch (Exception ex)
{
throw new Exception("GetIntValue: \n" + ex.Message);
}
}
VulpesPosted Dec 9, 2014, 10:11 AM
You'd need to pass an additional parameter (of type T) so that the compiler could infer the type of T but, if you made this into an 'out' parameter then you wouldn't need a return value as well:
and call with:
David SmithPosted Dec 9, 2014, 9:18 AM
1st Option:
public static objectGetValue(this DataTable datatable, int rowindex, string columnName)
{
}
2nd Option: Change the name of the function
public static decimal GetIntValue(this DataTable datatable, int rowindex, string columnName)
{
}
public static decimal GetDoubleValue(this DataTable datatable, int rowindex, string columnName)
{
}
VulpesPosted Dec 9, 2014, 5:34 AM
or an int column with:
int i = (int)datatable.GetValue(rowindex, columnName);
There's also the possibility that the method may return DBNull.Value if the corresponding value in the database is NULL.
It would be possible to deal with that automatically by returning instead the default value of the column's type using the following revised code:
So, in the first example, instead of returning DBNull.Value which would cause an exception when you attempt to cast to decimal, the method would return 0m which is the default value of the decimal type and there would be no exception:
decimal d = (decimal)datatable.GetValue(rowindex, columnName); // d == 0m
Vikram AgrawalPosted Dec 9, 2014, 2:40 AM
Well as of now whatever manish has told is absolutely right and if you want all these things then you should change your method name and here is no need to keep same name.
You can name it like :
GetIntValue(parameters)
GetDoubleValue(parameters)
GetFloatValue(parameters)
GetDecimalValue(parameters)
Thanks.
David SmithPosted Dec 9, 2014, 2:11 AM
public static int GetValue(this DataTable datatable, int rowindex, string columnName)
Manish Kumar ChoudharyPosted Dec 9, 2014, 1:51 AM
u can just do like this(Simplest way)
public static int GetValue(this DataTable datatable, int rowindex, string columnName)
{
Convert.ToInd(rowindex)//Use It as int
return 0;
}
public static double GetValue(this DataTable datatable, double rowindex, string columnName)
{
Convert.ToInd(rowindex)//Use It as int
return 0.0;
}
public static decimal GetValue(this DataTable datatable, decimal rowindex, string columnName)
{
Convert.ToInd(rowindex)//Use It as int
return (decimal)0.0;
}
public static float GetValue(this DataTable datatable, float rowindex, string columnName)
{
Convert.ToInd(rowindex)//Use It as int
return (float)0.0;
}
call the above function with just rowindex that can be converted to int easily.
Visit following links for more details
http://msdn.microsoft.com/en-us/library/ms229029.aspx
David SmithPosted Dec 9, 2014, 1:38 AM
public static int GetValue(this DataTable datatable, int rowindex, string columnName)
{
return 0;
}
public static double GetValue(this DataTable datatable, int rowindex, string columnName)
{
return 0.0;
}
public static decimal GetValue(this DataTable datatable, int rowindex, string columnName)
{
return (decimal)0.0;
}
public static float GetValue(this DataTable datatable, int rowindex, string columnName)
{
return (float)0.0;
}
Manish Kumar ChoudharyPosted Dec 9, 2014, 12:45 AM
Overloading means using same function name doing different things. For that Two function should have different signature otherwise compiler cant differentiate. You must have different signature.
David SmithPosted Dec 9, 2014, 12:37 AM
Manish Kumar ChoudharyPosted Dec 8, 2014, 11:13 PM
Change the parameter list of one of the function so that compiler can identify the difference of those functions then your code will run fine.
this DataTable datatable, int rowindex, string columnName
this list should be different for all function.