Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to syntax with statement with multiple objects simultaneously?

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
0
0
GR
Hello, a simple question please, Is it possible to use the with - end with statement with more than one objects simultaneously??? And how???
For example

With object1
.property1=object2.property1
.property2=object2.property2
.property3=object2.property2
etc...
end with

I would like to avoid writing object2.property1, object2.property2, object2.property3, object2.property4 etc...
I tried with object1, object2 or with object1 and object2 but it's not allowed. Is there a way or should I have to write it like the above example?
 
No and nohow

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
If you switch programming language to Delphi, then Yes, otherwise no.
 
As SkipVought indicated, you cannot use With the way you want. However, you can use System.Reflection to copy property values between two objects. Here's a sub to do this:

Private Sub CopyProperties(ByVal Source As Object, ByRef Dest As Object)

Dim t1 As System.Type = Source.GetType
Dim t2 As System.Type = Dest.GetType

For Each pi1 As System.Reflection.PropertyInfo In t1.GetProperties
For Each pi2 As System.Reflection.PropertyInfo In t2.GetProperties
If pi1.Name = pi2.Name Then
If pi1.CanRead And pi2.CanWrite Then
pi2.SetValue(Dest, pi1.GetValue(Source, Nothing), Nothing)
Exit For
End If
End If
Next
Next
End Sub

I haven't had a chance to test this, but it should work.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
You could create a method

Code:
object1.CopyProperties(object2)

or you could set a temporary object to distinguish the source object with Intellisense

Code:
With object1
    Dim src = object2
    .property1 = src.property1
    .property2 = src.property2
    ' etc...
End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top