0

I am trying to link 2 different classes together. Relationship is a one to many relationship. So I'm trying to make a program which will let me make every object unique. So I decided to add Unique IDs to each object. How can I make a method that will give me a unique number that won't be repeated. I'm using C# .net5.0.

EDIT: a method that just gives a number that hasn't been used before is ok too. For example, 1, and then it will check if 1 has been used before if not than it will use one and if yes than it will use 2

  • Does this answer your question? [Generating random, unique values C#](https://stackoverflow.com/questions/14473321/generating-random-unique-values-c-sharp) and [Unique id number C#](https://stackoverflow.com/questions/25464266/unique-id-number-c-sharp) and [Generate unique ID from string in C#](https://stackoverflow.com/questions/63615950/generate-unique-id-from-string-in-c-sharp) and [How generate unique Integers based on GUIDs](https://stackoverflow.com/questions/2920696/how-generate-unique-integers-based-on-guids) –  May 24 '21 at 15:29
  • 3
    Using the [Guid string](https://en.wikipedia.org/wiki/Universally_unique_identifier) is the actual modern standard, better and more secure than autoinc, even managed by a database server. –  May 24 '21 at 15:31
  • 3
    Is this runtime uniqueness, or persistable uniqueness? – Matthew Watson May 24 '21 at 15:37

1 Answers1

1

You could use a static counter, something like this:

class Thing {

    static int IdCount;

    private int id;

    public Thing() {
        IdCount++;
        this.id = IdCount;
    }
    
    ...

}

The counter will be zero initially (the default integer value) and will increment on each instantiation of the class and assign the new value to the ID field of the object.

Be aware that this is not thread-safe and may not be appropriate for your needs.

EDIT (following question edit and comment):

You could use a function to generate the ID number but you should still consider thread-safety. A simple lock might be enough for your needs, something like this:

class Thing {

    static int IdCount;
    static readonly object objectLock = new object();
    
    private int id;

    public Thing() {
        this.id = GetIdNumber();
    }
    
    private static GetIdNumber() {
        lock (objectLock) {
            IdCount++;
            return IdCount;        
        }
    }
}

The lock will prevent a second, near-simultaneous instantiation from incrementing the counter before the first instantiation has had chance to return the number. For anything more complex you could look at (e.g.) the Interlocked class.

It should be obvious that the above are runtime-only solutions; values will not persist outside the lifetime of the application.

Neil T
  • 1,794
  • 1
  • 12
  • 21
  • 2
    Before making a suggestion like this, you need to find out whether the lifetime of the application domain is a sufficient scope for "uniqueness". If these IDs are persisted outside of the application, it won't do for them to start back at zero every time the app restarts. – StriplingWarrior May 24 '21 at 15:33
  • 2
    That's not very thread-safe. Consider using the `Interlocked` class – Flydog57 May 24 '21 at 15:35
  • 1
    @StriplingWarrior - I'm trying to give a quick, potentially useful answer to a somewhat vague question. If the OP needs something else he can ask for it. It's obvious that this won't persist outside the lifetime of the application, and that might be fine for this use case. – Neil T May 24 '21 at 15:36
  • 1
    @NeilT is it possible i can make that a function. So everytime it types a number in it remembers the last number it used so it can just add a number to it? hence i don't have to rely on any possibility of it accidently using the same id twice. – school student in uni May 24 '21 at 16:00
  • @user14186716 - it really depends what you're trying to achieve. I suggest you read the comments above and clarify the question. You should consider "thread safety" which in this context essentially means whether it's possible for the "function" to be called twice at more or less exactly the same time (and what happens / should happen here). A simple object lock might be enough, but without information it's not possible to say for sure. – Neil T May 24 '21 at 16:12
  • @NeilT can't I just make 2 functions but different names. so 1 class has there own id and the other class has it own id. since both classes can have same id for one of their objects. eg. class one has a id number 210 and class two also has a id number 210. – school student in uni May 24 '21 at 16:16
  • @user14186716 - I've edited the answer to show one way you could use a "function" to generate the IDs. I've also added a bit of thread-safety. You could use a similar technique on both classes and the counters would be independent, so it would be fine for two objects (one of each type) to have the same ID. – Neil T May 24 '21 at 16:24
  • @NeilT hey sorry i tried implementing this but im having a hard time. can u explain where to add it. – school student in uni May 24 '21 at 17:39
  • For this implementation, you'll need to add the two static fields, the instance field and the static `GetIdNumber()` method to each of your two classes. You'll then need to call the respective `GetIdNumber()` method from the constructor of each class. If you don't know these terms I suggest reading up a little on C# basics first otherwise it will be hard for me to help in a way that makes sense. – Neil T May 24 '21 at 18:05
  • @NeilT i dont know if this is ethical here but can u look at my code? discord? – school student in uni May 24 '21 at 19:40
  • Go on then, if it's quick! If you post your Discord tag I'll send you a message. – Neil T May 24 '21 at 19:50