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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How could I make a Name attribute a primary key?

How could I make a Name attribute a primary key?

Scheduled Pinned Locked Moved C#
regexquestion
2 Posts 2 Posters 1 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.
  • M Offline
    M Offline
    MBursill
    wrote on last edited by
    #1

    I have a simple class in which I will be making several objects from. Similar to WinForm controls I want to utilize some form of String attribute called Name to identify each object uniquely. Is there an easy way to have the maintaining of the list of names automatically handled? If there isn’t, I’m thinking of building a name manager system. Essentially just a factory pattern which accepts names (a String) and returns objects based on this. If the user tries to set the name to an object already in use, the factory wouldn’t return null, therefore the system disallows it. I’m curious to know if and how generics can play a part in making this work better. In other words, with generics would it be possible to build the factory to know that the name attribute was used on class ABC and therefore always return type ABC? -Mike.

    G 1 Reply Last reply
    0
    • M MBursill

      I have a simple class in which I will be making several objects from. Similar to WinForm controls I want to utilize some form of String attribute called Name to identify each object uniquely. Is there an easy way to have the maintaining of the list of names automatically handled? If there isn’t, I’m thinking of building a name manager system. Essentially just a factory pattern which accepts names (a String) and returns objects based on this. If the user tries to set the name to an object already in use, the factory wouldn’t return null, therefore the system disallows it. I’m curious to know if and how generics can play a part in making this work better. In other words, with generics would it be possible to build the factory to know that the name attribute was used on class ABC and therefore always return type ABC? -Mike.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can use a static HashTable in the class to keep track of the objects. In the constructor, add a reference to the object itself to the list. Create a static method that checks the list for an object by name. A factory to create the instances might be a good idea. Just make a static method in the class to create an instance, and make the constructor private so it can only be used by the factory method. --- b { font-weight: normal; } -- modified at 3:48 Friday 13th January, 2006

      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