How can I copy a value type dynamically without knowing the type.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to be able to dynamically create a new copy of a value type without
knowing the type at compile time. i.e. I need to be able to write a method
such as:

ValueType MyMethod(ValueType data)
{
return copy-of-data;
}

which returns a new ValueType that is a copy of the 'data' parameter.
Returning the 'data' parameter itself is no good as it returns the boxed
value which is passed into the method, and therefore references the original
variable.

I would have thought that there would be a simple way to do this, but I
can't think of it.

Can anyone give me an idea how to do this?

Robert.
 
Robert Matheson said:
I need to be able to dynamically create a new copy of a value type without
knowing the type at compile time. i.e. I need to be able to write a method
such as:

ValueType MyMethod(ValueType data)
{
return copy-of-data;
}

which returns a new ValueType that is a copy of the 'data' parameter.
Returning the 'data' parameter itself is no good as it returns the boxed
value which is passed into the method, and therefore references the
original
variable.

I would have thought that there would be a simple way to do this, but I
can't think of it.

Can anyone give me an idea how to do this?

Is this a job interview question?

ValueType MyMethod(ValueType data)
{
return data;
}


John Saunders
 
John Saunders said:
Is this a job interview question?

ValueType MyMethod(ValueType data)
{
return data;
}


John Saunders

That doesn't work for the reasons I stated above. Although ValueType is the
base class of all value types, it itself behaves as a Reference Type and the
value passed back is a reference to the original variable and not a new copy.
I need to be able to make a copy which can be modified without affecting the
original variable.

Robert.
 
private ValueType GetValue(ValueType i)
{
return i;
}

will work if you are assigning to a value. Take, for example, this code:

public static void Main(string [] args)
{
int i = 0;
int j = (int) GetValueType(i);

Console.WriteLine("Original i = " + i.ToString());
Console.WriteLine("Original j = " + j.ToString());

i=1;

Console.WriteLine("i is now 1 = " + i.ToString());
Console.WriteLine("Current j = " + j.ToString());


Console.Read();

}

will yield:

Original i = 0
Original j = 0
i is now 1 = 1
Current j = 0

I assume I am missing something?


---

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

***************************
Think Outside the Box!
***************************
 
I need to be able to make a copy which can be modified without affecting
the
original variable.
Just do as John suggested.

public class MyClass
public static void Main()
{
ValueType value1=1;
ValueType value2=MyMethod(value1);
value1=2;
Console.WriteLine((int)value1); // Prints 2
Console.WriteLine((int)value2); // Prints 1
}

public static ValueType MyMethod(ValueType data)
{
return data;
}
}

The applicaiton above prints:
2
1

The value of value2 is not affected by the change to value1.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Apologies - this code obviously does work fine, though it doesn't solve my
own problem, which is buried in several layers of software. I tried to get it
down to a manageable level but obviously went too far.

Thanks for the help.
 
I have now managed to generate some code which I think shows that the copy is
still a reference to the original object (or in some other way is still able
to modify it), but it rather more involved as it only seems to show up using
Reflection and ref parameters.

As a result, I have posted it under a new topic of "Unexpected interaction
of ValueType, Reflection and ref parameter".
 
Thanks for the suggestion. Unfortunately it doesn't work in my code as all I
can see is my variable being changed! I have since tracked it down further
and the problem seems to show up when the variable copy is used subsequently
as a ref parameter when calling a method via Reflection.

As a result, I have posted it under a new topic of "Unexpected interaction
of ValueType, Reflection and ref parameter".

Cowboy (Gregory A. Beamer) - MVP said:
private ValueType GetValue(ValueType i)
{
return i;
}

will work if you are assigning to a value. Take, for example, this code:

public static void Main(string [] args)
{
int i = 0;
int j = (int) GetValueType(i);

Console.WriteLine("Original i = " + i.ToString());
Console.WriteLine("Original j = " + j.ToString());

i=1;

Console.WriteLine("i is now 1 = " + i.ToString());
Console.WriteLine("Current j = " + j.ToString());


Console.Read();

}

will yield:

Original i = 0
Original j = 0
i is now 1 = 1
Current j = 0

I assume I am missing something?


---

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

***************************
Think Outside the Box!
***************************


Robert Matheson said:
I need to be able to dynamically create a new copy of a value type without
knowing the type at compile time. i.e. I need to be able to write a method
such as:

ValueType MyMethod(ValueType data)
{
return copy-of-data;
}

which returns a new ValueType that is a copy of the 'data' parameter.
Returning the 'data' parameter itself is no good as it returns the boxed
value which is passed into the method, and therefore references the original
variable.

I would have thought that there would be a simple way to do this, but I
can't think of it.

Can anyone give me an idea how to do this?

Robert.
 
Back
Top