Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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#:

C++
class A
{
public:
    int* p;
    A(int* pv) : p(pv) {}
    void DoPlusPlus() { (*p)++; }
};

int i = 5;
A a(&i);
a.DoPlusPlus();


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
Comments
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.
Sergey Alexandrovich Kryukov 30-Sep-11 1:17am    
To me, this would be the best answer.
--SA

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);
a.DoPlusPlus();


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.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 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.
--SA
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
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Sep-11 1:24am    
I agree, a very good point, my 5.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900