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;
}

Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 8, 2014, 6:13 PM EST
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.
Vikram AgrawalRank #4,713 · 329 reputationPosted Dec 8, 2014, 9:40 PM EST
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.
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 12:34 AM EST
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
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 5:11 AM EST
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:
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 5:44 AM EST
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);
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 6:18 AM EST
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.
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 6:29 AM EST
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.
VulpesRank #55 · 29.2K reputationPosted Dec 10, 2014, 4:22 AM EST
David SmithRank #999 · 2K reputationPosted Dec 8, 2014, 7:37 PM EST
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 8, 2014, 7:45 PM EST
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 SmithRank #999 · 2K reputationPosted Dec 8, 2014, 8:38 PM EST
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 ChoudharyRank #339 · 7.4K reputationPosted Dec 8, 2014, 8:51 PM EST
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 SmithRank #999 · 2K reputationPosted Dec 8, 2014, 9:11 PM EST
public static int GetValue(this DataTable datatable, int rowindex, string columnName)
David SmithRank #999 · 2K reputationPosted Dec 9, 2014, 4:18 AM EST
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)
{
}
David SmithRank #999 · 2K reputationPosted Dec 9, 2014, 5:15 AM EST
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);
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 5:27 AM EST
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 SmithRank #999 · 2K reputationPosted Dec 9, 2014, 5:36 AM EST
David SmithRank #999 · 2K reputationPosted Dec 9, 2014, 5:52 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 5:58 AM EST
David SmithRank #999 · 2K reputationPosted Dec 9, 2014, 6:11 AM EST
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);
}
}
David SmithRank #999 · 2K reputationPosted Dec 9, 2014, 6:22 AM EST
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);
}
}
David SmithRank #999 · 2K reputationPosted Dec 9, 2014, 6:54 AM EST
David SmithRank #999 · 2K reputationPosted Dec 9, 2014, 6:54 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 9, 2014, 6:59 AM EST
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 SmithRank #999 · 2K reputationPosted Dec 9, 2014, 5:09 PM EST
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);
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 10, 2014, 12:59 AM EST
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 SmithRank #999 · 2K reputationPosted Dec 10, 2014, 3:15 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 10, 2014, 3:39 AM EST
David SmithRank #999 · 2K reputationPosted Dec 10, 2014, 4:07 AM EST
David SmithRank #999 · 2K reputationPosted Dec 10, 2014, 4:27 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 10, 2014, 4:33 AM EST
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 SmithRank #999 · 2K reputationPosted Dec 10, 2014, 4:59 AM EST