i want to pass by ref (which i did by adding the ref keyword)
now i want to hold the reference to change the value later on. I've tried casting to an object but this doesnt work :-/
also tried a pointer member variable in my class but compiler complains about it.
how am i supposed to do this in c#

Code for unmanaged primitive type and managed object storage
Kehama
Hi,
Can you give an example of your case I didn't quite grasp what you were after. Does this solve your problem
String phonenumber = "123-456789";
String operatorPart = "";
String numberPart = "";
Console.WriteLine("phonenumber: " + phonenumber);
SplitPhonenumber(phonenumber, ref operatorPart, ref numberPart);
Console.WriteLine("operator: " + operatorPart);
Console.WriteLine("number: " + numberPart);
...
public static bool SplitPhonenumber(String phonenumber, ref String operatorPart, ref String numberPart)
{
if (phonenumber.Length == 0)
{
operatorPart = "";
numberPart = "";
return false;
}
int index = phonenumber.IndexOf("-");
if (index == -1)
{
operatorPart = "";
numberPart = "";
return false;
}
operatorPart = phonenumber.Substring(0, index);
numberPart = phonenumber.Substring(index + 1);
return true;
}
~ Sand ~
sachin kumar rana
But as I understand C#, it has been designed to resemble Java not C++. C# like Java are weak when you need to have two or more pointers to the same memory location. A typical problem arises when object A creates object GG and wants to store GG in itself and in another object B. In that case, one wants the GG to reside in a single memory location (i.e. only one copy) but to have two pointers (or references) to it.
Oh by the way, the boxing/unboxing of an object always makes a copy of the object being boxed/unboxed. And it only works for managed objects, I presume.
Hmm... What do you mean with the "fixed block" The FloatUnmanaged class or the line: "unsafe { aimAngle.Store(&real); }"
About creating the float as an object: create a wrapper class that has a single member variable (the float one), getters, and setters for it. Would that suffice I'll post a few test methods and classes soon as an example.
~ Sand ~
Surinder
I got your point and made these two examples to help. The first one (TestUnmanaged) demonstrates how to save a reference (actually a c++ style pointer) to an unmanaged float value. The second (TestManaged) illustrates how to store a reference to an object (of type DataMan).
The source code in the below is actually a full class file. So, create a new class named Tester in the IDE and replace the compiler generated code with mine. Remember to add "using Test" in the beginning of the file you are using to test my stuff. Then add lines "Tester.TestUnmanaged();" and "Tester.TestManaged();" wherever you want to initiate the testing.
Implemented classes:
Tester - A simple class with two static methods to test references/pointers.
DataMan - A simple class for testing set/get of referenced objects.
FloatUnmanaged - A class for storing a pointer to a float value.
DataManaged - A class for storing a reference to a DataMan object.
And the code:
using System;
namespace Test
{
public class Tester
{
public Tester()
{
}
public static void TestUnmanaged()
{
// The actual float value that is saved in aimAngle storage.
float real = 35.5F;
// This is used to check the stored value in aimAngle storage.
float stored = 0.0F;
// Storage class holding a pointer to "real".
FloatUnmanaged aimAngle = new FloatUnmanaged();
// Store a pointer to "real" into the storage class.
unsafe { aimAngle.Store(&real); }
Console.WriteLine("\n\n Real angle at #1: " + real.ToString());
Console.WriteLine("Stored angle at #1: " + stored.ToString());
// Get the value of the stored pointer.
stored = aimAngle.GetFloat();
Console.WriteLine(" Real angle at #2: " + real.ToString());
Console.WriteLine("Stored angle at #2: " + stored.ToString());
// Change the value through the storage class
aimAngle.SetFloat(90.2F);
Console.WriteLine(" Real angle at #3: " + real.ToString());
Console.WriteLine("Stored angle at #3: " + stored.ToString());
// Get the supposedly changed value of the stored pointer.
stored = aimAngle.GetFloat();
Console.WriteLine(" Real angle at #4: " + real.ToString());
Console.WriteLine("Stored angle at #4: " + stored.ToString());
// Change directly the "real" value.
real = 140.9F;
Console.WriteLine(" Real angle at #5: " + real.ToString());
Console.WriteLine("Stored angle at #5: " + stored.ToString());
// Get the supposedly changed value of the stored pointer.
stored = aimAngle.GetFloat();
Console.WriteLine(" Real angle at #6: " + real.ToString());
Console.WriteLine("Stored angle at #6: " + stored.ToString());
}
public static void TestManaged()
{
// The actual DataMan object that is saved in holder storage.
DataMan me = new DataMan(30, 180);
// This is used to check the stored value in holder storage.
DataMan test = null;
// Storage class holding a reference to "me".
DataManaged holder = new DataManaged();
// Store a pointer to "real" into the storage class.
holder.Store(me);
Console.WriteLine("\n\nReal me at #1 age: " + me.Age.ToString() + ", height: " + me.Height.ToString());
if (test != null)
Console.WriteLine("Test me at #1 age: " + test.Age.ToString() + ", height: " + test.Height.ToString());
else
Console.WriteLine("Test me at #1 age: null");
// Get the value of the stored pointer.
test = holder.GetData();
Console.WriteLine("Real me at #2 age: " + me.Age.ToString() + ", height: " + me.Height.ToString());
if (test != null)
Console.WriteLine("Test me at #2 age: " + test.Age.ToString() + ", height: " + test.Height.ToString());
else
Console.WriteLine("Test me at #2 age: null");
// Change the value through the storage class
holder.SetData(44, 213);
Console.WriteLine("Real me at #3 age: " + me.Age.ToString() + ", height: " + me.Height.ToString());
if (test != null)
Console.WriteLine("Test me at #3 age: " + test.Age.ToString() + ", height: " + test.Height.ToString());
else
Console.WriteLine("Test me at #3 age: null");
// Get the supposedly changed value of the stored reference.
test = holder.GetData();
Console.WriteLine("Real me at #4 age: " + me.Age.ToString() + ", height: " + me.Height.ToString());
if (test != null)
Console.WriteLine("Test me at #4 age: " + test.Age.ToString() + ", height: " + test.Height.ToString());
else
Console.WriteLine("Test me at #4 age: null");
// Change directly the "me" value.
me.Age = 111;
me.Height = 85;
Console.WriteLine("Real me at #5 age: " + me.Age.ToString() + ", height: " + me.Height.ToString());
if (test != null)
Console.WriteLine("Test me at #5 age: " + test.Age.ToString() + ", height: " + test.Height.ToString());
else
Console.WriteLine("Test me at #5 age: null");
// Get the supposedly changed value of the stored reference.
test = holder.GetData();
Console.WriteLine("Real me at #6 age: " + me.Age.ToString() + ", height: " + me.Height.ToString());
if (test != null)
Console.WriteLine("Test me at #6 age: " + test.Age.ToString() + ", height: " + test.Height.ToString());
else
Console.WriteLine("Test me at #6 age: null");
}
}
public class DataMan : System.Object
{
private int _age;
private int _height;
public int Age
{
set
{
_age = value;
}
get
{
return _age;
}
}
public int Height
{
set
{
_height = value;
}
get
{
return _height;
}
}
public DataMan(int age, int height)
{
_age = age;
_height = height;
}
}
public class FloatUnmanaged
{
private unsafe float* _pointee;
public FloatUnmanaged()
{
}
public unsafe FloatUnmanaged(float* storage)
{
_pointee = storage;
}
public unsafe void Store(float* storage)
{
_pointee = storage;
}
public unsafe void SetFloat(float val)
{
if (_pointee == null)
return;
*_pointee = val;
}
public unsafe float GetFloat()
{
if (_pointee == null)
return 0.0F;
return *_pointee;
}
}
public class DataManaged
{
private DataMan _pointee;
public DataManaged()
{
}
public DataManaged(DataMan person)
{
_pointee = person;
}
public void Store(DataMan person)
{
_pointee = person;
}
public void SetData(int age, int height)
{
if (_pointee == null)
return;
_pointee.Age = age;
_pointee.Height = height;
}
public DataMan GetData()
{
if (_pointee == null)
return null;
return _pointee;
}
}
}
~ Sand ~
RAi SWETA
class Test
{
object ThisShouldBeARef;
void SetVariable(ref float test)
{
ThisShouldBeARef = test;
}
}
so when my class changes ThisShouldBeARef then the float passed in to SetVariable should be changed (so like a pointer in c++)
Saishyam
You could have a pointer to float member by changing the project property "Allow unsafe code blocks" and having
unsafe float * ThisShouldBeARef;
and pass the float in with
unsafe void SetVariable(float * test){...}
Or, if you'd rather avoid unsafe code, you could create a very simple class to hold the float, and pass this object into the SetVariable method - now ThisShouldBeARef would hold a handle to that object and any changes made on its member float would change on the passed in object.
timmid
btw wont you get problems when assignign the pointer as you need to do it in a fixed block.
also could i create my float as an object so when i do pass it around it gets treated as a reference.
James Robertson
public static void TestDoubleManaged()
{
Double real = 12.0;
Double stored = 0.0;
DoubleManaged store = new DoubleManaged(real);
Console.WriteLine("\n\n Real angle at #1: " + real.ToString());
Console.WriteLine("Stored angle at #1: " + stored.ToString());
stored = store.GetData();
Console.WriteLine(" Real angle at #2: " + real.ToString());
Console.WriteLine("Stored angle at #2: " + stored.ToString());
store.SetValue(4.4);
Console.WriteLine(" Real angle at #3: " + real.ToString());
Console.WriteLine("Stored angle at #3: " + stored.ToString());
stored = store.GetData();
Console.WriteLine(" Real angle at #4: " + real.ToString());
Console.WriteLine("Stored angle at #4: " + stored.ToString());
real = 98.7;
Console.WriteLine(" Real angle at #5: " + real.ToString());
Console.WriteLine("Stored angle at #5: " + stored.ToString());
stored = store.GetData();
Console.WriteLine(" Real angle at #6: " + real.ToString());
Console.WriteLine("Stored angle at #6: " + stored.ToString());
}
public static void TestDoubleStoreManaged()
{
double val = 12.0;
DoubleStore real = new DoubleStore(val);
DoubleStore stored = null;
DoubleStoreManaged store = new DoubleStoreManaged(real);
Console.WriteLine("\n\n Real angle at #1: " + real.Value.ToString());
if (stored != null)
Console.WriteLine("Stored angle at #1: " + stored.Value.ToString());
else
Console.WriteLine("Stored angle at #1: null");
stored = store.GetData();
Console.WriteLine(" Real angle at #2: " + real.Value.ToString());
if (stored != null)
Console.WriteLine("Stored angle at #2: " + stored.Value.ToString());
else
Console.WriteLine("Stored angle at #2: null");
store.SetValue(4.4);
Console.WriteLine(" Real angle at #3: " + real.Value.ToString());
if (stored != null)
Console.WriteLine("Stored angle at #3: " + stored.Value.ToString());
else
Console.WriteLine("Stored angle at #3: null");
stored = store.GetData();
Console.WriteLine(" Real angle at #4: " + real.Value.ToString());
if (stored != null)
Console.WriteLine("Stored angle at #4: " + stored.Value.ToString());
else
Console.WriteLine("Stored angle at #4: null");
real.Value = 98.7;
Console.WriteLine(" Real angle at #5: " + real.Value.ToString());
if (stored != null)
Console.WriteLine("Stored angle at #5: " + stored.Value.ToString());
else
Console.WriteLine("Stored angle at #5: null");
stored = store.GetData();
Console.WriteLine(" Real angle at #6: " + real.Value.ToString());
if (stored != null)
Console.WriteLine("Stored angle at #6: " + stored.Value.ToString());
else
Console.WriteLine("Stored angle at #6: null");
}
public static void TestObjectManaged()
{
float real = 12.0F;
float stored = 0.0F;
ObjectManaged store = new ObjectManaged(real);
Console.WriteLine("\n\n Real angle at #1: " + real.ToString());
Console.WriteLine("Stored angle at #1: " + stored.ToString());
stored = (float) store.Ref;
Console.WriteLine(" Real angle at #2: " + real.ToString());
Console.WriteLine("Stored angle at #2: " + stored.ToString());
store.Ref = 4.4F;
Console.WriteLine(" Real angle at #3: " + real.ToString());
Console.WriteLine("Stored angle at #3: " + stored.ToString());
stored = (float) store.Ref;
Console.WriteLine(" Real angle at #4: " + real.ToString());
Console.WriteLine("Stored angle at #4: " + stored.ToString());
real = 98.7F;
Console.WriteLine(" Real angle at #5: " + real.ToString());
Console.WriteLine("Stored angle at #5: " + stored.ToString());
stored = (float) store.Ref;
Console.WriteLine(" Real angle at #6: " + real.ToString());
Console.WriteLine("Stored angle at #6: " + stored.ToString());
}
public class DoubleStore : System.Object
{
private double _value;
public double Value
{
set
{
_value = value;
}
get
{
return _value;
}
}
public DoubleStore(double val)
{
_value = val;
}
}
public class DoubleStoreManaged
{
private DoubleStore _pointee;
public DoubleStoreManaged()
{
_pointee.Value = 0.0;
}
public DoubleStoreManaged(DoubleStore val)
{
_pointee = val;
}
public void Store(DoubleStore val)
{
_pointee = val;
}
public void SetValue(double val)
{
_pointee.Value = val;
}
public DoubleStore GetData()
{
return _pointee;
}
public double GetValue()
{
return _pointee.Value;
}
}
public class DoubleManaged
{
private Double _pointee;
public DoubleManaged()
{
_pointee = 0.0;
}
public DoubleManaged(Double val)
{
_pointee = val;
}
public void Store(Double val)
{
_pointee = val;
}
public void SetValue(double val)
{
_pointee = val;
}
public Double GetData()
{
return _pointee;
}
}
public class ObjectManaged
{
object _reference;
public object Ref
{
set
{
_reference = value;
}
get
{
return _reference;
}
}
public ObjectManaged()
{
}
public ObjectManaged(object store)
{
_reference = store;
}
}
Huh, some code ;-)
~ Sand ~