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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# equivalent of VB's With statement

Status
Not open for further replies.

digiduck

Programmer
Apr 4, 2003
95
US
What is the C# equivalent of VB's 'With/End With' statement?
 
It doesn't have one, per-se.

What you can do, though is if you have a deeply nested object that you want to do the equivalent of the With..EndWith to, is assign it to a temporary variable, so your list of dots isn't so long.

For example, if you have a Forest.Tree.Branch.Twig.Leaf hierarchy, you can create a temporary Twig object to shorten the code somewhat. So instead of:
Code:
Forest.Tree(0).Branch(2).Twig(0).Leaf(0).Color = Red;
Forest.Tree(0).Branch(2).Twig(0).Leaf(1).Color = Yellow;
Forest.Tree(0).Branch(2).Twig(0).Leaf(2).Color = Yellow;
Forest.Tree(0).Branch(2).Twig(0).Leaf(3).Color = Green;
you can do something like:
Code:
Twig MyTwig = Forest.Tree(0).Branch(2).Twig(0);
MyTwig.Leaf(0).Color = Red;
MyTwig.Leaf(1).Color = Yellow;
MyTwig.Leaf(2).Color = Yellow;
MyTwig.Leaf(3).Color = Green;
The technique is probably only useful when you're setting/accessing 5 or more properties/methods in a row.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top