making generic list class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • colin

    making generic list class

    Hi,

    Im trying to implement a generic list class for a number of 3d object types,
    that form a 3d solid model, take the easiest one Point for example,

    it contains a Vector3 and a list of other 3d object types
    such as surfaces and wires wich also reference this point.

    I need to be able to search the list for an exisitng Point
    but just using the Vector3, then if its not in the list
    to insert a new Point with the given coordinates.

    Is there a good/easy way of doing this with a template class
    with only specifying the one type ie Point in the template ?
    I had problems in that you cant use parameters in a new statement,
    nor have static functions in the interface.

    I dont realy want to resort to using object as a parameter.

    nor do I want to use a list such as <key,valuepai r
    as the key is in the value. although I dont need it to be fast.

    Im stil relativly new to c# and I tried it a few ways using class template
    with 2 types but
    it was awkward, even trying to use an interface template as the base for the
    3d objects.
    I keep trying to do the things I know I can do in C++ wich probably isnt so
    good.

    In the end I found it easier to do a seperate list class for each type,
    as trying to use templates I seemed to end up with more helper classes,
    and the information was buried under more layers when viewed in the
    debugger.

    thanks
    Colin =^.^=


  • Jon Skeet [C# MVP]

    #2
    Re: making generic list class

    colin <colin.rowe1@nt world.NOSPAM.co mwrote:
    Im trying to implement a generic list class for a number of 3d object types,
    that form a 3d solid model, take the easiest one Point for example,
    >
    it contains a Vector3 and a list of other 3d object types
    such as surfaces and wires wich also reference this point.
    >
    I need to be able to search the list for an exisitng Point
    but just using the Vector3, then if its not in the list
    to insert a new Point with the given coordinates.
    Okay - that sounds like you just need to use List<Pointand then
    List<T>.Find with a predicate which matches based on the Vector3.

    I'm afraid if that doesn't help, I'm not terribly clear on what you're
    trying to do...

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

    Comment

    • Michael D. Ober

      #3
      Re: making generic list class

      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.21b3de 8e653bb0f5665@m snews.microsoft .com...
      colin <colin.rowe1@nt world.NOSPAM.co mwrote:
      >Im trying to implement a generic list class for a number of 3d object
      >types,
      >that form a 3d solid model, take the easiest one Point for example,
      >>
      >it contains a Vector3 and a list of other 3d object types
      >such as surfaces and wires wich also reference this point.
      >>
      >I need to be able to search the list for an exisitng Point
      >but just using the Vector3, then if its not in the list
      >to insert a new Point with the given coordinates.
      >
      Okay - that sounds like you just need to use List<Pointand then
      List<T>.Find with a predicate which matches based on the Vector3.
      >
      I'm afraid if that doesn't help, I'm not terribly clear on what you're
      trying to do...
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      World class .NET training in the UK: http://iterativetraining.co.uk
      >
      How about Dictionary<poin t, Vector3?.

      Mike.


      Comment

      • colin

        #4
        Re: making generic list class


        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:MPG.21b3de 8e653bb0f5665@m snews.microsoft .com...
        colin <colin.rowe1@nt world.NOSPAM.co mwrote:
        >Im trying to implement a generic list class for a number of 3d object
        >types,
        >that form a 3d solid model, take the easiest one Point for example,
        >>
        >it contains a Vector3 and a list of other 3d object types
        >such as surfaces and wires wich also reference this point.
        >>
        >I need to be able to search the list for an exisitng Point
        >but just using the Vector3, then if its not in the list
        >to insert a new Point with the given coordinates.
        >
        Okay - that sounds like you just need to use List<Pointand then
        List<T>.Find with a predicate which matches based on the Vector3.
        Aha ok thanks, I remember I did come accross that
        but im not familiar with it and it wasnt obvious to me at first glance how
        to even use it,
        so il have to go have a further read now youve given me a hint it might be
        in the right direction.
        :)

        I kinda like the idea of keeping it simple,
        as theres only 3 seperate list types.
        after having to use reflection im kinda weary of complicated stuff.
        I'm afraid if that doesn't help, I'm not terribly clear on what you're
        trying to do...
        Well actually I get that feeling too sometimes, ...

        I remember hearing somewhere there was so much information being generated
        about
        windows no one person could ever keep up with the new information let alone
        know everything there is to know.

        Colin =^.^=


        Comment

        • colin

          #5
          Re: making generic list class

          "Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
          news:13kjmp1id1 po1da@corp.supe rnews.com...
          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
          news:MPG.21b3de 8e653bb0f5665@m snews.microsoft .com...
          >colin <colin.rowe1@nt world.NOSPAM.co mwrote:
          >>Im trying to implement a generic list class for a number of 3d object
          >>types,
          >>that form a 3d solid model, take the easiest one Point for example,
          >>>
          >>it contains a Vector3 and a list of other 3d object types
          >>such as surfaces and wires wich also reference this point.
          >>>
          >>I need to be able to search the list for an exisitng Point
          >>but just using the Vector3, then if its not in the list
          >>to insert a new Point with the given coordinates.
          >>
          >Okay - that sounds like you just need to use List<Pointand then
          >List<T>.Find with a predicate which matches based on the Vector3.
          >>
          >I'm afraid if that doesn't help, I'm not terribly clear on what you're
          >trying to do...
          >>
          >--
          >Jon Skeet - <skeet@pobox.co m>
          >http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          >World class .NET training in the UK: http://iterativetraining.co.uk
          >>
          >
          How about Dictionary<poin t, Vector3?.
          hmm my Vector3 can change, ie the point can move,
          the point is shared by numerous objects (Ie 3d surfaces)
          wich each need to see the updated position.
          I hadnt considerd sorting the lists but the following would cuase an issue I
          hadnt even thought of before.

          quote:-
          As long as an object is used as a key in the Dictionary, it must not change
          in any way that affects its hash value. Every key in a Dictionary must be
          unique according to the dictionary's equality comparer. A key cannot be a
          null reference (Nothing in Visual Basic), but a value can be, if the value
          type TValue is a reference type.

          Il look into Jon's sugestion soon but for now im bogged down with trying to
          get XNA to draw the lines for my wire frames as wide as I tell it to in
          rather than just 1 pixel wide.

          thanks
          Colin =^.^=


          Comment

          Working...