PC Review


Reply
Thread Tools Rate Thread

Indexed Properties

 
 
hufaunder@yahoo.com
Guest
Posts: n/a
 
      13th Feb 2007
I wonder about the pros and cons of a setter for an index property. As
an example let's look at the Dataset

DataSet ds;
.....
ds.Tables[0] = new Table(); //Assume Tables[0] exists already

The above will generate a compile error. Why is this allowed? Tables
basically looks like an array/list to the user and arrays and lists
allow this assignment. What is the logic behind this limitation? What
are the pros/cons in general, i.e. not just for the above example but
any custom class that has an indexed property?

Thanks

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      13th Feb 2007
I'm not exactly sure what this issue is here, but the correct object is not
"Table", its new "DataTable()". The preferred construct would be
ds.Tables.Add(yourDataTable);

Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"(E-Mail Removed)" wrote:

> I wonder about the pros and cons of a setter for an index property. As
> an example let's look at the Dataset
>
> DataSet ds;
> .....
> ds.Tables[0] = new Table(); //Assume Tables[0] exists already
>
> The above will generate a compile error. Why is this allowed? Tables
> basically looks like an array/list to the user and arrays and lists
> allow this assignment. What is the logic behind this limitation? What
> are the pros/cons in general, i.e. not just for the above example but
> any custom class that has an indexed property?
>
> Thanks
>
>

 
Reply With Quote
 
hufaunder@yahoo.com
Guest
Posts: n/a
 
      13th Feb 2007
Your right, that should have read

ds.Tables[0] = new DataTable().

I know you can add a table with ds.Tables.Add. But if I want to
replace(!) a table I'll have to remove it first and then add the new
one rather then just replace it like in the statement above. So what
is the logic behind this limitation?

Again, note that I just take the DataSet as an example. The reason I
am asking is because I wonder about what should be offered in a custom
class.

Thanks

 
Reply With Quote
 
Tom Porterfield
Guest
Posts: n/a
 
      13th Feb 2007
(E-Mail Removed) wrote:
> Your right, that should have read
>
> ds.Tables[0] = new DataTable().
>
> I know you can add a table with ds.Tables.Add. But if I want to
> replace(!) a table I'll have to remove it first and then add the new
> one rather then just replace it like in the statement above. So what
> is the logic behind this limitation?
>
> Again, note that I just take the DataSet as an example. The reason I
> am asking is because I wonder about what should be offered in a custom
> class.


I think part of the reason the DataTableCollection indexer is read-only is
that it isn't always safe to remove a table from the collection. The proper
way to remove a table would be to call CanRemove first and then Remove.
This is due to the relational nature of tables in a dataset. I would think
that if the table at [0] can't be removed then it could lead to confusion as
to why the assigment of ds.Tables[0] = new DataTables() failed.

In your custom class, if it is always safe to remove items then I see no
reason to make the indexer read-only.
--
Tom Porterfield

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Indexed Properties hufaunder@yahoo.com Microsoft C# .NET 3 13th Feb 2007 09:59 PM
Indexed properties? =?Utf-8?B?RGFuaWVsIE1pbGxlcg==?= Microsoft C# .NET 2 15th Jun 2005 04:00 PM
Indexed properties in .NET Kor Microsoft Dot NET 2 8th Dec 2004 06:55 AM
Overloaded indexed properties =?Utf-8?B?TWljaGFlbEdlaWVy?= Microsoft VC .NET 4 20th Sep 2004 04:25 PM
Why are there no indexed properties? Russell Bearden Microsoft C# .NET 8 8th Feb 2004 06:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:56 PM.