C# Generics - Single Function to pass in a type double and string and have it return a null

I have 2 functions below where I've removed the meat of the work in this equation for simplicity.  The 'meat' is the same for both functions.  I want a single function that I can use for both nullable and non-nullable types.  If thats not possible then at least 1 function that will contain the actual logic and one function just calls another but for some reason I can't get it going.  The answer most likely will be in the T declaration or the constraints.  Just can't get it to do exactly what I want.

The FindEntryNull function is used where the objects can be null (string for example) where the FindEntry is used where the type can't be null (double for example).

Any help is appreciated.
public Nullable<T> FindEntry<T>(string key, object OneBasedIndex, Nullable<DateTime> asOfDate) where T : struct, IConvertible
        {
                     string item = FindEntry<string>(key);
            if (null == item)
                return null;
            return (T)Convert.ChangeType(item, typeof(T)); 
        }

        public T FindEntryNull<T>(string key, object OneBasedIndex, Nullable<DateTime> asOfDate) where T : class, IConvertible
        {
             string item = FindEntry<string>(key);
            if (null == item)
                return null;
            return (T)Convert.ChangeType(item, typeof(T)); 
        }

Open in new window

nitecowAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

lazyberezovskyCommented:
Well, returning null is not very good style of programming - you give NullReferenceException headache to users of your code.

Consider following code:
public bool TryGetEntry<T>(string key, out T value) where T : IConvertible
{
    string item = FindEntry<string>(key);

    if (null == item)
    {
        value = default(T);
        return false;
    }

    value = (T)Convert.ChangeType(item, typeof(T));
    return true;
}

Open in new window

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
w00teCommented:
I would think that Nullable<yourtype> would be part of thet type in the first place.  You could just pass Nullable<int> as the parameter to your template when you use it, then you woudlnt need two different templates at all because you could call myTemplate<Nullable<Type>> or myTemplate<Type> depending on if you wanted it to be nullable (you can pass templated types as parameters into templates).
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
C#

From novice to tech pro — start learning today.