A reference to value type (double)

W

wxdeveloper

Hello,

I am a C++ developer converting to C# but I am still thinking in C++
way :)

In C++ I can do this:

double* p1 = new double;
double* p2 = p1;
*p1 = 5.0;
*p2 = 7.0;

Now both *p1 and *p2 have value 7 (they are pointing the same address
in memory).
Now I would like to achieve the same thing in C# (I would like to have
a reference to double) but if I do this:

System.Double a = new System.Double();
System.Double b = a;

a = 5;
b = 7;

It will not work. I understand that his because double is a value type
and it cannot be used as a reference no matter what (System.Double is
value type).
So far the only other way to go around this problem is to use a class
with double value inside than I can create as many references to the
object as I need.
Is my thinking correct ?

Thank you
J. Grabis
 
A

Arne Vajhøj

wxdeveloper said:
Hello,

I am a C++ developer converting to C# but I am still thinking in C++
way :)

In C++ I can do this:

double* p1 = new double;
double* p2 = p1;
*p1 = 5.0;
*p2 = 7.0;

Now both *p1 and *p2 have value 7 (they are pointing the same address
in memory).
Now I would like to achieve the same thing in C# (I would like to have
a reference to double) but if I do this:

System.Double a = new System.Double();
System.Double b = a;

a = 5;
b = 7;

It will not work. I understand that his because double is a value type
and it cannot be used as a reference no matter what (System.Double is
value type).
So far the only other way to go around this problem is to use a class
with double value inside than I can create as many references to the
object as I need.
Is my thinking correct ?

I think you want to use a class if you want this behavior (which I
do not quite understand why that should be necessary for your
application).

Note though that C# supports pointers in unsafe blocks.

Arne
 
G

Gregory A. Beamer

Now both *p1 and *p2 have value 7 (they are pointing the same address
in memory).
Now I would like to achieve the same thing in C#

In C#, you do things a bit differently. First, you have to set up the
project to allow unsafe code. To do this, go to project properties and
then the build tab. You then check "allow unsafe code".

Second, you have to mark the code unsafe:

unsafe
{
//Code with pointers here
}

Then you have to attach the actual value type to the pointer. For
example, this is valid:

double d = 5;
double* p1 = & d;
double* p2 = p1;

Now all pointers point to d. You can then manipulate d and both pointers
will be updated. You can also set the pointer. Here is a console
application to illustrate:

class MyClient
{
public static void Main()
{
unsafe
{
double d = 5;
double *p1 = & d;
double *p2 = p1;
d = 7.0;

Console.WriteLine(*p1);
Console.WriteLine(*p2);

*p2 = 6.0;

Console.WriteLine(*p1);
Console.WriteLine(*p2);
}

Console.Read();
}
}

The main differences from C++:

1. Have to declare code unsafe
2. There is no implicit creation of the underlying value type, as in C++

Peace and Grace,




--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
W

wxdeveloper

I think you want to use a class if you want this behavior (which I
do not quite understand why that should be necessary for your
application).

Note though that C# supports pointers in unsafe blocks.

Arne

Well, I am converting some solution from C++ to C#.
Here's an example of what I have in C++ (it is simplified just to give
you an idea):

class A
{
A(double* pDouble) { m_DoubleValue = pDouble }
double* m_pDoubleValue;
}

class B
{
B() { m_pHolder = new A(&m_dValue); }


double m_dValue;
A* m_pHolder;

}

The idea is to find out in class A that field of class B has been
modified (in real solution B has a collection of holders to keep
pointers to different fields).
It works well with StringBuilder in C# (StringBuilder is a class).
Know I would have to create "dummy" class for each base type (double,
int, bool, etc.) to have it stored as a reference.
I know that unsafe would solve my problem but I do want to avoid this.

Thanks
J.
 
P

Peter Duniho

wxdeveloper said:
Well, I am converting some solution from C++ to C#.

For better or worse, if you want to port code from C++ to C#
effectively, you need to be willing to adjust the paradigm in the code
to match the natural idioms and mechanics in the target language.
[...]
The idea is to find out in class A that field of class B has been
modified (in real solution B has a collection of holders to keep
pointers to different fields).

Of course, in actuality, at least based on the code you posted, having a
pointer in A to the field in B doesn't do anything at all to help A know
when the field in B has _changed_. It just allows class A to inspect
the value whenever it wants without knowing the value resides in class B.

As far as notifying of a change goes, in .NET (and C#) the usual idiom
for this sort of thing is to have class A subscribe to an event in class
B, and have class B raise the event when the value has changed. Then
class A can inspect the value in class B directly.
It works well with StringBuilder in C# (StringBuilder is a class).
Know I would have to create "dummy" class for each base type (double,
int, bool, etc.) to have it stored as a reference.

IMHO a "dummy class" is unnecessary, and wasteful. If class A wants to
know the current value of the value in class B, it should keep a
reference to class B and inspect class B. Class B is already a "holder"
of the value; why create another one?

And if you want class A to be notified when the value changes, you'll
need some sort of alert mechanism for that, which in C# is typically
implemented using an event.
I know that unsafe would solve my problem but I do want to avoid this.

Actually, I doubt you could implement it in unsafe code anyway, unless
you simply kept all your data structures pinned all the time, which
isn't feasible.

Pete
 
A

Arne Vajhøj

wxdeveloper said:
Well, I am converting some solution from C++ to C#.
Here's an example of what I have in C++ (it is simplified just to give
you an idea):

class A
{
A(double* pDouble) { m_DoubleValue = pDouble }
double* m_pDoubleValue;
}

class B
{
B() { m_pHolder = new A(&m_dValue); }


double m_dValue;
A* m_pHolder;

}

The idea is to find out in class A that field of class B has been
modified (in real solution B has a collection of holders to keep
pointers to different fields).
It works well with StringBuilder in C# (StringBuilder is a class).
Know I would have to create "dummy" class for each base type (double,
int, bool, etc.) to have it stored as a reference.
I know that unsafe would solve my problem but I do want to avoid this.

I don't think I like the approach in neither C# nor C++.

It seems to me that the best solution is to use the observer pattern.

If you insist then use a wrapper class. You may be able to use a
single generic class instead of multiple classes.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top