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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:41:18+00:00 2026-05-23T04:41:18+00:00

I have a field with a custom attribute on it, that looks like this:

  • 0

I have a field with a custom attribute on it, that looks like this:

[DBDataTypeAttribute(FieldType = "varchar(1000)",AllowNulls=false)]
public String ERLineID;

I’ve got some code that looks for this, like so:

foreach (FieldInfo field in userType.GetFields())
{
    currentDefaultDataType = createtabledefaultdatatype;
    currentDefaultAllowNulls = createtabledefaultallownulls;

    DBDataTypeAttribute attribute =
        (DBDataTypeAttribute)Attribute.GetCustomAttribute(field, typeof(DBDataTypeAttribute));

    if (attribute != null)
    {
        // at least one of createtabledefaultdatatype or createtabledefaultallownulls exists
        if (attribute.FieldType != null)
        {
            currentDefaultDataType = attribute.FieldType;
        }
        if (attribute.AllowNulls != null)
        {
            currentDefaultAllowNulls = attribute.AllowNulls;
        }
...

The value createtabledefaultallownulls comes from a config file and is set to true. However I’ve found when I leave off the ‘allownulls’ in my attribute, it overwrites my ‘true’ default with a value of false.

If I specified only the FieldType in my attribute, I would have expected ‘if (attribute.AllowNulls != null)’ to evaluate false. Instead, it says that the value isn’t null and says that attribute.AllowNulls is false. So it overwrites my default even though no value was specified. Is this something to do with bools not being nullable? Or perhaps if you add an attribute you must explicity define all the parameters within it?

The workaround I’m considering is just making it a string and bool.parsing it, but that doesn’t sound very elegant! Is there a better way?

  • 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-23T04:41:19+00:00Added an answer on May 23, 2026 at 4:41 am

    You need a third state other than true of false, and that would be unset.

    Change AllowNulls to a Nullable boolean, bool?. AllowNulls will then allow the values of null, true, or false. If the user does not specify it in the attribute it will be initialized to null. So your attribute.AllowNulls != null will do exactly what you want and users will still be able to set the value to true or false.

    Ok, so you can’t use nullables on Attributes. Didn’t know that. You could use something like below rather than changing over to a string, although it isn’t that pretty:

     private bool? allowNulls = null;
     public bool AllowNulls  {
        get { return allowNulls.GetValueOrDefault(false); }
        set { allowNulls = value; }
     }
     public bool AllowNullsWasSet() { return allowNulls.HasValue; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a source object that looks like this: private class SourceObject { public
I have a custom Attribute class that I defined as: [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] public
I have a custom attribute that is being filled from a database. This attribute
I develop a custom control that have some field like below: ControlKind, Field1 ,
Does anyone have any good links for developing custom field and content types that
I have an xml string like this and I want to get attribute value
It looks like in edit and add forms all field have fixed width. I
I have a custom field called HourField, which lets you enter 1:30 to represent
I wanted to use the custom field for wordpress to have a different header
I have a custom list_display field which is responsible for a column of integers

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.