Re: What am I doing wrong?
- From: "Larry Serflaten" <serflaten@xxxxxxxxxxxxxx>
- Date: Sun, 11 Jun 2006 14:20:06 -0500
"AK" <nospam@xxxxxxxxxxx> wrote
My pea brain is thus thinking that object itself must have a "handle" ....
which is NOT the Name Property the object. While the name of the object is
itself an object (along the lines like this thead is a discussion object) it
is NOT the object itself (french frys mentioned earlier). Heck, maybe that
"handle" as I call it, is indeed the Name of the object, which, howevver,
is different than the Name Property.
Not quite, as Rick states, the Name is a convenience for the programer,
the OS is going to use a window's handle to differentiate between objects.
Some of those objects may in fact have the same Name, but their handle
will be unique. For example, in a new project, add this to Form1:
Private Sub Form_Load()
Caption = Me.Name & " - " & CStr(Me.hWnd)
Me.Move Forms.Count * 1000, Forms.Count * 1000
If Forms.Count < 4 Then
With New Form1
.Show
End With
End If
End Sub
When you run the program you'll see 4 forms showing, all with the
name of "Form1", but each with a unique windows handle. (Of
course the computer reduces everything down to a numeric value
to store anything in memory, so the handle is actually a number)
Windowed controls also have a handle (the hWnd property). While
you can pass that handle value around to various routines, it will require
a little API trickery to put it to use.
So, in a nutshell, you can't (easily) use a value to represent an object.
You can (more easily) use a reference to some object instead.
Anytime you are assigning objects to variables, expect to use Set!
With Text1 added to a new form, try this code in the form:
Private Sub Form_Load()
Dim ref1 As TextBox
Dim ref2 As Control
Dim ref3 As Object
Dim ref4 As Variant
Set ref1 = Text1
Set ref2 = Text1
Set ref3 = Text1
Set ref4 = Text1
ref1.Text = "Some text string"
ref2.Move 90, 90
ref3.BackColor = vbBlack
ref4.ForeColor = vbWhite
End Sub
What that shows is that you can have any number of references
all pointing to the same object, and there are a few different VB
data types that accept being assigned an object. I listed the
Dim statements in order from the specific to the more general
form of reference. Only ref1 will have Intellisense, the others
do not know what type of object they might be assigned and
therefore cannot know what properties and methods to show
for the dropdown list in the IDE.
You don't have to specifically create a variable reference to
pass objects around, you can pass the objects themselves,
where the Sub or Function will use a reference as one of it
parameters:
Private Sub SelectAllText(TextBox As TextBox)
With TextBox
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
That can be called passing in any textbox object:
SelectAllText Text1
SelectAllText Text2
' ... etc...
The point here is that when you want to work with objects in
a generic fashion (such as the above sub) work with them as
objects, and not as values (either the Name or hWnd or other
such value.) Also keep it clear in your mind that objects are
not values, objects have properties that are set to some value
or another. When you have a variable that contains data,
such as a string or number, then it is a 'variable that contains
a value'. When you have variables that refer to objects, then
you have a 'variable that is a reference to an object'.
The main difference is that when you erase a value (or assign
a new value), the old value is gone. When you erase a
reference (by setting the variable to Nothing, or assigning
a new object) the actual object will only be destroyed if that
is the last remaining reference to that object. In other words,
while ref1 can be set to Nothing in the above example, the
actual textbox will not be destroyed, there are other references
to that object remaining; such as the other refX variables plus
one in the Controls collection, and the generic one used by VB.
(The one called 'Text1' or other such name you give it.)
And just to reiterate, because it is important:
Anytime you are assigning objects to variables, expect to use Set!
A = B ' is a value assignment
Set A = B ' is a reference assignment
HTH
LFS
.
- References:
- What am I doing wrong?
- From: AK
- Re: What am I doing wrong?
- From: Larry Serflaten
- Re: What am I doing wrong?
- From: AK
- Re: What am I doing wrong?
- From: Larry Serflaten
- Re: What am I doing wrong?
- From: AK
- Re: What am I doing wrong?
- From: Bob O`Bob
- Re: What am I doing wrong?
- From: Rick Rothstein
- Re: What am I doing wrong?
- From: AK
- What am I doing wrong?
- Prev by Date: Re: Can someone help me?
- Next by Date: Re: Advanced Visual Basic 6 CD
- Previous by thread: Re: What am I doing wrong?
- Next by thread: Re: What am I doing wrong?
- Index(es):
Relevant Pages
|