Create new object on the fly from another object type

  • Thread starter Thread starter sebascomeau
  • Start date Start date
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!!!
 
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.
 
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;

}

}
 
Back
Top