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.
LVL 1
AlyantoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
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

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

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Lokesh B RDeveloperCommented:
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

0
Éric MoreauSenior .Net ConsultantCommented:
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
0
AlyantoAuthor 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
0
Éric MoreauSenior .Net ConsultantCommented:
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

0
Éric MoreauSenior .Net ConsultantCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlyantoAuthor Commented:
Excellent solution Eric, you have no idea how useful I have found a proper Coalese has been in a tight corner.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.