Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Indexed Properties

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hufaunder@yahoo.com - 13 Feb 2007 18:26 GMT
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
Peter Bromberg [C# MVP] - 13 Feb 2007 18:55 GMT
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

Signature

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

> I wonder about the pros and cons of a setter for an index property. As
> an example let's look at the Dataset
[quoted text clipped - 10 lines]
>
> Thanks
hufaunder@yahoo.com - 13 Feb 2007 19:30 GMT
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
Tom Porterfield - 13 Feb 2007 20:59 GMT
> Your right, that should have read
>
[quoted text clipped - 8 lines]
> 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.
Signature

Tom Porterfield


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.