Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Matheson - 13 Dec 2004 13:43 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
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.
John Saunders - 13 Dec 2004 14:15 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 - 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/
Cowboy (Gregory A. Beamer) - MVP - 13 Dec 2004 14:45 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.