Click here to Skip to main content
11,414,234 members (71,191 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
{
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 29-Sep-11 1:21am
Comments
Richard MacCutchan at 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 at 30-Sep-11 1:17am
   
To me, this would be the best answer.
--SA
Rate this: bad
good
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);
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.
  Permalink  
v2
Comments
SAKryukov at 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 at 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
good
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.
  Permalink  
Comments
SAKryukov at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,130
1 OriginalGriff 7,477
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web03 | 2.8.150427.2 | Last Updated 29 Sep 2011
Copyright © CodeProject, 1999-2015
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