473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Problem with objects and automatic numbering in VB.NET

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

Jan 22 '06 #1
6 1144
Hello john

Use a static variable to hold the last number:
public class Shop {
private static int nextNumber = 0;
private int number;

public int Number {
get { retun this.number; }
}

public Shop() {
this.number = nextNumber;
nextNumber++;
}

}
--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
http://www.cornerstone.se
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

Jan 22 '06 #2
<jo********@hotmail.com> wrote:
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 ...


Use a Shared variable to keep track of how many instances have been
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
Jan 22 '06 #3
Hello Patrik,

LOL sorry, read johns post. Shared is the correct term in VB.NET. A bit to
quick there.
--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
http://www.cornerstone.se
Hello john

Use a static variable to hold the last number:

public class Shop {
private static int nextNumber = 0;
private int number;
public int Number {
get { retun this.number; }
}
public Shop() {
this.number = nextNumber;
nextNumber++;
}
}

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
http://www.cornerstone.se
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

Jan 22 '06 #4

Hi,
Many thanx for the fast responses.

Jon, could you please give a small example ?

John

Jan 22 '06 #5
<jo********@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
Jan 22 '06 #6
Hi John,

You must define it as a static member and try working around this.

Regards

Udai.M
Team Leader,Consultant
Chimeratechnologies
Bangalore
"jo********@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

Jan 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
5
by: fatih kayaalp | last post by:
Hi, i have imported an access database into sqlserver 2000. In access, i have some table fields which were using automatic number as datatype. But i see that there is not a datatype in sqlserver...
6
by: Olaf Martens | last post by:
Greetings! Please consider the following piece of program code (note that I have stripped quite a lot of code here): int foo(void) { unsigned short l_valbuf; // address of this goes to...
12
by: jimjim | last post by:
Hello, 1. As the subject sugests, are the objects created in the stack guarranted to have been created? 2. How can I verify this? (e.g. for objects created in the heap A *a = new A one can...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
4
by: johndevlon | last post by:
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...
27
by: onkar | last post by:
#include<stdio.h> int main(void){ char a="abcde"; char *p=a; p++; p++; p='z'; printf("%s\n",p); return 0; }
1
by: Michael Doubez | last post by:
Hello, I want to mix in the same document Parts that have automatically numbered sections and parts which section are not numbered. Is there any out of the box way to locally suppress the...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.