What is With/EndWith Used For?

Makes an easier way to set multiple properties of an object.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mySideLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Pen(Color.Black)
[/SIZE][SIZE=2][COLOR=#0000ff]  With[/COLOR][/SIZE][SIZE=2] mySideLine
   .Width = 1
   .DashStyle = Drawing2D.DashStyle.Custom
[/SIZE][SIZE=2][COLOR=#0000ff]  End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

instead of

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2][COLOR=#000000] mySideLine [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2][COLOR=#000000] Pen(Color.Black)[/COLOR]
[/SIZE][SIZE=2]mySideLine.Width = 1
mySideLine.DashStyle = Drawing2D.DashStyle.Custom
[/SIZE][SIZE=2][COLOR=#0000ff]

[/COLOR][/SIZE]It pays off more when you have many properties your are setting.
 
VB.NET:
Dim ofd As New OpenFileDialog
With ofd
  .Filter = "All Files |*.*"
  .Title = "Find File"
  .DefaultExt = ".txt"
  .FileName = "1.txt"
  .ShowDialog()
End With

the above is extremely common
 
Heh, yea, he's got a much better example :) I happened to be working with a Pen and the moment so it was on the brain. Thanks for the assist Juggalo :)
 
Back
Top