|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem with objects and automatic numbering in VB.NETI'm having a small problem using objects in VB.NET. I've created a class "Shop" and one of its properties is a number which I've named "intNumber" (Integer). I've created several objects of the class Shop and everythink works fine. What I'm trying to do is to modify my class in such a manner that when a create a new object, the property "intNumber" has already a value. Using a constructor you can do this but I would like to give each object a different number, starting at 1 or 0, increasing 1 each time a new object has been created... The first object has automatically number 1 assigned to "intNumber" , the second number 2, etc... I know this is possible but have forgotten how to do this ... Many thanks John <johndev***@hotmail.com> wrote:
Show quoteHide quote > I'm having a small problem using objects in VB.NET. Use a Shared variable to keep track of how many instances have been > > I've created a class "Shop" and one of its properties is a number which > I've named "intNumber" (Integer). > > I've created several objects of the class Shop and everythink works > fine. What I'm trying to do is to modify my class in such a manner that > when a create a new object, the property "intNumber" has already a > value. Using a constructor you can do this but I would like to give > each object a different number, starting at 1 or 0, increasing 1 each > time a new object has been created... > > The first object has automatically number 1 assigned to "intNumber" , > the second number 2, etc... > > I know this is possible but have forgotten how to do this ... created before. Don't forget threading - either acquire a lock while you're incrementing, or use the Interlocked class. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Hi,
Many thanx for the fast responses. Jon, could you please give a small example ? John <johndev***@hotmail.com> wrote:
> Jon, could you please give a small example ? Something like this:Imports System Imports System.Threading Class Test Shared instancesCreated As Integer Dim id As Integer Sub New id = Interlocked.Increment (instancesCreated) End Sub Shared Sub Main() Dim x0 As New Test Dim x1 As New Test Dim x2 As New Test Dim x3 As New Test Console.WriteLine (x0.id) Console.WriteLine (x1.id) Console.WriteLine (x2.id) Console.WriteLine (x3.id) End Sub End Class -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Hi John,
You must define it as a static member and try working around this. Regards Udai.M Team Leader,Consultant Chimeratechnologies Bangalore Show quoteHide quote "johndev***@hotmail.com" wrote: > Hi everyone, > > I'm having a small problem using objects in VB.NET. > > I've created a class "Shop" and one of its properties is a number which > I've named "intNumber" (Integer). > > I've created several objects of the class Shop and everythink works > fine. What I'm trying to do is to modify my class in such a manner that > when a create a new object, the property "intNumber" has already a > value. Using a constructor you can do this but I would like to give > each object a different number, starting at 1 or 0, increasing 1 each > time a new object has been created... > > The first object has automatically number 1 assigned to "intNumber" , > the second number 2, etc... > > I know this is possible but have forgotten how to do this ... > > Many thanks > > John > >
OK....forget the code...how about some pointers?
Is F1 in VS2005 really dumb? "Global" Class Library: Best Practices? API confusion Threading question.... vb.net tabpage hiding painting Detect if user is logged in ADO.NET now or LINQ later Setting Individual Pixel Colors in VB 2005 How to show a new form. |
|||||||||||||||||||||||