I have a problem using the class of structures made.
Here's the basic definition:
using System;
struct Real
{
public double real;
public Real(double real)
{
this.real = real;
}
}
class Record
{
public Real r;
public Record(double r)
{
this.r = new Real(r);
}
public void Test(double origval, double newval)
{
if (this.r.real == newval)
Console.WriteLine("r = newval-test passed\n");
else if (this.r.real == origval)
Console.WriteLine("r = origval-test failed\n");
else
Console.WriteLine("r = neither-test failed\n");
}
}
When I create a non-dynamic (static?) Record, install Real. When I create a dynamic record, setting up the real one does not work.
When I create a dynamic recording, I replace the real work.
And here is the test program
class Program
{
static void Main(string[] args)
{
double origval = 8.0;
double newval = 5.0;
Record record1 = new Record(origval);
record1.r.real = newval;
record1.Test(origval, newval);
dynamic dynrecord2 = new Record(origval);
dynrecord2.r.real = newval;
dynrecord2.Test(origval, newval);
dynamic dynrecord3 = new Record(origval);
dynamic r = dynrecord3.r;
r.real = newval;
dynrecord3.r = r;
dynrecord3.Test(origval, newval);
}
}
And here is the conclusion: r = newval-test passed Error r = origval-test r = newval-test passed
When I change the Real structure to the Real class, all three things work.
So what is going on?
Thanks
Max
source
share