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 1165
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********@hot mail.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.co m> 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********@hot mail.com> wrote: Jon, could you please give a small example ?
Something like this:
Imports System
Imports System.Threadin g
Class Test
Shared instancesCreate d As Integer
Dim id As Integer
Sub New
id = Interlocked.Inc rement (instancesCreat ed)
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.WriteLi ne (x0.id)
Console.WriteLi ne (x1.id)
Console.WriteLi ne (x2.id)
Console.WriteLi ne (x3.id)
End Sub
End Class
--
Jon Skeet - <sk***@pobox.co m> 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,Consulta nt
Chimeratechnolo gies
Bangalore
"jo********@hot mail.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 the thread is over a month old, I decided to
start a new one with my response.
Please see my comments inline.
|
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 like
automatic no. I have to enter id numbers to my tables for each records
automatically. Would anybody help me about solving this problem? Any
idea? Thanks
|
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 another function
Objtype *l_item; // I want to get this one's data!
|
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 check as: if ( a == NULL), or try to catch the bad_alloc exception)
TIA
|
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 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
|
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 automatic
numbering of section like in latex using \section*{} instead of
\section{} ?
Or should I customize the XSLT with a new kind of section
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |