Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 782517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:23:53+00:00 2026-05-14T20:23:53+00:00

I alredy have this: public static object GetDBValue(object ObjectEvaluated) { if (ObjectEvaluated == null)

  • 0

I alredy have this:

public static object GetDBValue(object ObjectEvaluated)
 {
  if (ObjectEvaluated == null)
   return DBNull.Value;
  else
   return ObjectEvaluated;
 }
used like:

 List<SqlParameter> Params = new List<SqlParameter>();
 Params.Add(new SqlParameter("@EntityType", GetDBValue(EntityType)));

Now i wanted to keep the same interface but extend that to use it with nullable

 public static object GetDBValue(int? ObjectEvaluated)
 {
  if (ObjectEvaluated.HasValue)
   return ObjectEvaluated.Value;
  else
   return DBNull.Value;
 }

 public static object GetDBValue(DateTime? ObjectEvaluated)
 {...}

but i want only 1 function GetDBValue for nullables. How do I do that and keep the call as is is? Is that possible at all?

I can make it work like:

 public static object GetDBValue<T>(Nullable<T> ObjectEvaluated) where T : struct
 {
  if (ObjectEvaluated.HasValue)
   return ObjectEvaluated.Value;
  else
   return DBNull.Value;
 }

But the call changes to:

Params.Add(new SqlParameter("@EntityID ", GetDBValue<int>(EntityID)));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:23:53+00:00Added an answer on May 14, 2026 at 8:23 pm

    The generic version that you have is fine, although you can shorten it even more with the null-coalescing operator:

    public static object GetDBValue<T>(Nullable<T> ObjectEvaluated)
        where T : struct
    {
        return ObjectEvaluated ?? (object)DBNull.Value;
    }
    

    What you might be missing is that you don’t need to use the generic parameter when you call it; C#’s type inference will figure it out for you:

    int? x = null;
    object y = GetDBValue(x);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method: public static IEnumerable<T> ExecuteReaderSp<T>(string sp, string cs, object parameters) where
I have a class called Paragens like this: public class Paragens { static int
i have a method in a class like this: public static void postEvents(List<RuleEvent> eventList)
Is there any point in doing this? public static void Write<T>(T value) { textWriter.Write(value.ToString());
I have the following method signature: public static void InvokeInFuture(Delegate method, params object[] args)
How do I make code like this thread safe? public static class Converter {
I have a custom html helper: public static MvcHtmlString MyLink(this HtmlHelper htmlHelper, string linkText,
I have something like this: public void Delete(T entity) { Context.DeleteObject(entity); Context.SaveChanges(); } I
While executing this method: public static List<T> ToList<T>(DataTable dataTable) { Type type = typeof(T);
So I have this textbox on the page: <%= Html.TextBox(EffectiveDate, Model.EffectiveDate.HasValue ? Model.EffectiveDate.Value.ToString(dd-MMM-yyyy) :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.