Converting a piece of code from VB to C#

Alyanto
Alyanto used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lokesh B RDeveloper
Top Expert 2015

Commented:
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

Author

Commented:
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

Author

Commented:
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

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Lokesh B RDeveloper
Top Expert 2015

Commented:
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

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
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

Author

Commented:
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
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
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

Senior .Net Consultant
Top Expert 2016
Commented:

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial