Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 38131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:39:34+00:00 2026-05-10T14:39:34+00:00

I would like to give a class a unique ID every time a new

  • 0

I would like to give a class a unique ID every time a new one is instantiated. For example with a class named Foo i would like to be able to do the following

dim a as New Foo() dim b as New Foo() 

and a would get a unique id and b would get a unique ID. The ids only have to be unique over run time so i would just like to use an integer. I have found a way to do this BUT (and heres the caveat) I do NOT want to be able to change the ID from anywhere. My current idea for a way to implement this is the following:

Public Class test     Private Shared ReadOnly _nextId As Integer     Private ReadOnly _id As Integer     Public Sub New()         _nextId = _nextId + 1         _id = _nextId     End Sub End Class 

However this will not compile because it throws an error on _nextId = _nextId + 1 I don’t see why this would be an error (because _Id is also readonly you’re supposed to be able to change a read only variable in the constructor.) I think this has something to do with it being shared also. Any solution (hopefully not kludgy hehe) or an explanation of why this won’t work will be accepted. The important part is i want both of the variables (or if there is a way to only have one that would even be better but i don’t think that is possible) to be immutable after the object is initialized. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:39:35+00:00Added an answer on May 10, 2026 at 2:39 pm

    Consider the following code:

    Public Class Foo      Private ReadOnly _fooId As FooId       Public Sub New()          _fooId = New FooId()      End Sub       Public ReadOnly Property Id() As Integer          Get              Return _fooId.Id          End Get      End Property  End Class   Public NotInheritable Class FooId      Private Shared _nextId As Integer      Private ReadOnly _id As Integer       Shared Sub New()          _nextId = 0      End Sub       Public Sub New()          SyncLock GetType(FooId)              _id = System.Math.Max(System.Threading.Interlocked.Increment(_nextId),_nextId - 1)          End SyncLock      End Sub       Public ReadOnly Property Id() As Integer          Get              Return _id          End Get      End Property  End Class  

    Instead of storing an int inside Foo, you store an object of type FooId. This way you have full control over what can and cannot be done to the id.

    To protect our FooId against manipulation, it cannot be inherited, and has no methods except the constructor and a getter for the int. Furthermore, the variable _nextId is private to FooId and cannot be changed from the outside. Finally the SyncLock inside the constructor of FooId makes sure that it is never executed in parallell, guaranteeing that all IDs inside a process are unique (until you hit MaxInt :)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's possible, but you'll have to define the enum manually.… May 12, 2026 at 10:08 am
  • Editorial Team
    Editorial Team added an answer This was my solution: RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] May 12, 2026 at 10:08 am
  • Editorial Team
    Editorial Team added an answer yes, just change the host configuration on your connection string… May 12, 2026 at 10:08 am

Related Questions

I have, for my game, a Packet class, which represents network packet and consists
I have an array of hashes, and I want the unique values out of
I want to run some code on all my treeView nodes depending on a
My situation is that I screwed up essentially. I inherited my code base about

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.