Tag Archives: with endwith

Use With/EndWith to Save Time and Typing in VB & VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

This simple tip is just to show how you can do less typing and save time using the ‘With’ statement. This statement is nothing new as it has been around since the VB Classic (6.0) days. I usually do not remember to use this statement when I could benefit from it. So I wanted to write this post to simply remind those who may have forgotten about it or just don’t think about it when it could save time and do less typing. It didn’t occur to me until I had to refer to a Listview control many times to add information. It then came to me and by using this statement it saved me some time. IF you have to refer to a control or whatever and set alot of its properties or methods, then “With/EndWith” should be a welcome time saver. Below is a example of using he ‘With’ statement…

'Use a WITH statement to save some typing and time since you won't have keep referring
'to the object when you are setting its propertys and functions and what-not. LV is
'a Listview control.

With lv

  For x AsInteger = 0 To length - 1 

    f = New IO.FileInfo(s.GetValue(x))




    .Items.Item(x).SubItems.Add(f.Length / 1000 & " KBs") 



End With

Just a tip for those who may not know.