-5
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using NodaTime;

namespace MyApp
{
    public partial class MainForm : Form
    {
        public class Foo
        {
            private LocalDateTime date_time;

            public Foo(string data)
            {
                Int32 i;
                char[] delimiters = { ',', '/', ':' };
                string[] tokens = data.Split(delimiters);

                if( Int32.TryParse(tokens[0], out i ))
                {
                    date_time.Month = i;
                }
            }
        };

        public MainForm()
        {
            InitializeComponent();
        }
    }
}

The line where I set date_time.Month to i is where I get the error called out in the title - Property or indexer cannot be assigned to -- it is read only. I've searched through many of the similar posts, but wasn't able to find a solution. Any help would be greatly appreciated. Thanks in advance!

sparky
  • 3
  • 2
  • What solution do you want? Reading the documentation? This property IS read only. – TomTom Apr 18 '16 at 06:08
  • You can't modify the value by changing its `Month` property, try creating a new instance. – Cheng Chen Apr 18 '16 at 06:10
  • It doesn't make sense *logically* to change a date. A date and time is unchanging. You need to create a new representation of a new date. – Rob Apr 18 '16 at 06:13
  • If you can tell us what you're attempting to do, I'm sure we can help you. – Jon Skeet Apr 18 '16 at 16:09
  • I'm just trying to create a member variable in my class, of type "LocalDateTime", and then I want to assign the various fields to it (month, day, etc). I thought the way I did it was creating a new instance, but I must be missing something. – sparky Apr 18 '16 at 23:28
  • I'm reading lines of data from a database, and each line has a date/time stamp. So I'm trying to store the date and time for each data point into my class data. – sparky Apr 18 '16 at 23:37
  • 1
    So create a new `LocalDateTime`, passing in the year/month/day/hour/minute/second. Most types in Noda Time are immutable. Note that if you've got a date and time in a fairly regular text format, Noda Time provides text handling facilities anyway. – Jon Skeet Apr 19 '16 at 05:43
  • I guess I'm confused - I thought I was creating a new LocalDateTime with the statement "private LocalDateTime date_time;". If you could point me in the right direction as far as how to create a new LocalDateTime member in my class, that would be great. This is the part I haven't been able to figure out. Thanks for your help! – sparky Apr 19 '16 at 15:28

1 Answers1

1

If LocalDateTime was mutable, you could do this:

LocalDateTime x = new LocalDateTime();
x.Year = 2016;
x.Month = 4;
x.Day = 20;
x.Hour = 11;
x.Minute = 30;

But, it's not mutable. It's immutable. Therefore, you have to do this:

LocalDateTime x = new LocalDateTime(2016, 4, 20, 11, 30);

In your code, you've defined a field date_time in your class, and declared it as a LocalDateTime type, but you've never assigned a value to it. Since this type is a struct, is will be initialized with its default value, which for LocalDateTime is 1970-01-01 00:00:00.

You then attempt to set a property as if the value was mutable, but it's immutable so there is no setter. Instead, you need to assign a new instance of the LocalDateTime structure to the field, similarly to how I showed above.

Also recognize that Noda Time has extensive parsing abilities built in, so there's no need to split strings into components or to try parsing them as integers. For example:

using NodaTime;
using NodaTime.Text;

...

var pattern = LocalDateTimePattern.CreateWithInvariantCulture("M/d/yyyy HH:mm:ss");
var result = pattern.Parse("4/20/2016 11:30:00");
if (!result.Success)
{
    // handle error
}

LocalDateTime ldt = result.Value;

If you're going to be parsing a lot of values, you can hold on to the pattern instance in a static variable to avoid having to create it every time. This provides better performance than the equivalent BCL methods like DateTime.ParseExact.

Matt Johnson-Pint
  • 197,368
  • 66
  • 382
  • 508