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 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
<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
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
Hi,
Many thanx for the fast responses.
Jon, could you please give a small example ?
John
<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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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;
|
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...
|
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;
}
|
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...
|
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???
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |