Good evening-
What I'm trying to do is take a variable, place a reference to it in a container, have some math operations act on the whole container (which might have any number of references to variables) and then be able to access the original data in the original variable.
Hope that wasn't too confusing.
In C++ this was performed by using references and pointers - I'd pass the variable to a function by reference, make a pointer, place the pointer into a container, do my operations, and at any time I could query the original variable and get the updated results since the pointer pointed back to it.
How is this accomplished in C# I banged my head up against the wall for a few hours this evening trying a few things but I don't quite see how...
Thanks!
-philski

learning to live w/o pointers (C++ programmer here)
Jakob S
I want to be able to persistantly operate on basically what in c++ what would be a pointer. This is a common practice in engineering codes. You use friendly variable names for the engineering variables (like x, y, z, Velocity, Acceleration) and then make an array of pointers and operate on the array of pointers. I don't know at development time what the names of the variables will be, and I don't want my users to have to make 10+ changes to the code to allow for their custom variable names.
Is there no similar functionality to this in C#
Thanks
Hokgiarto
>> Is there no similar functionality to this in C#
Yes there is. In fact, it's rather difficult to avoid this functionality.
When you pass a variable (of a class type) to a method in C#, you ARE passing a reference.
StreamReader sr = new StreamReader("MyFile.txt");
string first = sr.ReadLine();
MyReader mr = new MyReader(sr);
mr.ReadMore();
string fourth = sr.ReadLine();
mr.ReadEvenMore();
class MyReader
{
StreamReader m_sr;
string second;
string third;
string fifth;
public MyReader(StreamReader sr)
{ m_sr = sr ;}
public void ReadMore()
{
second = m_sr.ReadLine();
third = m_sr.ReadLine();
}
public void ReadEvenMore()
{
fifth = m_sr.ReadLine();
}
}
Note, that this only applies to reference (class) types. Value (struct, or in C++ terms, intrinsic) types are passed by value.
enric vives
You can use ref and out keyword in specifying a parameter of a method.
For example: (Not a best practice, let say I want to write a method, calculate a + b, and return the value to c, not using return value)
protected void Calculate ( int a, int b, out int c )
{
c = a + b ;
}
And this is how we call it:
int result:
Calculate ( 5, 10, out result ) ;
Another example: I want to write a method to swap value of 2 variable:
protected void Swap ( ref int a, ref int b )
{
int temp = a ;
a = b ;
b = a ;
}
And this is how we call it:
int a = 5 ;
int b = 10 ;
Swap ( ref a, ref b ) ;// After this call a will has value of 10, and b will has value of 5
Notice that using ref, we should initialize value of the ref parameter first, while using out, we don't need to initialize, instead in the definition of the method, we have to give a value to that out parameter.
Sorry for my bad english, but I hope this helps
Gabriel Lozano-Moran
Perhaps what you want is like this
int x, y, z, Velocity, Acceleration ;
unsafe
{
int*[] p_arr = new int*[4];
p_arr[0] = &x ;
p_arr[1] = &y ;
p_arr[2] = &Velocity ;
p_arr[3] = &Acceleration ;
//Do something like:
*p_arr[0] = 10 ;
}
Console.WriteLine ( x ) ;
DRoden
Your snippet it's what I guessed, but honestly, I can't figure out the "Engeneered" approach in such a code.
What sense has to define a pointer-vector of data semantically not correlated
I've learnt by Kerningan & Ritchie to use pointer to struct, at worst... Have I to buy an update book
Pixelpands
u can use ref keyword
void Method(ref int i)
{
i = 10;
}
static void Main()
{
int val = 0;
Method(ref val);
// val is now 10
}
Regards,
Sudeesh
Ted.
I'm a simulation programmer. I want to have a framework that propogates a simulation forward in time. That's standard stuff, I can make a set of classes or framework to do it.
However I don't know what my user wants to program. Is he going to have 5 variables to integrate, or 500
So the way I tacked it (and others to, too) is something like this in c++
void addDerivitive(double &x, double &dx) {
double *x_ptr, *dx_ptr;
x_ptr = &x;
dx_ptr = &dx;
// place the pointers into a vector or array
}
Then my engineering framework can deal with the vector or array without knowing exactly what model the user is building, and the user can use whatever variables are convenient to him. I just integrate (or differentiate, or whatever) a vector, and they program the appropriate model in their classes. I can call their classes to update their derivitive model and then integrate the vector and it all works, since their variables coincide to my vector.
I don't see an easy way to do this kind of functionality in C#. Basically I'd have to tell my users that they have to use Vector
-philski-
Sigge Persson
James: I want to pass doubles. Doubles are value types. Your code snippet doesn't apply.
I posted a code snippet above. Again - I'm looking for a replacement for this idea
User has some variables in his class, I have some pointers to his variables in a vector.
I operate on the vector, he operates on his variables
That way, I can write a engineering code that will be useful to someone and generic enough that they can pick it up, write their own class with vairables, place pointers to those variables into my vector using a convencience function, and I can operate on that vector by iterating through it.
Thanks anyways ... guess I'll have to stick with unsafe code for now.
Philski
VBE_programmer
In addition to these very good replies (much better than mine), I thought I should add that if you don't know how big your array is going to by, you can use a list<type> instead of an array.
(Nimrand's reply actually answered an unrelated question I was going to post
)
antigone
I'm attempting to figure out exactly how this "engeneered approach" works, maybe I'm misunderstanding the stuff.
Please, would you be so gentle to post a C++ snippet that clarify the concept
Sql4088
I'm not 100% clear one what you're trying to do. A little bit of sample code would probably help illustrate. However, maybe this will help.
.NET divides all data types into reference types and value types. Classes are reference types, so anytime you have an instance of a class in C#, what you actually have is a .NET reference to that instance (not to be confused with C++ references). References are essentially very smart pointers without the need for indirection syntax. So, if you have an array of objects, you pretty much have an array of pointers to objects. Structs such as ints, doubles, and dates are value types. When you have an instance of a struct, you have the value itself, not a pointer to it. If you want to have a pointer to an int, you have to enclose it in a class. Then, you can have a reference to an object of that class that contains an int.
The 'ref' and 'out' keywords often muddle the issue when it comes to first understanding .NET's use of references. Its a completely different issue from reference types versus value types, even though they use similar names. The 'ref' keyword essentially passes a pointer to the VARIABLE, not the object. So, if the variable is of a reference type (i.e., classes), then the function being called is allowed to modify what object the variable of the calling method references. You can always manipulate the object being passed to you, and since it is a reference, those changes will be appearant anywhere else that object is used. But you can't make the calling method's variable point to another object unless its passed in as a ref or out parameter.
Edit- oops, posted a little too late. :-)
BetterTrades
You can work with pointers in c#, however you must compile your code as unsafe, but I'm pretty sure unsafe code causes the garbage collector to take a nutty. I think you'd be better off working around how you used to work with pointers. My background isn't engineering so I don't really understand why you'd NEED to use pointers in your above example, maybe you could give an example for us poor DB programmers
I'm a former C++ programmer myself, so I can understand your initial frustration with the differences in C#. But I haven't run into anything in C# that I used to be able to do in C++ w/o a little research :v)