Custom Cast

  • Thread starter Thread starter tascien
  • Start date Start date
T

tascien

I know vb.net does not support operator overloading... I want to creat
a simple custom casting... this will just take the public members of
one class, and assign their values to public members of another
class... methods and functions are ignored...

class type1
public value1, value2, value3
end class

class type2
public value1, value2, value3
end class

i want to make a function that does this:

dim t2 as new type2
t2.value1 = "this is value 1 in t2"
t2.value2 = "this is value 2 in t2"
t2.value3 = "this is value 3 in t2"

dim t1 as type1 = ChangeType(t2, Type1)

now, t1's value1, value2 and value3, will contain the same information
as in t2's...

Just as simple as that... Don't want complicated matter...

Note: ChangeType function is independent of class passed to it. it can
accept any classes, as long as they have members that match members in
converted class... if corresponding name is not found in the
destination class, then that member is ignored...

Thanks for your help in advance...
 
tascien said:
I know vb.net does not support operator overloading... I want to creat
a simple custom casting... this will just take the public members of
one class, and assign their values to public members of another
class... methods and functions are ignored...

class type1
public value1, value2, value3
end class

class type2
public value1, value2, value3
end class

i want to make a function that does this:

dim t2 as new type2
t2.value1 = "this is value 1 in t2"
t2.value2 = "this is value 2 in t2"
t2.value3 = "this is value 3 in t2"

dim t1 as type1 = ChangeType(t2, Type1)

now, t1's value1, value2 and value3, will contain the same information
as in t2's...

Just as simple as that... Don't want complicated matter...

Note: ChangeType function is independent of class passed to it. it can
accept any classes, as long as they have members that match members in
converted class... if corresponding name is not found in the
destination class, then that member is ignored...

I suggest you look into reflection, and GetProperties (or GetFields,
but I hope you don't have any public fields in the first place) in
particular.
 
Back
Top