Results 1 to 6 of 6

Thread: c# help needed (again) please

  1. #1
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    c# help needed (again) please

    Hi all, i've come across an issue which i can't figure out, i'll start with the line of code which is giving me problems

    Code:
    List<Link> tempList = this._networkData.links;
    This is the code that looks after the links in _networkData.

    Code:
    private List<Link> _links = new List<Link>();
    public List<Link> links
    {
       get
       {
           return _links;
       }
       set
       {
           _links = value;
        }
    }
    Link is a class of my own construction, so i've created a strongly type list of Links stored in an instance of a class _networkData. I want to assign the list stored in _networkData to the temporary list tempList so that i can get a snapshot of it. The problem is that subsequently if i add another Link to _networkData.Links, this will also be added tp tempList even though i haven't explicitly added it to tempList and i don't want this to happen, i want tempList to stay the same.

    I know my c# skills aren't brilliant and i can't find any help on google so i was hoping that someone on here could tell me what i'm doing wrong.

    Thanks in anticipation.

    Andy

  2. #2
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: c# help needed (again) please

    a quick note, the properties are normally capitalised starting letter, just a nice way of clearing it up, also 'this' is often superflous, makes it shorter cleaner to read.


    This is happening because your getting a pointer to the original List.

    You want a (shallow?) clone of the list.

    so:
    Code:
    List<Link> tempList = new List<Link>(_networkData.links);
    As a quick asside, the generic list class (like all generics in .Net) can support 'Value' rather than 'Object' types. This is better than java, which boxes all value types to objects. This is a performance hit.

    As such if you've got a List<T> where T is something that can be represented as a struct you will get better performance.

    Generally speaking structs shouldn't get above 16bytes ish. (so 4, 4byte integers). This can really improve performance when doing big things with lists, and is very simple.
    throw new ArgumentException (String, String, Exception)

  3. Received thanks from:

    kasavien (06-03-2008)

  4. #3
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Re: c# help needed (again) please

    Thanks for your help, it works perfectly now. I'm still learning a lot of the intricacies of the language and finding it very enjoyable as i go a long, but i'm still in some instances stuck in my c programming roots where things like this wouldn't really come up!

    Thanks again for your help.

    Andy

  5. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: c# help needed (again) please

    yeh its a bit of a culture shock, just because the syntax is similar doesn't mean much under the hood would be.

    Thing is arrays are passed via pointer, and generic collections are of course no expection (as they are a class, which is always passed by pointer).

    Because creating a shall copy would take quite some time.
    throw new ArgumentException (String, String, Exception)

  6. #5
    Pup
    Pup is offline
    woof
    Join Date
    Apr 2006
    Location
    West London
    Posts
    265
    Thanks
    18
    Thanked
    19 times in 15 posts
    • Pup's system
      • Motherboard:
      • Asus P8Z68-V Pro
      • CPU:
      • i7-2600K
      • Memory:
      • 8GiB Corsair Vengeance LP DDR3 1600MHz
      • Storage:
      • 2 x 1TB Samsung Spinpoint F3
      • Graphics card(s):
      • 2 x MSI GTX 560 Ti Twin Frozr II 1GiB
      • PSU:
      • Corsair HX850W
      • Case:
      • Antec P182
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • HP LP2475w & Iiyama Prolite E435s
      • Internet:
      • Virgin Media 20mb

    Re: c# help needed (again) please

    A google of something like 'value vs reference types c#' would be a good place to go if you want further info on whats going on. I recently read the following books which also have decent explanations, and I would recommend either if you are looking for one :

    Microsoft&#174; Visual C#&#174; 2008 Step by Step (good intro book)
    APRESS.COM : Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition

    Theres 2005 versions of those books too, which might be better for some as these ones replace stuff like windows forms with WPF chapters for example.

  7. #6
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Re: c# help needed (again) please

    Now i feel dumb, i have the 2005 step by step book next to me on my desk and on page 133 it answers the question i originally asked the problem is i don't know if the book would have helped because i didn't really know what topic the problem related to. Oh well you live and learn.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Advice needed please...
    By Areku in forum PC Hardware and Components
    Replies: 16
    Last Post: 12-09-2006, 01:31 PM
  2. Is the 12V extra power plug needed?
    By MSIC in forum Help! Quick Relief From Tech Headaches
    Replies: 12
    Last Post: 25-05-2006, 08:10 AM
  3. Help needed...overclocking hell.
    By RDL in forum Help! Quick Relief From Tech Headaches
    Replies: 1
    Last Post: 11-05-2005, 02:56 PM
  4. Radeon 8500 linux driver install problems
    By Dorza in forum Software
    Replies: 0
    Last Post: 22-09-2003, 12:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •