Link to home
Create AccountLog in
Avatar of Alyanto
AlyantoFlag for United Kingdom of Great Britain and Northern Ireland

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.

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

Open in new window


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

Open in new window


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\Visual Studio 2012\Projects\DataServices\DataServices\DataServices.cs      11      26      DataServices

I suspect it is to do with where I have tried to put T? making T a nullable object.
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Hi,

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

Open in new window

Avatar of Alyanto

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\Visual Studio 2012\Projects\DataServices\DataServices\DataServices.cs      24      20      DataServices
Avatar of Alyanto

ASKER

Just to be clear the C# version of IsDBNull is

        public static Boolean IsDBNull(Object Value)
        {
            return DBNull.Value.Equals(Value);  
        }

Open in new window

Hi,

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

Open in new window

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
Avatar of Alyanto

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

Open in new window


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\Visual Studio 2012\Projects\DataServices\DataServices\DataServices.cs      24      20      DataServices
You also need to change (as indicated by the error)
return null;

Open in new window

for
return default(T);

Open in new window


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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Alyanto

ASKER

Excellent solution Eric, you have no idea how useful I have found a proper Coalese has been in a tight corner.