Click here to Skip to main content
13,147,037 members (60,994 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello CodeProject Gurus,

I need help with some C# fundamentals... Look at this C code and please tell me how to do this in a simple and civilized way in C#:

class A
    int* p;
    A(int* pv) : p(pv) {}
    void DoPlusPlus() { (*p)++; }
int i = 5;
A a(&i);

I have a whole bunch of classes like this where I am just hitting the wall. All I need to do is to initialize an instance of a class with some scalar and have this instance to be able to modify the value of this scalar later on.
Posted 29-Sep-11 0:21am
Richard MacCutchan 29-Sep-11 6:57am
Seems a rather odd (dangerous) way of constructing a class; the value should be inside the class to protect it from random manipulation. My advice would be to redesign your classes completely.
SAKryukov 30-Sep-11 1:17am
To me, this would be the best answer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I think the answer to this one is: you shouldn't do it that way. If a C# programmer sees an int variable he won't expect it to change unless he passes it as ref to a method.

The simple technical solution is
class WrappedInt {
 public int i; 
 public WrappedInt(int i) { this.i = i; }
class A {
 private WrappedInt p;
 public A(WrappedInt pv) { p = pv; }
 public void DoPlusPlus() { p.i++; }
WrappedInt i = new WrappedInt(5);
A a = new A(i);

You can extend this to a generic boxing class that is implicitly castable to and from its inner type relatively easily. I'm not going to show that*, though, because I think this is the wrong road to go down.

But this seems like a strange thing to be doing. If your A class modifies the state of the application, it should do so in a more explicit and obvious way. Give A an instance of the global state (or data model or whatever i represents a piece of) and have it update that directly:

class State {
 public int i; 
 public int otherStateVariable;
 // ... etc
class A {
 State state;
 public A(State state) { this.state = state; }
 public void DoPlusPlus { state.i++; }

In a real scenario the state or data model would expose properties that would notify other layers and so on.

Of course the best by far is if A can be written with no external dependencies and definitely with no external writable dependencies (those are what really make an application hard to maintain), but I'm guessing you already thought of trying to do that.

*: Edit: I posted it as a Tip/Trick[^] instead.
SAKryukov 30-Sep-11 1:23am
I voted 5 for this good explanation and Tip/Trick article, but... please see my comment. I like the short comment by Richard MacCutchan, see above.
BobJanova 30-Sep-11 13:09pm
Yep, I agree that a redesign is in order. Hopefully the first and last paragraph make that clear enough :P
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hmm. I guess this is an example rather than real code because simply you wouldn't create a class to increment an integer.

C# doesn't have the concept of integer pointers either (unsafe code aside) so you're going to run into the problem of 'boxing' if you attempt this, that is when you create a reference, it will actually copy the value and reference that rather than the thing you want to reference - very confusing to C++ types.

One approach might be to wrap your scalar in some sort of class to make it a reference type.

I need to get some lunch. Can you explain a little more why you need these sort of constructs - this approach really doesn't lend itself well to .NET.
SAKryukov 30-Sep-11 1:24am
I agree, a very good point, my 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 29 Sep 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100