copy list to object : list

R

rodchar

Hi All,

I have a simple console app listed in its entirety below. I was just
wondering why I can't copy a list from point A to point B?

namespace PracticeCopyListToObject
{
class Program
{
static void Main(string[] args)
{
ReceiptList list2 = new ReceiptList();

List<Receipt> list = new List<Receipt>();
list.Add(new Receipt { InvoiceDate = "10/29/2009", ReceiptId = 2
});
list.Add(new Receipt { InvoiceDate = "10/28/2009", ReceiptId = 1
});

list2 = list; //how would i copy to my object
}
}

public class Receipt
{
public int ReceiptId { get; set; }
public string InvoiceDate { get; set; }
}

public class ReceiptList : List<Receipt>
{
public ReceiptList()
{

}
}
}


thanks,
rodchar
 
F

Family Tree Mike

rodchar said:
Hi All,

I have a simple console app listed in its entirety below. I was just
wondering why I can't copy a list from point A to point B?

namespace PracticeCopyListToObject
{
class Program
{
static void Main(string[] args)
{
ReceiptList list2 = new ReceiptList();

List<Receipt> list = new List<Receipt>();
list.Add(new Receipt { InvoiceDate = "10/29/2009", ReceiptId = 2
});
list.Add(new Receipt { InvoiceDate = "10/28/2009", ReceiptId = 1
});

list2 = list; //how would i copy to my object
}
}

public class Receipt
{
public int ReceiptId { get; set; }
public string InvoiceDate { get; set; }
}

public class ReceiptList : List<Receipt>
{
public ReceiptList()
{

}
}
}


thanks,
rodchar

Do you really mean copy the list, or just set the references equal? Your
code as shown would give an error regarding a cast problem, which is fixed by:

list2 = (ReceiptList) list;

It's not clear if that is what you want however.

Mike
 
P

Peter Duniho

Family said:
Do you really mean copy the list, or just set the references equal? Your
code as shown would give an error regarding a cast problem, which is fixed by:

list2 = (ReceiptList) list;

No, that won't work. The "list" variable was initialized as an instance
of "List<Receipt>". It's not an instance of "ReceiptList" and in the
code provided there's no explicit conversion implemented, so the cast
will fail (throw an exception).
It's not clear if that is what you want however.

I agree. The question needs to be more specific. But, assuming the
question is how to simply assign the reference value of "list" to the
variable "list2", the only way to do that is to have "list" be an
instance of "ReceiptList" in the first place. Then it can be
successfully cast as you've suggested.

Actually copying is also possible, if it turns out that's what the OP
meant. It's just a matter of passing the "list" reference to the
constructor of ReceiptList that supports that. Of course, it would have
to be added to the ReceiptList class:

class ReceiptList : List<Receipt>
{
public ReceiptList(IEnumerable<Receipt> list) : base(list) { }

// ...
}

then:

list2 = new ReceiptList(list);

Now, all that said, I have found that inheriting the generic collection
classes is usually not that great an idea anyway. There's usually so
little added benefit, and to make the inherited class fully useful, you
have to add so much stuff to it (such as copying a specific constructor,
as above), and even doing that you often run into situations where you
lose the type information (and so have to cast it back later), or you
find yourself dealing with an instance of the base type instead (as
above, thus requiring a complete copy of the object to recover the
type). Not to mention the problems that come up if you start trying to
hide base class members in order to add functionality to them.

The OP isn't clear on why he's doing this, but probably the best answer
to the question is "don't create a class that inherits List<Receipt>".

Pete
 
R

rodchar

Thanks all for the insight. My intent was to copy the contents of the list
not reference it.
-rod.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top