Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have multiple objects in a With statement in VB?

Tags:

vb.net

I have a lot of labels in VB that I use in With statement to set their properties.

Problem Is there any way I can do something like the following:

With lblA, lblB, lblC
    .fontColor = color.Red
End With

Is this possible, or do I have to manually do a With statement for each of them?

like image 533
DemCodeLines Avatar asked Nov 19 '12 00:11

DemCodeLines


People also ask

What does the with command do in VBA?

VBA With is a statement to specify an object for once and then run multiple statements for it. In simple words, by using the “WITH” statement to specify an object, and after that, you can access all the properties and methods in one go.

What is with Endwith statement?

By using With... End With , you can perform a series of statements on a specified object without specifying the name of the object multiple times. Within a With statement block, you can specify a member of the object starting with a period, as if the With statement object preceded it.

Can you nest with statements in VBA?

You can nest With statements by placing one With block within another.

What does end with do in a macro?

The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. The picture above shows a macro that changes a few properties of the Range Object.


1 Answers

There is a shorter and more readable version of your solution:

For Each lbl As Label In {lblA, lblB, lblC}
  With lbl
    '...
  End With
Next
like image 119
Neolisk Avatar answered Nov 08 '22 04:11

Neolisk