Nouveautés de VB2008

  • Thread starter Thread starter Gilbert Tordeur
  • Start date Start date
G

Gilbert Tordeur

Bonjour.

N'y aurait-il pas depuis la VB8 un moyen d'assigner en une seule instruction
l'ensemble des propriétés d'un objet aux propriétés de mêmes noms d'un autre
objet, plutôt que d'écrire la liste fastidieuse :

O2.P1 = O1.P1
O2.P2 = O1.P2
O3.P2 = O1.P3
etc.

Une sorte de MOVE CORRESPONDING en COBOL en quelque sorte.

Il me semble avoir entendu parler de quelque chose de semblable lors d'une
présentation de LINQ, mais le souvenir est trop vague.

Merci de votre aide,
Gilbert
 
Bonjour.

N'y aurait-il pas depuis la VB8 un moyen d'assigner en une seule instruction
l'ensemble des propriétés d'un objet aux propriétés de mêmes noms d'un autre
objet, plutôt que d'écrire la liste fastidieuse :

O2.P1 = O1.P1
O2.P2 = O1.P2
O3.P2 = O1.P3
etc.

Une sorte de MOVE CORRESPONDING en COBOL en quelque sorte.

Il me semble avoir entendu parler de quelque chose de semblable lors d'une
présentation de LINQ, mais le souvenir est trop vague.

Merci de votre aide,
Gilbert

English please?

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Sorry, in English :

Hello,
Is there a way since VB2008 to assign with only one statement all the
properties of an object to the (same name) properties of another object,
instead of coding number of trivial lines like
O2.P1 = O1.P1
O2.P2 = O1.P2
O2.P3 = O1.P3
etc.

Something like a MOVE CORRESPONDING in COBOL.

I think I have heard somethink like this in a LINQ meeting sometime ago, but
I do not remember. Thank you for your help.
Gilbert
 
Sorry, in English :

Hello,
Is there a way since VB2008 to assign with only one statement all the
properties of an object to the (same name) properties of another object,
instead of coding number of trivial lines like


Something like a MOVE CORRESPONDING in COBOL.

I think I have heard somethink like this in a LINQ meeting sometime ago, but
I do not remember. Thank you for your help.
Gilbert

I don't think there is a built in method (I could be wrong).

Once upon a time I wrote a piece using Reflection that did this, but
I'm not sure where that code is. Basically you do a GetProperties on
the source class and then you attempt to do a set value for each of
the properties in the destination class, not the cleanest solution,
but it works.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
OK, I think I can do that.
Thank you,
Gilbert

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...
Sorry, in English :

Hello,
Is there a way since VB2008 to assign with only one statement all the
properties of an object to the (same name) properties of another object,
instead of coding number of trivial lines like


Something like a MOVE CORRESPONDING in COBOL.

I think I have heard somethink like this in a LINQ meeting sometime ago,
but
I do not remember. Thank you for your help.
Gilbert

I don't think there is a built in method (I could be wrong).

Once upon a time I wrote a piece using Reflection that did this, but
I'm not sure where that code is. Basically you do a GetProperties on
the source class and then you attempt to do a set value for each of
the properties in the destination class, not the cleanest solution,
but it works.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
OK, I think I can do that.
Thank you,
Gilbert

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...





I don't think there is a built in method (I could be wrong).

Once upon a time I wrote a piece using Reflection that did this, but
I'm not sure where that code is. Basically you do a GetProperties on
the source class and then you attempt to do a set value for each of
the properties in the destination class, not the cleanest solution,
but it works.

Thanks,

Seth Rowe [MVP]http://sethrowe.blogspot.com/

I just threw some code onto my blog that should do this. As a warning
it's completely untested!

http://sethrowe.blogspot.com/2008/08/property-moving-class.html

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Gilbert said:
Is there a way since VB2008 to assign with only one statement all the
properties of an object to the (same name) properties of another
object, instead of coding number of trivial lines like

I could be getting the wrong idea, but isn't that what .Clone is for?

Andrew
 
I could be getting the wrong idea, but isn't that what .Clone is for?

Andrew

Clone doesn't work if you want to change class types (which is what I
took as what the OP was looking for). It also will only work with the
class implements ICloneable.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Andrew,
I think .Clone is for the same class, but if you copy to an object of
another class ?
Regards,
Gilbert
 
OK, I got it. I will try and let you know.
Regards,
Gilbert

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...
OK, I think I can do that.
Thank you,
Gilbert

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...





I don't think there is a built in method (I could be wrong).

Once upon a time I wrote a piece using Reflection that did this, but
I'm not sure where that code is. Basically you do a GetProperties on
the source class and then you attempt to do a set value for each of
the properties in the destination class, not the cleanest solution,
but it works.

Thanks,

Seth Rowe [MVP]http://sethrowe.blogspot.com/

I just threw some code onto my blog that should do this. As a warning
it's completely untested!

http://sethrowe.blogspot.com/2008/08/property-moving-class.html

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Gilbert said:
Is there a way since VB2008 to assign with only one statement all the
properties of an object to the (same name) properties of another object,
instead of coding number of trivial lines like

To copy absolutely /everything/, implement a Clone() method.

To create one object [class] based on another object [class], create the
relevant constructor in the second class:

Class C1
Public Sub New()
End Sub
End Class

Class C2
Public Sub New( ByVal source as C1 )
End Sub
End Class

Dim o1 as New C1()
Dim o2 as New C2( o1 )

Even if there /was/ a way of doing this "automagically", personally, I'd
avoid it; I like to [think I] know what my code is actually doing! :-)
YMMV.

HTH,
Phill W.
 
Back
Top