a With block is a powerful thing. Here's an example...
With LoginForm.ButtonX
.Top = 10
.Left = 17
.Text = "OK"
.ForeColor = BUTTON_TEXT_COLOR
.Background = BUTTON_FACE_COLOR
End With
The With block serves a few useful purposes. It makes it easy to cut and paste code and use it elsewhere (perhaps for a different Button). And all you have to change is the ButtonX at the top to be ButtonZ instead, and you have updated the whole block to work with ButtonZ.
It is also nice because it allows the reader to focus on what is being done, instead of focusing on the full names of the object(s) being worked with
the following code is not hard to understand, but you do have to be careful and make sure that all the repetitive stuff really is the same...
As nice as the with block already is, it needs some enhancements.
There are times when you want to work with the object itself and not just one of its properties. Suppose you want to send it as a parameter to some function... all of a sudden you have to use the fully qualified name again.
I suggest two ways to fix this (1) allow . to be used in place of the variable. if the . is alone, then it will be used as the object being with'ed. And (2) allow With blocks to create aliases... like so.
With LoginForm.ButtonX as Btn
.Top = 10
.Left = 17
.Text = "OK"
Btn.ForeColor = BUTTON_TEXT_COLOR
Btn.Background = BUTTON_FACE_COLOR
DoSomethingWith(Btn)
DoSomethingWith(.)
End With
notice the "Btn.ForeColor = ..." statement you would be able to use an alias if you wanted, or just continue using the dot notation , and notice the two possible ways to call DoSomethingWith()
-----
Also, we need one more enhancement. When dealing with Nested With's we need some more power.
With DatabaseX
[...some code...]
With .RecordSetZ
[...some code...]
With .FieldQ
.Value = "Hi There"
End With
End With
End With
What if we want to do something with DatabaseX or RecordSetZ while we are inside of the FieldQ With block?
I guess we could have used aliases for them, like "db" and "rs" then we could talk about them inside of the inner block, but what if we also let people use . (one dot) to speak about the inner most block, .. (two dots) to refer to the second most inner with block, ... (three dots) for the next block out, and so on.
-------
And now, a restriction.
You cannot refer to the object of a with block from inside the with block in any way other than through the (.) or the alias. This rule prevents potential errors down the road when someone modifies the object of the with block, and expects every reference to that object to be correctly updated without having to verify every single line.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
a With block is a powerful thing. Here's an example...
With LoginForm.ButtonX
.Top = 10
.Left = 17
.Text = "OK"
.ForeColor = BUTTON_TEXT_COLOR
.Background = BUTTON_FACE_COLOR
End With
The With block serves a few useful purposes. It makes it easy to cut and paste code and use it elsewhere (perhaps for a different Button). And all you have to change is the ButtonX at the top to be ButtonZ instead, and you have updated the whole block to work with ButtonZ.
It is also nice because it allows the reader to focus on what is being done, instead of focusing on the full names of the object(s) being worked with
the following code is not hard to understand, but you do have to be careful and make sure that all the repetitive stuff really is the same...
LoginForm.ButtonX.Top = 10
LoginForm.ButtonX.Left = 17
LoginForm.ButtonX.Text = "OK"
LoginForm.ButtonX.ForeColor = BUTTON_TEXT_COLOR
LoginForm.ButtonX.Background = BUTTON_FACE_COLOR
-----
As nice as the with block already is, it needs some enhancements.
There are times when you want to work with the object itself and not just one of its properties. Suppose you want to send it as a parameter to some function... all of a sudden you have to use the fully qualified name again.
I suggest two ways to fix this (1) allow . to be used in place of the variable. if the . is alone, then it will be used as the object being with'ed. And (2) allow With blocks to create aliases... like so.
With LoginForm.ButtonX as Btn
.Top = 10
.Left = 17
.Text = "OK"
Btn.ForeColor = BUTTON_TEXT_COLOR
Btn.Background = BUTTON_FACE_COLOR
DoSomethingWith(Btn)
DoSomethingWith(.)
End With
notice the "Btn.ForeColor = ..." statement you would be able to use an alias if you wanted, or just continue using the dot notation , and notice the two possible ways to call DoSomethingWith()
-----
Also, we need one more enhancement. When dealing with Nested With's we need some more power.
With DatabaseX
[...some code...]
With .RecordSetZ
[...some code...]
With .FieldQ
.Value = "Hi There"
End With
End With
End With
What if we want to do something with DatabaseX or RecordSetZ while we are inside of the FieldQ With block?
I guess we could have used aliases for them, like "db" and "rs" then we could talk about them inside of the inner block, but what if we also let people use . (one dot) to speak about the inner most block, .. (two dots) to refer to the second most inner with block, ... (three dots) for the next block out, and so on.
-------
And now, a restriction.
You cannot refer to the object of a with block from inside the with block in any way other than through the (.) or the alias. This rule prevents potential errors down the road when someone modifies the object of the with block, and expects every reference to that object to be correctly updated without having to verify every single line.