Need advice please.With-End With

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Is there any performance advantage if I follow example 2.
Which one is preferred.

Example 1
Dataset.Table.Column1.DefaultValue =SomeThing
Dataset.Table.Column2.DefaultValue =SomeThing
Dataset.Table.Column3.DefaultValue =SomeThing
....
....

Example 2
With Dataset.Table
.Column1.DefaultValue=SomeThing
.Column2.DefaultValue=SomeThing
.Column3.DefaultValue=SomeThing
...
...
End With

Thanks for you suggestion you can provide.
Tim
 
by MSDN doc..

"In Visual Basic you must usually specify an object in every statement that
calls one of its methods or accesses one of its properties. However, if you
have a series of statements that all operate on the same object, you can use
a With...End With structure to specify the object once for all of the
statements. This can make your procedures run faster and help you avoid
repetitive typing."

VJ
 
Tim said:
Is there any performance advantage if I follow example 2.
Which one is preferred.

Example 1
Dataset.Table.Column1.DefaultValue =SomeThing
Dataset.Table.Column2.DefaultValue =SomeThing
Dataset.Table.Column3.DefaultValue =SomeThing
...
...

Example 2
With Dataset.Table
.Column1.DefaultValue=SomeThing
.Column2.DefaultValue=SomeThing
.Column3.DefaultValue=SomeThing
...
...
End With

Thanks for you suggestion you can provide.

There's a *tiny* performance improvement in the second, in that it's
not looking at the "Table" property each time. That's all. You could
easily get the same effect with:

DataTable table = Dataset.Table
table.Column1.DefaultValue=SomeThing
table.Column2.DefaultValue=SomeThing
table.Column3.DefaultValue=SomeThing

Personally that's what I'd do - I'm not a fan of "With".
 
VJ said:
by MSDN doc..

"In Visual Basic you must usually specify an object in every statement that
calls one of its methods or accesses one of its properties. However, if you
have a series of statements that all operate on the same object, you can use
a With...End With structure to specify the object once for all of the
statements. This can make your procedures run faster and help you avoid
repetitive typing."

Yes, but that doesn't explain *why* it's faster, which is because
writing out the same expression multiple times requires that the
expression (in this case DataSet.Table) is evaluated multiple times.
That can be avoided without using a "With" block just by using a local
variable.
 
Ok I do certainly understand what you are saying.. so adding a variable is
in that one point additional memory space?.. and second point again doing
Table.Columns three times, that gets to have "table" ( your local variable)
evaluated 3 times.. so isn't With the better way than a local variable

Unless I missed the bigger picture here !

VJ
 
VJ said:
Ok I do certainly understand what you are saying.. so adding a variable is
in that one point additional memory space?..

No, as they compile to the same code.
and second point again doing Table.Columns three times, that gets to
have "table" ( your local variable) evaluated 3 times..

Evaluating a local variable is very quick. Evaluating a property may
not be. That's the difference.
so isn't With the better way than a local variable

Unless I missed the bigger picture here !

Well, the bigger picture starts with the fact that With/End With gets
compiled to the same code as using a local variable. How else would you
expect the IL to look?

Compile the following code, and compare the compiled methods in ILDASM:

Public Class Foo

Dim x As String = "Hello"

Public Shared Sub Main
Dim y as new Foo
With y.x
Console.WriteLine (.Length)
Console.WriteLine (.Substring(1))
End With
End Sub

Public Shared Sub Main2
Dim y as new Foo
Dim s as String = y.x
Console.WriteLine (s.Length)
Console.WriteLine (s.Substring(1))
End Sub

End Class
 
Yes after I posted here that is what I did (write and check the ILDASM) and
got it. Thanks a lot for explaination Jon..

VJ
 
Back
Top