Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Data structure design

Data structure design

Scheduled Pinned Locked Moved Design and Architecture
data-structureshelpquestiondesigncryptography
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    What is the best way to structure class objects in a program that allows me to search for values from different properties of the class? I am making an application that holds client name, address phone and note information. I created a Client class that holds all these together. In my main program, I was putting each new client in an array list, but I ran across a problem. I can't search my array list for just a first or last name. I don't think that overriding the Equals function of my class would allow me to change that, so I started looking at other ways to hold my data. If I created another value in my class, like an ID field, I could create a hash table that was a ID/value pair (ID/First Name, ID/Last Name, ID/Address, ect). This would allow me to search each hash table and find the ID to get the full record, but that would mean I would need a table for each field that I would want to search. At this point, I'm a bit stuck on the best way to set my data structure up. If there's some articles that give some guidance and suggestions, I would appreciate it. I can't seem to find anything because I don't think I'm using the right terminology.

    R M L C 4 Replies Last reply
    0
    • H hpjchobbes

      What is the best way to structure class objects in a program that allows me to search for values from different properties of the class? I am making an application that holds client name, address phone and note information. I created a Client class that holds all these together. In my main program, I was putting each new client in an array list, but I ran across a problem. I can't search my array list for just a first or last name. I don't think that overriding the Equals function of my class would allow me to change that, so I started looking at other ways to hold my data. If I created another value in my class, like an ID field, I could create a hash table that was a ID/value pair (ID/First Name, ID/Last Name, ID/Address, ect). This would allow me to search each hash table and find the ID to get the full record, but that would mean I would need a table for each field that I would want to search. At this point, I'm a bit stuck on the best way to set my data structure up. If there's some articles that give some guidance and suggestions, I would appreciate it. I can't seem to find anything because I don't think I'm using the right terminology.

      R Offline
      R Offline
      Ray Cassick
      wrote on last edited by
      #2

      hpjchobbes wrote:

      but that would mean I would need a table for each field that I would want to search

      Consider this the way that DBs do it. That is (at a high level) what gets created when you form an index on a table. That's why you really should not go and create indexes on every attribute of an entity. One thing you could consider doing is instead of storing the data in an array list of objects in memory, store it in an in memory data-set. That way you can query it using standard SQL queries all you want. Wrap the collection in a class that uses an data set internally to store the data and returns collections of objects to you as the result. That way you can keep your data in a simple search-able format and still get objects to work with latter. I am sure that someone here could also come up with some cool alternatives using objects and LINQ, but I am not all up on that tech yet :)


      FFRF[^]
      My LinkedIn profile[^]
      My Programmers Blog[^]

      H 1 Reply Last reply
      0
      • R Ray Cassick

        hpjchobbes wrote:

        but that would mean I would need a table for each field that I would want to search

        Consider this the way that DBs do it. That is (at a high level) what gets created when you form an index on a table. That's why you really should not go and create indexes on every attribute of an entity. One thing you could consider doing is instead of storing the data in an array list of objects in memory, store it in an in memory data-set. That way you can query it using standard SQL queries all you want. Wrap the collection in a class that uses an data set internally to store the data and returns collections of objects to you as the result. That way you can keep your data in a simple search-able format and still get objects to work with latter. I am sure that someone here could also come up with some cool alternatives using objects and LINQ, but I am not all up on that tech yet :)


        FFRF[^]
        My LinkedIn profile[^]
        My Programmers Blog[^]

        H Offline
        H Offline
        hpjchobbes
        wrote on last edited by
        #3

        I'm just starting to learn programming, so I was trying to keep my implementation simple, but I guess this is a bit more complex problem that I originally thought! I guess for my simple application having a hash table for each field is probably the easiest way to go. I didn't even think of making my own collection class, but that seems like it might be a good way to go to learn more about databases before I start diving into some of the main databases out there. I'm not to the point where I need to worry about handling large amounts of data, so for now, I'll keep it simple. Thanks for the information!

        R 1 Reply Last reply
        0
        • H hpjchobbes

          I'm just starting to learn programming, so I was trying to keep my implementation simple, but I guess this is a bit more complex problem that I originally thought! I guess for my simple application having a hash table for each field is probably the easiest way to go. I didn't even think of making my own collection class, but that seems like it might be a good way to go to learn more about databases before I start diving into some of the main databases out there. I'm not to the point where I need to worry about handling large amounts of data, so for now, I'll keep it simple. Thanks for the information!

          R Offline
          R Offline
          Ray Cassick
          wrote on last edited by
          #4

          keeping it simple is always good, just build it so that you can extend it latter without having to redo too much of the host application. Always be thinking about encapsulation, even when trying to keep it simpler. Things have a way of getting away from you and not staying simple for long :)


          FFRF[^]
          My LinkedIn profile[^]
          My Programmers Blog[^]

          1 Reply Last reply
          0
          • H hpjchobbes

            What is the best way to structure class objects in a program that allows me to search for values from different properties of the class? I am making an application that holds client name, address phone and note information. I created a Client class that holds all these together. In my main program, I was putting each new client in an array list, but I ran across a problem. I can't search my array list for just a first or last name. I don't think that overriding the Equals function of my class would allow me to change that, so I started looking at other ways to hold my data. If I created another value in my class, like an ID field, I could create a hash table that was a ID/value pair (ID/First Name, ID/Last Name, ID/Address, ect). This would allow me to search each hash table and find the ID to get the full record, but that would mean I would need a table for each field that I would want to search. At this point, I'm a bit stuck on the best way to set my data structure up. If there's some articles that give some guidance and suggestions, I would appreciate it. I can't seem to find anything because I don't think I'm using the right terminology.

            M Offline
            M Offline
            Mark Churchill
            wrote on last edited by
            #5

            Overriding Equals is only for comparing if objects are the same. Ignoring indexes, if you need to search a List< Person> then you can do it by hand. Iterate over the list with foreach, and put the results into your result list when the fields match. If you are using a later version of the framework you can pass an anonymous method into the .Find(Predicate< T>) method. This would look like this: List< Person> results = data.Find(delegate (Person p) { return p.Name == searchName; }); Or in a lambda expression: List< Person> results = data.Find(p => p.Name == searchName); If you are just starting programming then worry about the basics first. As for indexing, you should either use a database (Diamond Binding in my sig will make it pretty easy for you) or if you want a pure object solution then you might want to check out a pure object database. db4objects is a very good object database, but can be quite expensive. Generally in the "real world" you would rarely be writing your index code by hand (but it is important to be aware of lookup performance in your data structures).

            Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
            Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

            H 1 Reply Last reply
            0
            • M Mark Churchill

              Overriding Equals is only for comparing if objects are the same. Ignoring indexes, if you need to search a List< Person> then you can do it by hand. Iterate over the list with foreach, and put the results into your result list when the fields match. If you are using a later version of the framework you can pass an anonymous method into the .Find(Predicate< T>) method. This would look like this: List< Person> results = data.Find(delegate (Person p) { return p.Name == searchName; }); Or in a lambda expression: List< Person> results = data.Find(p => p.Name == searchName); If you are just starting programming then worry about the basics first. As for indexing, you should either use a database (Diamond Binding in my sig will make it pretty easy for you) or if you want a pure object solution then you might want to check out a pure object database. db4objects is a very good object database, but can be quite expensive. Generally in the "real world" you would rarely be writing your index code by hand (but it is important to be aware of lookup performance in your data structures).

              Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
              Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

              H Offline
              H Offline
              hpjchobbes
              wrote on last edited by
              #6

              Thanks for the response! The .Find(Predicate) function is interesting and I'm going to be looking into that more. All my programming is really for learning purposes, so I tend to try and stay away from solutions that have been built for me as I don't feel that I get a good understanding of what is going on. It's probably not the 'best' method for learning, but it seems to work decent for me. What I just thought about now, seeing you mention using List instead of ArrayList is how the two differ. I would imagine that ArrayList just boxes everything into an object, where List uses the base type. Would this make it faster to search through a List than an ArrayList as there is no need for un-boxing? I guess there's another topic added to my list of things to research! Thanks for the information!

              M 1 Reply Last reply
              0
              • H hpjchobbes

                Thanks for the response! The .Find(Predicate) function is interesting and I'm going to be looking into that more. All my programming is really for learning purposes, so I tend to try and stay away from solutions that have been built for me as I don't feel that I get a good understanding of what is going on. It's probably not the 'best' method for learning, but it seems to work decent for me. What I just thought about now, seeing you mention using List instead of ArrayList is how the two differ. I would imagine that ArrayList just boxes everything into an object, where List uses the base type. Would this make it faster to search through a List than an ArrayList as there is no need for un-boxing? I guess there's another topic added to my list of things to research! Thanks for the information!

                M Offline
                M Offline
                Mark Churchill
                wrote on last edited by
                #7

                Yeah theres a bit of history there. List< T> is a generic list, which means when you use say List< Foo> you have effectively created a brand new type out of thin air, a List of Foos. Before framework 2, generics didn't exist in the runtime, and ArrayList was a provided and the commonly used IList implementation. Both are implemented the same under the hood, they both wrap an array which they resize as necessary. For new development you may as well use List< object> over ArrayList, they are functionally identical, but using List< object> makes it very clear that you know the generic class exists - and thus the list is actually full of random crap. I guess the generic list would remove any unboxing that happens, I had never really looked at it that way (it would definitely help with the semantics of having to do foo = (long)(int)objectWhichIsReallyABoxedInt). As you learn programming you will hear a lot of people talking (mostly crap) about optimisation. Generally speaking you should look out for the big-O order of things, and obvious problems like doing a bunch of high-latency things in serial. Most things like "avoiding boxing", "avoiding reflection" make little difference to a real world application. In fact I've seen a great many "tips for faster code" that results in identical IL (and thats before JIT optimisation). I use reflection extensively in Entanglar (even in the renderer I'm working on), and the performance of that is more than acceptable.

                Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                1 Reply Last reply
                0
                • H hpjchobbes

                  What is the best way to structure class objects in a program that allows me to search for values from different properties of the class? I am making an application that holds client name, address phone and note information. I created a Client class that holds all these together. In my main program, I was putting each new client in an array list, but I ran across a problem. I can't search my array list for just a first or last name. I don't think that overriding the Equals function of my class would allow me to change that, so I started looking at other ways to hold my data. If I created another value in my class, like an ID field, I could create a hash table that was a ID/value pair (ID/First Name, ID/Last Name, ID/Address, ect). This would allow me to search each hash table and find the ID to get the full record, but that would mean I would need a table for each field that I would want to search. At this point, I'm a bit stuck on the best way to set my data structure up. If there's some articles that give some guidance and suggestions, I would appreciate it. I can't seem to find anything because I don't think I'm using the right terminology.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  The data-structure you're looking for is called a "multi-index" container. There is a very good example of one in BOOST. http://www.boost.org/doc/libs/1_36_0/libs/multi_index/doc/index.html[^]

                  1 Reply Last reply
                  0
                  • H hpjchobbes

                    What is the best way to structure class objects in a program that allows me to search for values from different properties of the class? I am making an application that holds client name, address phone and note information. I created a Client class that holds all these together. In my main program, I was putting each new client in an array list, but I ran across a problem. I can't search my array list for just a first or last name. I don't think that overriding the Equals function of my class would allow me to change that, so I started looking at other ways to hold my data. If I created another value in my class, like an ID field, I could create a hash table that was a ID/value pair (ID/First Name, ID/Last Name, ID/Address, ect). This would allow me to search each hash table and find the ID to get the full record, but that would mean I would need a table for each field that I would want to search. At this point, I'm a bit stuck on the best way to set my data structure up. If there's some articles that give some guidance and suggestions, I would appreciate it. I can't seem to find anything because I don't think I'm using the right terminology.

                    C Offline
                    C Offline
                    CodingYoshi
                    wrote on last edited by
                    #9

                    What if you did something like this: Create a class called ClientCollection which will have all the clients in some data structure of your choice. Within this class (ClientCollection) have a method called Search, or Find which will take 2 parameters: 1. The property to search by and 2. The value to search for. Within this method create a Dictionary and put all your clients into it. The property to search by will be the Key, and the value will be the Clients. Afterwards, call the Contains method of the Dictionary to search the value passed in. Of course, this means that if you have many clients putting them into the Dictionary will take time. I will also do what you suggested above in addition to this but create tables for the field(s) which will be used mostly for searching. Now you don't have too many tables, but you are also providing the flexibility of searching by any field.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups