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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:43:07+00:00 2026-05-23T12:43:07+00:00

Say I have a C# Nullable DateTime? property that needs to be consumed by

  • 0

Say I have a C# Nullable DateTime? property that needs to be consumed by VBA through COM.

    public DateTime? TestDate {
        get ; set;
    }

Unfortunately, Nullables are not visible through COM, so I would like to have the property return a something that will be seen as a Variant from VBA.

Unfortunately, I’m not sure how this should be written.

I tried using an object and a dynamic instead of DateTime?: while I can get the property value, I can’t set it (I get Run-Time error ‘424’ Object required errors from VBA).

Note: I don’t have issues with making my library COM Visible: everything works fine and I’m able to use .Net types from VBA without problem, except for this particular issue.

Thanks for any pointers.

EDIT: I found an interesting page describing the default marshalling for objects, but I can’t seem to explain why I can’t set my property if it’s declared as object.
I’m missing something.

  • 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-23T12:43:07+00:00Added an answer on May 23, 2026 at 12:43 pm

    Here what I did to go around the issue.

    Object properties

    public object MyObject {
        get ; set;
    }
    

    When using a .Net Object property from VBA, reading the property is no problem and it will be correctly seen as a Variant.
    Unfortunately, trying to set the property from VBA will fail.

    However, using plain methods will work fine:

    private object _MyObject;
    
    public object GetMyObject() {
        return _MyObject;
    }
    
    public void SetMyObject(object value) {
        if (value == DbNull.Value)
            value = null;   
        _MyObject = value;
    }
    

    The check for DBNull is to get around the problem that VBA’ Null is actually marshalled as DBNull to .Net.

    DateTime?

    Now, to make nullable DateTime? work, we can do something like:

    private DateTime? _MyDate;
    
    public object GetMyDate() {
        return _MyDate
    }
    
    public void SetMyDate(object value) {
        if (value == null || value == DbNull.Value)
            _MyDate = null;   
        else
            _MyDate = (DateTime?)value;
    }
    

    And in VBA, we can hide these get/set in properties (assuming we have an existing instance of our class in myclassinstance):

    Public Property Get MyDate() As Variant
        MyDate = myclassinstance.GetMyDate()
    End Property
    
    Public Property Set MyDate(value as Variant)
        myclassinstance.SetMyDate value
    End Property
    

    A more generic way

    This is a bit ugly since our C# class is exposing MyDate as GetMyDate/SetMyDate methods instead of properties.
    To implement this in a more generic way so the mechanism is usable for all properties in our class, we can use a Dictionary as a backing store:

    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class MyClass {
    
        private Dictionary<string,object> backingStore = new Dictionary<string,object>();
    
        public object GetPropertyValue(string propertyName) {
            if (backingStore.ContainsKey(propertyName))
                return backingStore[propertyName];
            else
                return null
        }
    
        public void SetPropertyValue(string propertyName, object value) {
            if (value == DBNull.Value) value = null;
            if (backingStore.ContainsKey(propertyName))
                backingStore[propertyName] = value;
            else
                backingStore.Add(propertyName, value);
        }
    
        [ComVisible(false)]
        public DateTime? MyDate {
            get {
                return GetPropertyValue(@"MyDate") ?? default(DateTime?);
            }
            set {
                SetPropertyValue(@"MyDate", value);
            }            
        }
    }
    

    The ComVisible(false) attribute ensures that the properties are not visible from VBA.

    And in VBA, we declare the properties:

    Public Property Get MyDate() As Variant
        MyDate = myclassinstance.GetPropertyValue("MyDate")
    End Property
    
    Public Property Set MyDate(value as Variant)
        myclassinstance.SetPropertyValue "MyDate", value
    End Property
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this class: class myclass { public int Field1{ get; set; }
I have a property like so public decimal? MyProperty { get; set; } ,
Let's say that I have the methodInfo for something like Nullable<int>.HasValue . Is there
Say I have a class named Frog, it looks like: public class Frog {
Let's say I have a MySql stored procedure that inserts a record with some
Say I have a method like this: public void SaveData() { try { foreach
Say I have a set (or graph) which is partitioned into groups. I am
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
Say I have a function such as: public TProperty Foo<TClass, TProperty>(TClass instance, Expression<Func<TClass, TProperty>>
For example, let's say i have a string property Document.DocumentType.DocumentCode with DocumentCode is a

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.