Alyanto
asked on
Converting a piece of code from VB to C#
A Year or so I wrote a piece of code in VB to allow me to have an array of objects that could be tested to sse if they existed i.e. not null. I am trying to convert this to C#. I have got the conversion this far.
The original looked like this.
The error I am getting is
I suspect it is to do with where I have tried to put T? making T a nullable object.
public class DataServices
{
public static T? Coalesce<T>(T Obj, params T[] Args)
{
if (Obj != null && IsDBNull(Obj))
{
return Obj;
}
else
{
foreach (T arg in Args )
{
if (arg != null && IsDBNull(arg)) return arg;
}
}
return null;
}
public static Boolean IsDBNull(Object Value)
{
return DBNull.Value.Equals(Value);
}
}
The original looked like this.
Public Shared Function Coalesce(Of T)(ByVal obj As T, ByVal ParamArray Args() As T) As T
If obj IsNot Nothing AndAlso Not IsDBNull(obj) Then
Return obj
End If
For Each arg As T In Args
If arg IsNot Nothing AndAlso Not IsDBNull(obj) Then
Return arg
End If
Next
Return Nothing
End Function
The error I am getting is
Error 1 The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>' C:\Users\carl.symington\Documents\Vi sual Studio 2012\Projects\DataServices \DataServi ces\DataSe rvices.cs 11 26 DataServices
I suspect it is to do with where I have tried to put T? making T a nullable object.
ASKER
Lokesh this causes the error
Error 1 Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead. C:\Users\carl.symington\Documents\Vi sual Studio 2012\Projects\DataServices \DataServi ces\DataSe rvices.cs 24 20 DataServices
ASKER
Just to be clear the C# version of IsDBNull is
public static Boolean IsDBNull(Object Value)
{
return DBNull.Value.Equals(Value);
}
Hi,
This is the code i get when converted from VB to C#
This is the code i get when converted from VB to C#
public static T Coalesce<T>(T obj, params T[] Args)
{
if (obj != null && !Information.IsDBNull(obj)) {
return obj;
}
foreach (T arg in Args) {
if (arg != null && !Information.IsDBNull(obj)) {
return arg;
}
}
return null;
}
by definition, an object can be null so it doesn't need the ? (as opposed to date, int, ...). I think you just need to remove it
ASKER
Eric the code that you suggested gives this error
public class DataServices
{
public static T Coalesce<T>(T Obj, params T[] Args)
{
if (Obj != null && IsDBNull(Obj))
{
return Obj;
}
else
{
foreach (T arg in Args )
{
if (arg != null && IsDBNull(arg)) return arg;
}
}
return null;
}
public static Boolean IsDBNull(Object Value)
{
return DBNull.Value.Equals(Value);
}
}
Error 1 Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead. C:\Users\carl.symington\Documents\Vi sual Studio 2012\Projects\DataServices \DataServi ces\DataSe rvices.cs 24 20 DataServices
You also need to change (as indicated by the error)
so the fuil method would read:
return null;
for return default(T);
so the fuil method would read:
public static T Coalesce<T>(T obj, params T[] args)
{
if (obj != null)
return obj;
foreach (T arg in args)
{
if (arg != null)
return arg;
}
return default(T);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent solution Eric, you have no idea how useful I have found a proper Coalese has been in a tight corner.
Open in new window