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 / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Replacement for CTypedPtrList

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Wells - 13 Feb 2008 20:14 GMT
I am converting a C++ project management program I wrote to C#. Each Protask
obect represent a task and includes two CTypedPtrList objects: the lists of
pointers to the prerequisite tasks and dependent tasks. There doesn't seem
to be an equivalent to the CTypedPtrList in C#. I'm not sure if ArrayList or
a List(T) generic would best fill the requirement. I am guessing that
regardless of which I use, I will need to use the unsafe keyword to use
pointers. Would a List(T) generic list be safer than an ArrayList?
Any thoughts would be most appreciated.
Bob
Marc Gravell - 13 Feb 2008 20:46 GMT
I'm not familiar with CTypedPtrList, but from your description it
sounds like List<T>, where T is a class (not a struct):

* each item is a reference
* each item is strongly typed

i.e.
      public sealed class Protask {
           private readonly List<Protask>
               preRequisites = new List<Protask>(),
               dependents = new List<Protask>();

           public List<Protask> PreRequisites { get { return
preRequisites; } }
           public List<Protask> Dependents { get { return
dependents; } }

       }

Of course, you might want to encapsulate the lists a bit more if you
want editing one to automatically update the other end (if you see
what I mean).

Marc
Robert Wells - 13 Feb 2008 21:27 GMT
Hello Marc,

Thank you for your reply. Can T be a pointer to a class?

> I'm not familiar with CTypedPtrList, but from your description it
> sounds like List<T>, where T is a class (not a struct):
[quoted text clipped - 20 lines]
>
> Marc
Nicholas Paldino [.NET/C# MVP] - 13 Feb 2008 21:42 GMT
Robert,

   You don't have pointers to classes in .NET, you have references (there
is a difference).  But yes, T can be a reference type, where the List<T> is
the list of reference types.  This means that when you access an item from
the List<T>, you will get a reference that points to the same item that the
reference in List<T> points to (whereas if T was a struct, you would get a
copy on return because of the copy semantics of structures on assignment).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello Marc,
>
[quoted text clipped - 24 lines]
>>
>> Marc
Robert Wells - 14 Feb 2008 00:11 GMT
Hello Nicholas,

I'm still not sure if I understand this correctly.
Does a List<T> object holds a bunch of objects of type T or
Does it only hold the references (addresses?) of those objects?

I certainly don't want each task to hold copies of all its prerequisites and
dependent tasks.

Thanks,
Bob

> Robert,
>
[quoted text clipped - 34 lines]
>>>
>>> Marc
Jon Skeet [C# MVP] - 14 Feb 2008 00:21 GMT
> I'm still not sure if I understand this correctly.
> Does a List<T> object holds a bunch of objects of type T or
> Does it only hold the references (addresses?) of those objects?
>
> I certainly don't want each task to hold copies of all its prerequisites and
> dependent tasks.

Assuming T is a reference type (e.g. a class) then it only holds
references.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Robert Wells - 14 Feb 2008 00:29 GMT
Thank You!

>> I'm still not sure if I understand this correctly.
>> Does a List<T> object holds a bunch of objects of type T or
[quoted text clipped - 6 lines]
> Assuming T is a reference type (e.g. a class) then it only holds
> references.
Marc Gravell - 14 Feb 2008 05:10 GMT
As Jon has stated - references; that is why I added my "where T is a
class (not a struct)" caveat to my original reply.

In .NET, and very unlike C++, there is a large and fundamental
difference between "struct" and "class". A struct (examples: int,
float, DateTime, etc)  follows value-type semantics, and will copy
itself (memcopy) sooner than you can glance at it - i.e.

int x = 5;
int y = x; // [memcopy of the struct]

Because of this, structs are almots always immutable (non-editable
once created), because it just gets too confusing to think about them
otherwise.

In contrast, classes have reference-type semantics. "new" creates a
new object on the managed heap, and the field/variable just holds the
referenece (think: pointer). Most .NET types are classes. Assignment
of variables copies *the reference*, not the object:

Foo x = new Foo();
Foo y = x; // reference value is copied, both x & y point to same Foo

Marc
Robert Wells - 15 Feb 2008 17:11 GMT
Thank you!

> As Jon has stated - references; that is why I added my "where T is a
> class (not a struct)" caveat to my original reply.
[quoted text clipped - 20 lines]
>
> Marc

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.