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 7181305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:31:47+00:00 2026-05-28T17:31:47+00:00

Is there a way which works? Of course the following code doesn’t work: :-)

  • 0

Is there a way which works? Of course the following code doesn’t work: 🙂

There are multiple types of DataFields: int, string, DateTime, double, MyOwnType

The DataFields themselves are wrapped into a generic class and the type of them is of type T. All the T’s, which are value types, are nullable, though (int?, DateTime?, double?). That’s for storing reasons for our database.
If a value is null we don’t create a new record for that data field in our database.

public void SetDataFieldValue<T>(string fieldName, T value)
{
    DataField<T>.Value = (T)(object)value;
}

That works perfectly fine with the nullable T’s. But if we call it like

SetDataFieldValue<int>("NumberOfChildren", value)

it won’t work, because DataField<int> does not exist. So I need something like this (we just need that for empty inputs):

public void SetDataFieldValue<T>(string fieldName, T value)
{
    var defaultValue = default(T);
    if (defaultValue.GetType().IsValueType)
    {
        // This T? just works with value types.
        // Of I set a constraint where T: struct this would work.
        // So the if statement above is not enough to tell the compiler,
        // that in here only value types of T are handled
        DataField<T?>.Value = // whatever 
    }        
    if (defaultValue.GetType().IsReferenceType)
    {
        DataField<T>.Value = // whatever 
    }
}

So if you call it with int? you determine that you don’t want to store the value if it’s null. If you call it with int instead it’s gonna be stored with 0 (a record is created).

Please don’t suggest things like changing the design of our DataFields. One reason for not creating a new DB record, if the value is null, is, that null has been defined as “no information” (null children of a family is not a intentionally set field). “0” is information though (0 children).

UPDATE

Sorry. I forgot to mention that we have a public T GetDataFieldValue<T>(string fieldName), as well. And there we can’t determine just the type of the value.

The definition of our data fields says, if our data field is nullable, the value of it should be nullable as well. That should work for not nullable (actually not existing) data fields, as well.

So if we say GetDataField<int> we want to return (int)DataField<int?>.Value.

But we can’t cast it with (int) if the value is null. So we do it like this:

(T is int)

if (DataField<int?>.Value == null)
{
    return default(T);
}
return (int)DataField<int?>.Value

The only way I see at the moment is that we call this method like GetFieldValue<int?, int>(name) if we want to get back a not nullable integer. Else we call GetFieldValue<int?, int?> for getting back a nullable integer.

Obviously there is no way to create overloads with constraints? So the first method has where T: struct and the overload where T: class?

  • 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-28T17:31:48+00:00Added an answer on May 28, 2026 at 5:31 pm

    Why not just call it with Nullable<Int>?

    SetDataFieldValue<Nullable<int>>("NumberOfChildren", value)

    EDIT:

    GetDataFieldValue<T>(string fieldName) can also be called with Nullable. To get the value of T, use typeof(T):

    public T GetFieldValue<T>(string fieldName) {
        var theTypeOfT = typeof(T);
    }
    // Call this using Nullable<T>:
    
    var someNullableInt = GetFieldValue<Nullable<T>>("foo");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a build-in way in C# to work with simple config files which
EF4.1-Code-First-Gurus! I wonder if there is a more elegant way to handle the following
in Microsoft Access, is there a way which I can programatically set the Confirm
Is there any way by which we can export the result of a select
Is there a way by which we can simulate thread level constants in C++?
is there any way via which we can interact to iphone scheduler and insert
Is there a way in which the http connection and tcp connection listeners can
Is there a way to select which TestMethods you want to execute in Visual
Is there a way to get which JSP is currently rendered, with JSTL or
Is there a way to specify which namespace includes should be automatically added any

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.