1

I have a desktop app which uses Serialization and saves data before closing and load same data from file using this code:

IFormatter formatter = new BinaryFormatter();
var array = (MyClass[])formatter.Deserialize(stream);

MyClass has a datime property and its field. I need to make it nullable but i got Value cannot be null.\r\nParameter name: value InnerException when i made them both nullable like that.

    public DateTime? MyDate
    {
        get
        {
            return myDate;
        }
        set
        {
            if (myDate != value)
            {
                myDate = value;
            }
        }
    }

Then, I updated my code as follows

    public DateTime? MyDate
    {
        get
        {
            return myDate;
        }
        set
        {
            if (value != null && myDate != value)
            {
                myDate = value;
            }
        }
    }

Error occured in deserialization process as before. Then i had to make field non nullable and change property like as follows

            public DateTime? MyDate
    {
        get
        {
            if (myDate == DateTime.MinValue)
            {
                return null;
            }
            else
            {
                return myDate;
            }
        }
        set
        {
            if (myDate != value)
            {
                if (value == null)
                {
                    myDate = DateTime.MinValue;
                }
                else
                {
                    myDate = (DateTime)value;
                }

                onPropertyChanged("MyDate");
            }
        }
    }

That workaround worked as i expected but i cant understant why second code does not work for DateTime while same code works for nullable decimal properties. what is difference beetween field and propery when they have value of null?

onur demir
  • 421
  • 7
  • 13
  • 2
    is your myDate field also nullable? – asidis Jun 14 '18 at 09:10
  • I made it nullable in fırst and second tıme but last i had to made it non nullable. – onur demir Jun 14 '18 at 09:23
  • I did a test locally with nullable property and field and it works as expected. There is probably something else provoking your exception. How your code differs from: https://gist.github.com/avitsidis/9d172a59da326d1d78015687a2be1e06 ? – asidis Jun 14 '18 at 09:30
  • 1
    BinaryFormatter never serializes properties, only fields. So the change you made is breaking, be sure to delete all of the old serialized data. – Hans Passant Jun 14 '18 at 10:25
  • @asidis i tried the code and it worked but i read from a file there might be differences. @ hans i also deleted old files and started again. when i create file with nullable datetime field it cant read it. – onur demir Jun 14 '18 at 13:08
  • 1
    @onurdemir can you provide us more inputs so that we can reproduce your issue? – asidis Jun 14 '18 at 13:58
  • Thank so much for your helpfulness. I have to go vacation in an hour.I will try to reproduce it separately from the solution and share here. if i cant i will provide more details next monday. – onur demir Jun 14 '18 at 14:20

1 Answers1

0
if (myDate != value)
{
     myDate = value;
}

Might be your problem, not really sure, but performance wise it's not smart to do this anyway because setting the value requires less processing power than to compare it.

So why not make this:

public DateTime? MyDate { get;set; }

Let me know if that fixes your problem!

anothernode
  • 5,100
  • 13
  • 43
  • 62
  • the if block is probably there to avoid notifying property changed (onPropertyChanged("MaturityDate"); in the last sample of code) – asidis Jun 14 '18 at 09:29
  • Ye but I wonder if he placed it as a debug code or an attempt of understanding why things are going wrong. Or if it's there for different purposes. – Davey van Tilburg Jun 14 '18 at 09:33
  • @onurdemir if you actually use this code, I might suggest that you use a different class that models your serialization object to the file. And a wrapper class (for example) that adds functionality like notifying when a property has changed. Just to make it less complicated when you are focusing on the serialization aspect of your object. You don't have to debug code that has nothing to do with the serialization purpose of the object. – Davey van Tilburg Jun 14 '18 at 09:35
  • I updated the code onPropertyChanged("MyDate"); i did not wrote it for debug purposes. i am not sure it will work same as when i use autoproperty – onur demir Jun 14 '18 at 09:37
  • 1
    `but performance wise it's not smart to do this anyway because setting the value requires less processing power than to compare it` that's irrelevant. The comparison is due to the typical WPF/MVVM/INotifyPropertyChange pattern. It's not about performance. Besides, if you are calling your properties in a way such that this becomes relevant I think you'll have other performance issues to handle ;-) – Stefan Jun 14 '18 at 09:41