0

I have generic list:

 class BooksRegister <T>
    {
        private T[] Register;
        public int Count { get; set; }

       public BooksRegister()
        {
            Register = new T[100];
            Count = 0;
        }

        public void Add(T value)
        {
            if (Count >= 100)
            {
                return;
            }
            Register[Count] = value;
            Count ++;
        }
}

then object class:

class Book
    {
        public String Author { get; set; }
        public String Title { get; set; }
        public int Quantity { get; set; }

        public Book(String aut, String pav, int kiek)
        {
            this.Author = aut;
            this.Title = pav;
            this.Quantity = kiek;
        }

        public override string ToString()
        {
            return Author + " \"" + Title + "\" " + Quantity;
        }
    }

Then goes my Data class where I am reading information from file. I need to implement lazy initialization of object but when I do so I can't store my object in List.

public static void ReadBooks(BooksRegister<Book> allBooks)
        {
            StreamReader sr = new StreamReader("ListOfBooks.txt");

            string line = "";
            while ((line = sr.ReadLine()) != null)
            {
                string[] words = line.Split('|');

                String tempAuthor = words[0];
                String tempTitle = words[1];
                int quant = Convert.ToInt32(words[2]);

                Lazy<Book> tempas = new Lazy<Book>();

                tempas.Value.Author = tempAuthor;
                tempas.Value.Title = tempTitle;
                tempas.Value.Quantity = quant;

                allBooks.Add(tempas); // error here
}

How can I solve this problem? I have to use lazy initialization necessarily

uvytautas
  • 518
  • 8
  • 18
  • have you tried "allBooks.Add(tempas.Value)"? – dkackman Oct 25 '14 at 11:50
  • no, that works. But now I get Missing member exception: The lazily-initialized type does not have a public, parameterless constructor. – uvytautas Oct 25 '14 at 11:55
  • 1
    What's lazy about that when you immediately ask for a value of the newly created object by tampas.Value on the last line? – Ondrej Janacek Oct 25 '14 at 11:59
  • @OndrejJanacek I am new to Lazy object initialization, but I have to use this feature in my project. I am searching the way I can do it best – uvytautas Oct 25 '14 at 12:01

1 Answers1

0

If you must use lazy there are 2 ways:

You change you lazy initialization code with:

Lazy<Book> tempas = new Lazy<Book>(() => new Book(tempAuthor, tempTitle, quant));
allBooks.Add(tempas.Value);

What it does is defines an expression on how to initialize the book. This is a bad approach because you initialize lazy object on line one, and you initialize it on the second line, which basically makes using Lazy<Book> useless.

Another approach would be to change the method signature to

public static void ReadBooks(BooksRegister<Lazy<Book>> allBooks)

In this case your lazy initializing code would look like this:

Lazy<Book> tempas = new Lazy<Book>(() => new Book(tempAuthor, tempTitle, quant));
allBooks.Add(tempas);

One thing that is missing in this case is how to access Book in BooksRegister, as now it is write only object - you can add value, but there is no way to read it from outside the class.

dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • Oh my bad, everythings ok. But do you have any other ideas how to make this lazy initialization more reasonably used ? @dotnetom – uvytautas Oct 25 '14 at 12:33
  • @user3662708 Lazy initialization makes sense where you have an object that is expensive to load and you only want to do it just before it used. In your case if you had method `ReadBooks` wrapped in some kind of repository object, it could make sense to use `Lazy` and initialize and load all the data only when it is needed. – dotnetom Oct 25 '14 at 14:08
  • what do you mean by `repository object` ? – uvytautas Oct 25 '14 at 14:47