473,324 Members | 2,254 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,324 software developers and data experts.

shared int reference?

I'd like to know how one shares an int variable across different ocjects
(of different classes) without pointers.

public class ClassA {
public int n;
public ClassA(int N) {
n = ???
n++;
}
}

public class ClassB {
public int n = 0;
public ClassA = new ClassA(n);
}

If I increment n in the instance of ClassA, I would like the parent
ClassB's instance to have it's "n" varable to have the same value.

Alex
Nov 16 '05 #1
5 1450
Alex Leduc <le******@netscape.net> wrote in
news:O8**************@TK2MSFTNGP09.phx.gbl:
I'd like to know how one shares an int variable across different
ocjects (of different classes) without pointers.

public class ClassA {
public int n;
public ClassA(int N) {
n = ???
n++;
}
}

public class ClassB {
public int n = 0;
public ClassA = new ClassA(n);
}

If I increment n in the instance of ClassA, I would like the parent
ClassB's instance to have it's "n" varable to have the same value.


In general, you can use the 'ref' parameter:

public ClassA(ref int N) { ... }

and

public ClassA = new ClassA(ref n);

BTW, I've never seen ref's used on a constructor - I'm not sure if this
is valid or not. Then again, I've not seen anything that says it can't
be done (haven't tried it.)

-mdb
Nov 16 '05 #2
Michael Bray wrote:
Alex Leduc <le******@netscape.net> wrote in
news:O8**************@TK2MSFTNGP09.phx.gbl:

I'd like to know how one shares an int variable across different
ocjects (of different classes) without pointers.

public class ClassA {
public int n;
public ClassA(int N) {
n = ???
n++;
}
}

public class ClassB {
public int n = 0;
public ClassA = new ClassA(n);
}

If I increment n in the instance of ClassA, I would like the parent
ClassB's instance to have it's "n" varable to have the same value.

In general, you can use the 'ref' parameter:

public ClassA(ref int N) { ... }

and

public ClassA = new ClassA(ref n);

BTW, I've never seen ref's used on a constructor - I'm not sure if this
is valid or not. Then again, I've not seen anything that says it can't
be done (haven't tried it.)

-mdb


The problem is that the assignment operator copies the value of N into n

public ClassA(ref int N)
{
n = N;
}
Nov 16 '05 #3
ref will not allow you to share the reference across objects the way a
pointer would do, because if you assign the ref to a field of the target
object, you "copy" the value instead of sharing the reference.

If you want to share an int value, you have to create a small object with an
int property (IntCounter would be a good class name for this). Then, you can
share the object and the trick is done. But there is no way to share an int
directly, at least not in managed code.

Bruno.

"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> a écrit dans le
message de news:Xn**********************************@207.46.2 48.16...
Alex Leduc <le******@netscape.net> wrote in
news:O8**************@TK2MSFTNGP09.phx.gbl:
I'd like to know how one shares an int variable across different
ocjects (of different classes) without pointers.

public class ClassA {
public int n;
public ClassA(int N) {
n = ???
n++;
}
}

public class ClassB {
public int n = 0;
public ClassA = new ClassA(n);
}

If I increment n in the instance of ClassA, I would like the parent
ClassB's instance to have it's "n" varable to have the same value.


In general, you can use the 'ref' parameter:

public ClassA(ref int N) { ... }

and

public ClassA = new ClassA(ref n);

BTW, I've never seen ref's used on a constructor - I'm not sure if this
is valid or not. Then again, I've not seen anything that says it can't
be done (haven't tried it.)

-mdb

Nov 16 '05 #4
You can also allocate an array with a single entry (new int[1]) and share
it.

Bruno

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> a écrit dans le message de
news:uo****************@TK2MSFTNGP10.phx.gbl...
ref will not allow you to share the reference across objects the way a
pointer would do, because if you assign the ref to a field of the target
object, you "copy" the value instead of sharing the reference.

If you want to share an int value, you have to create a small object with an int property (IntCounter would be a good class name for this). Then, you can share the object and the trick is done. But there is no way to share an int directly, at least not in managed code.

Bruno.

"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> a écrit dans le
message de news:Xn**********************************@207.46.2 48.16...
Alex Leduc <le******@netscape.net> wrote in
news:O8**************@TK2MSFTNGP09.phx.gbl:
I'd like to know how one shares an int variable across different
ocjects (of different classes) without pointers.

public class ClassA {
public int n;
public ClassA(int N) {
n = ???
n++;
}
}

public class ClassB {
public int n = 0;
public ClassA = new ClassA(n);
}

If I increment n in the instance of ClassA, I would like the parent
ClassB's instance to have it's "n" varable to have the same value.


In general, you can use the 'ref' parameter:

public ClassA(ref int N) { ... }

and

public ClassA = new ClassA(ref n);

BTW, I've never seen ref's used on a constructor - I'm not sure if this
is valid or not. Then again, I've not seen anything that says it can't
be done (haven't tried it.)

-mdb


Nov 16 '05 #5
Alex Leduc <le******@netscape.net> wrote in news:eY26Gj#IEHA.3208
@TK2MSFTNGP10.phx.gbl:
The problem is that the assignment operator copies the value of N into n

public ClassA(ref int N)
{
n = N;
}


Yeah sorry I didn't understand your original question. You can't really
share something between multiple objects (unless its the same TYPE of
object, in which case you can use a static variable.)

-mdb
Nov 16 '05 #6

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

Similar topics

3
by: Bill | last post by:
I created a shared assemble and dropped it into Windows\assembly. Browsing this directory shows that it is in there and the properties looks as expected. However, when I attempt to add this as a...
0
by: RadekP | last post by:
Hi Gurus .. I would really appreciate some insights for the problem that bugs me for quite some time. I keep my custom controls in their own shared (private/public key signed) assembly. I need...
9
by: Invalidlastname | last post by:
Hi, We developed some assemblies which use EnterpriseServices queued components. In order to use EnterpriseServices, these assemblies need to be installed into GAC. I used the pre-build and...
3
by: Henri | last post by:
1) Do Shared methods always need to be synchronised to be thread-safe? 2) Why must SyncLock be used with a reference type? I can't get the relation between threads and a type. Thanks for your...
2
by: tshad | last post by:
I have a program I am trying to compile into a dll and am getting a bunch of: the following errors: error BC30469: Reference to a non-shared member requires an object reference. At first, I...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
4
by: Brett | last post by:
I'm trying to use the F1 function inside of F2 function below. I keep getting the error posted below the code. If I remove the Shared declaration from F2, it works fine. What exactly does the...
2
by: tshad | last post by:
I am trying to build a small shared routine that I call in all my pages. I don't want to create an object reference as I just want to make a routine that I can use to add in code that needs to be...
5
by: Simon | last post by:
Hi all, We have an ASP.NET 1.1 application running on IIS6 on Server 2003. Most of the base objects we are using in this application are taken from a windows application also written by us. We...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.