Create new object on the fly from another object type

S

sebascomeau

Hi,

I don't know if this is possible but a just want to know if it is.
I want to create a new object on the fly from another object with the
same properties but all properties was another type like String.

Example :

Object to copy have this properties :
- Id (Int)
- FirstName (String)
- LastName (String)
- IsMen (Bool)

The new object created on the fly result :
- Id (String)
- FirstName (String)
- LastName (String)
- IsMen (String)

Let me know if this is possible or not, or if you want more
information contact me.
Sorry for my english, I'm french! Thanks!!!
 
N

Nicholas Paldino [.NET/C# MVP]

The only way you can do this is if you create a separate type and then
perform a copy of the one object to another manually (meaning, copying all
the other properties, and converting the Id property).

Hope this helps.
 
Joined
Jul 18, 2011
Messages
1
Reaction score
0
Simply add a method to the object that returns a new instance of the same type poplated with the same property values.


public class foo

{



public string propertyA


{
get; set; }



public string propertyB

{

get; set; }



public string propertyC

{

get; set; }



public string propertyD

{

get; set; }



public foo copy()

{



foo _foo = new foo();

_foo.propertyA =

this.propertyA;

_foo.propertyB =

this.propertyB;

_foo.propertyC =

this.propertyC;

_foo.propertyD =

this.propertyD;



return _foo;

}

}
 

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