Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2007

Tip: Looking for answers? Try searching our database.

Need advice please.With-End With

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim - 21 Mar 2007 15:20 GMT
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
VJ - 21 Mar 2007 16:46 GMT
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

> Is there any performance advantage if I follow example 2.
> Which one is preferred.
[quoted text clipped - 17 lines]
> Thanks for you suggestion you can provide.
> Tim
Jon Skeet [C# MVP] - 21 Mar 2007 21:11 GMT
> by MSDN doc..
>
[quoted text clipped - 4 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jon Skeet [C# MVP] - 21 Mar 2007 21:10 GMT
> Is there any performance advantage if I follow example 2.
> Which one is preferred.
[quoted text clipped - 16 lines]
>
> 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".

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

VJ - 21 Mar 2007 22:31 GMT
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

>> Is there any performance advantage if I follow example 2.
>> Which one is preferred.
[quoted text clipped - 27 lines]
>
> Personally that's what I'd do - I'm not a fan of "With".
Jon Skeet [C# MVP] - 21 Mar 2007 23:06 GMT
> 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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

VJ - 22 Mar 2007 02:41 GMT
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

>> Ok I do certainly understand what you are saying.. so adding a variable
>> is
[quoted text clipped - 38 lines]
>
> End Class
Tim - 22 Mar 2007 18:53 GMT
Thanks VJ and Jon.

> Yes after I posted here that is what I did (write and check the ILDASM)
> and got it. Thanks a lot for explaination Jon..
[quoted text clipped - 43 lines]
>>
>> End Class

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.