>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
[quoted text clipped - 15 lines]
>
> Can anyone give me an idea how to do this?
Is this a job interview question?
ValueType MyMethod(ValueType data)
{
return data;
}
John Saunders
Robert Matheson - 13 Dec 2004 14:31 GMT
> >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
[quoted text clipped - 24 lines]
>
> 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.
Anders Nor?s [MCAD] - 13 Dec 2004 14:49 GMT
> 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/
Robert Matheson - 13 Dec 2004 16:21 GMT
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.
"Anders Norås [MCAD]" wrote:
> > I need to be able to make a copy which can be modified without affecting
> > the
[quoted text clipped - 25 lines]
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras/
Robert Matheson - 14 Dec 2004 10:01 GMT
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".
"Anders Norås [MCAD]" wrote:
> > I need to be able to make a copy which can be modified without affecting
> > the
[quoted text clipped - 25 lines]
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras/
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 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
[quoted text clipped - 16 lines]
>
> Robert.
Robert Matheson - 14 Dec 2004 10:05 GMT
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".
> private ValueType GetValue(ValueType i)
> {
[quoted text clipped - 59 lines]
> >
> > Robert.