Avatar of Alyanto
Alyanto
Flag 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.
Visual Basic.NETC#.NET Programming

Avatar of undefined
Last Comment
Alyanto

8/22/2022 - Mon
Lokesh B R

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

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Lokesh B R

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 Moreau

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
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Éric Moreau

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
Éric Moreau

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Alyanto

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