The Set statement in VB 
Author Message
 The Set statement in VB

Hi
Wanted to know why one needs to use the Set statement in
VB to assign a reference to an object to a variable? To be
precise I am looking for what is that the Set statement
does that the simple assignment operator doesn't, which
means

Why do:
Set myVar = New Somecomponent

and not simply:
myVar = New Somecomponent?

Hope to see some quick responses to this.

Thanks



Sun, 09 May 2004 21:05:02 GMT  
 The Set statement in VB

Quote:
> Why do:
> Set myVar = New Somecomponent

> and not simply:
> myVar = New Somecomponent?

Because that doesn't work, that's why :)

Objects differ in as much as when you

set obj = New MyObject

then obj does not "contain" your object, it simply points to it (or
references it to use the correct terminology).  I can then do

set obj2 = obj

Now both obj and obj2 point to the same object.  When you do a variable
assignment

i = 5

The i actually contains the value 5.  If I do

str = "Hello World"

then str uses 11 byes of memory (I know that is simplified, don't reply and
say it's wrong)

However both obj and obj2 are the same size, just 2 byes.  Those 2 bytes are
the memory reference to the actual object which might be 1Gb in size.

Set is used to differentiate between making a variable contain a value, and
making it point to something else.



Sun, 09 May 2004 21:38:37 GMT  
 The Set statement in VB

Quote:
> ... both [pointers to objects] obj and obj2 are the same
> size, just 2 byes. Those 2 bytes are the memory reference
> to the actual object which might be 1Gb in size.

Maybe 4 bytes each would do better! <g>


Sun, 09 May 2004 22:44:39 GMT  
 The Set statement in VB
Hello:  Regarding the following line...It's true but...

Quote:
> Set is used to differentiate between making a variable contain a value,
and
> making it point to something else.

BUT:  To delve a little deeper...
...If the object does not exist and you use "SET objX = NEW objX" SET is
actually creating the object in memory and returning an object reference to
the newly created object...So The first time you use SET on an object
variable it does more than simply assigning an object reference...Behind the
scenes, the vb runtime actually creates the object in memory...When you use
"SET objX2 = objX" you are 100% correct...objX2 holds an object reference to
the same object as objX and vb does not recreate the object in memory...So
depending on how you use set...The vb runtime may actually create an object
and return a reference to it or simply return a reference to an existing
object...

Shannon Richards
BBA, AIT, MCP

Quote:
> Objects differ in as much as when you

> set obj = New MyObject

> then obj does not "contain" your object, it simply points to it (or
> references it to use the correct terminology).  I can then do

> set obj2 = obj

> Now both obj and obj2 point to the same object

> Set is used to differentiate between making a variable contain a value,
and
> making it point to something else.



Mon, 10 May 2004 00:46:01 GMT  
 The Set statement in VB
There are some fundamentals that have been left out here.

Shannon's post states that Set used with New creates a new object. This is
not actually true. New creates a new object, whether it's used with Set or
not. The following, for example, are perfectly valid statements:

    With New ADODB.Connection
    Call SomeMethodThatTakesARecordset(New ADODB.Recordset)

This is also true of CreateObject.

The Set statement is more complex than the Let statement (which is optional
but implied when you do something like myVar = 5) by far. The Set statement
is designed not only to assign a reference around, but also is designed to
wrap IUnknown's QueryInterface call. If you don't know anything about COM,
know these few things:

- IUnknown is the fundamental building block of COM. Every COM object
implements this interface. It's how a client (user of the component)
interacts with the server (component) to maintain references and gain access
to various other functionality (exposed via interfaces) of the component.
- QueryInterface is the call that allows a client to ask the component if it
supports a given interface.

That being said, consider the following:

- ComponentA implements a number of interfaces, including IInterface1 and
IInterface2.
- You have the following code:

    Dim objObject As Object
    Dim objInterface1 As IInterface1
    Dim objInterface2 As IInterface2

    Set objObject = New ComponentA
    Set objInterface1 = objObject
    Set objInterface2 = objInterface2

In this case, the Set is charged with doing considerably more than simple
assigning pointers around. Since each of the variables are declared as
different types, the Set calls QueryInterface on that object for different
interfaces:

    Set objObject = New ComponentA ' Calls QueryInterface for interface
IDispatch, the fundamental building block of ActiveX
    Set objInterface1 = objObject ' Calls QueryInterface for interface
IInterface1
    Set objInterface2 = objInterface2 ' Calls QueryInterface for interface
IInterface2

Since every COM component implements IUnknown, you can rest assured that Set
will be able to call QueryInterface on the object, no matter what the
variable you're assigning *from* is declared as. That is, the third Set
above can be either:

    Set objInterface2 = objInterface2
    - or -
    Set objInterface2 = objObject

As for whether the component you're assigning from actually implements the
given interface, that's a different story. Consider the following:

    Dim adoConn As ADODB.Connection
    Set adoConn = New ADODB.Recordset

In this case, since your variable is declared as type ADODB.Connection, it
will attempt to query for the appropriate interface. When it does (at
runtime) it will receive an error (E_NOTIMPL) and will throw an error
stating "Type mismatch" or "Interface not supported." Unfortunately, no
compiler can make any assumptions about which components support which
interfaces. Therefore, the above code will compile without any problems.

I hope this helps.
--
Michael "The" Gnter, MCSD, MCP (VC++, VB)
Senior Software Engineer
Infinity Software Development, Inc.
Phone: (850) 383-1011 . Fax: (850) 383-1015


Quote:
> Hi
> Wanted to know why one needs to use the Set statement in
> VB to assign a reference to an object to a variable? To be
> precise I am looking for what is that the Set statement
> does that the simple assignment operator doesn't, which
> means

> Why do:
> Set myVar = New Somecomponent

> and not simply:
> myVar = New Somecomponent?

> Hope to see some quick responses to this.

> Thanks



Mon, 10 May 2004 02:04:30 GMT  
 The Set statement in VB
Plus without the set statement default properties would be no more (like
in .NET)

For instance

set control = text1

without the set you wouldn't know if the assignment referred to the
.text property (which is the default) or the control (object) itself.

Quote:

> ------------------------------------------------------------------------

> Subject:

> Re: The Set statement in VB
> From:


> Date:

> Wed, 21 Nov 2001 11:46:01 -0500

> Newsgroups:

> microsoft.public.vb.com

> Hello:  Regarding the following line...It's true but...

>>Set is used to differentiate between making a variable contain a value,

> and

>>making it point to something else.

> BUT:  To delve a little deeper...
> ...If the object does not exist and you use "SET objX = NEW objX" SET is
> actually creating the object in memory and returning an object reference to
> the newly created object...So The first time you use SET on an object
> variable it does more than simply assigning an object reference...Behind the
> scenes, the vb runtime actually creates the object in memory...When you use
> "SET objX2 = objX" you are 100% correct...objX2 holds an object reference to
> the same object as objX and vb does not recreate the object in memory...So
> depending on how you use set...The vb runtime may actually create an object
> and return a reference to it or simply return a reference to an existing
> object...

> Shannon Richards
> BBA, AIT, MCP

>>Objects differ in as much as when you

>>set obj = New MyObject

>>then obj does not "contain" your object, it simply points to it (or
>>references it to use the correct terminology).  I can then do

>>set obj2 = obj

>>Now both obj and obj2 point to the same object

>>Set is used to differentiate between making a variable contain a value,

> and

>>making it point to something else.



Mon, 10 May 2004 02:08:31 GMT  
 The Set statement in VB
Sorry ...These are VERY GOOD points...I did not want to get into the
symantics of COM and what the vb runtime does behind the scenes to make
components created in VB 100% COM compliant.  Every vb COM compliant object
derives from IUnknown and IDispatch for that matter (Dual interface) but vb
masks the complexity of this so vb programmers rarely if ever have to deal
with IUnknown directly...IUnknown only has 3 methods...QueryInterface,
AddRef and Release...

I should have mentioned that SET is more complicated than that but I didn't
want to get too deep into what vb does behind the scenes...
When you code:
DIM Dog as IDog
SET Dog = NEW CDog

'To get at another interface...
DIM Dog2 as IDog2
SET Dog2 = Dog --Query Interface gets called (If CDog does not also
implement IDog2 there will be a type mismatch error)

*A great book I used to get the COM basics for VB is: "Programming
Distributed Applications with COM+ and VB6" by Ted Patterson...

Michael: You are definently correct...Set is more complicated than what I
had originally stated...However some vb programmers don't really care about
how sweet the vb runtime layer is for shielding us from whats going on
behind the scenes...

Is there any performance difference between using SET Con = NEW Connection
vs the example you used below "With NEW ADODB.Connection"??

Thanx,
Shannon Richards
BBA, AIT, MCP



Quote:
> There are some fundamentals that have been left out here.

> Shannon's post states that Set used with New creates a new object. This is
> not actually true. New creates a new object, whether it's used with Set or
> not. The following, for example, are perfectly valid statements:

>     With New ADODB.Connection
>     Call SomeMethodThatTakesARecordset(New ADODB.Recordset)

> This is also true of CreateObject.

> The Set statement is more complex than the Let statement (which is
optional
> but implied when you do something like myVar = 5) by far. The Set
statement
> is designed not only to assign a reference around, but also is designed to
> wrap IUnknown's QueryInterface call. If you don't know anything about COM,
> know these few things:

> - IUnknown is the fundamental building block of COM. Every COM objectorry
> implements this interface. It's how a client (user of the component)
> interacts with the server (component) to maintain references and gain
access
> to various other functionality (exposed via interfaces) of the component.
> - QueryInterface is the call that allows a client to ask the component if
it
> supports a given interface.

> That being said, consider the following:

> - ComponentA implements a number of interfaces, including IInterface1 and
> IInterface2.
> - You have the following code:

>     Dim objObject As Object
>     Dim objInterface1 As IInterface1
>     Dim objInterface2 As IInterface2

>     Set objObject = New ComponentA
>     Set objInterface1 = objObject
>     Set objInterface2 = objInterface2

> In this case, the Set is charged with doing considerably more than simple
> assigning pointers around. Since each of the variables are declared as
> different types, the Set calls QueryInterface on that object for different
> interfaces:

>     Set objObject = New ComponentA ' Calls QueryInterface for interface
> IDispatch, the fundamental building block of ActiveX
>     Set objInterface1 = objObject ' Calls QueryInterface for interface
> IInterface1
>     Set objInterface2 = objInterface2 ' Calls QueryInterface for interface
> IInterface2

> Since every COM component implements IUnknown, you can rest assured that
Set
> will be able to call QueryInterface on the object, no matter what the
> variable you're assigning *from* is declared as. That is, the third Set
> above can be either:

>     Set objInterface2 = objInterface2
>     - or -
>     Set objInterface2 = objObject

> As for whether the component you're assigning from actually implements the
> given interface, that's a different story. Consider the following:

>     Dim adoConn As ADODB.Connection
>     Set adoConn = New ADODB.Recordset

> In this case, since your variable is declared as type ADODB.Connection, it
> will attempt to query for the appropriate interface. When it does (at
> runtime) it will receive an error (E_NOTIMPL) and will throw an error
> stating "Type mismatch" or "Interface not supported." Unfortunately, no
> compiler can make any assumptions about which components support which
> interfaces. Therefore, the above code will compile without any problems.

> I hope this helps.
> --
> Michael "The" Gnter, MCSD, MCP (VC++, VB)
> Senior Software Engineer
> Infinity Software Development, Inc.
> Phone: (850) 383-1011 . Fax: (850) 383-1015



> > Hi
> > Wanted to know why one needs to use the Set statement in
> > VB to assign a reference to an object to a variable? To be
> > precise I am looking for what is that the Set statement
> > does that the simple assignment operator doesn't, which
> > means

> > Why do:
> > Set myVar = New Somecomponent

> > and not simply:
> > myVar = New Somecomponent?

> > Hope to see some quick responses to this.

> > Thanks



Mon, 10 May 2004 03:44:35 GMT  
 The Set statement in VB
*rolls eyes*  I guess someone had to do it.  If the OP wants to know what
"set" is for, do you think they want to know about IUnknown and getting
handles to custom interfaces etc?



Quote:
> There are some fundamentals that have been left out here.

> Shannon's post states that Set used with New creates a new object. This is
> not actually true. New creates a new object, whether it's used with Set or
> not. The following, for example, are perfectly valid statements:

>     With New ADODB.Connection
>     Call SomeMethodThatTakesARecordset(New ADODB.Recordset)

> This is also true of CreateObject.

<snip irrelevant pick-me-miss-i'm-ever-so-smart info>


Mon, 10 May 2004 16:33:09 GMT  
 The Set statement in VB
Hi All
I sincerely appreciate the amount of response my original
post generated. I have been writing COM components till
now using VC++ and am definitely familiar with the{*filter*}
details of IUnknown, IDispatch and the endless other
interfaces that Microsoft has provided.

Since I rarely use VB (though I am pretty comfortable at
it) never had the drive of going through a book dedicated
to VB and COM. The replies posted have definitely given me
a good amount of understanding of what the VB Runtime does
in context of creating an instance of a COM object.

Thanks and regards
Manish

Quote:
>-----Original Message-----
>There are some fundamentals that have been left out here.

>Shannon's post states that Set used with New creates a
new object. This is
>not actually true. New creates a new object, whether it's
used with Set or
>not. The following, for example, are perfectly valid
statements:

>    With New ADODB.Connection
>    Call SomeMethodThatTakesARecordset(New
ADODB.Recordset)

>This is also true of CreateObject.

>The Set statement is more complex than the Let statement
(which is optional
>but implied when you do something like myVar = 5) by far.
The Set statement
>is designed not only to assign a reference around, but
also is designed to
>wrap IUnknown's QueryInterface call. If you don't know
anything about COM,
>know these few things:

>- IUnknown is the fundamental building block of COM.
Every COM object
>implements this interface. It's how a client (user of the
component)
>interacts with the server (component) to maintain

references and gain access

- Show quoted text -

Quote:
>to various other functionality (exposed via interfaces)
of the component.
>- QueryInterface is the call that allows a client to ask
the component if it
>supports a given interface.

>That being said, consider the following:

>- ComponentA implements a number of interfaces, including
IInterface1 and
>IInterface2.
>- You have the following code:

>    Dim objObject As Object
>    Dim objInterface1 As IInterface1
>    Dim objInterface2 As IInterface2

>    Set objObject = New ComponentA
>    Set objInterface1 = objObject
>    Set objInterface2 = objInterface2

>In this case, the Set is charged with doing considerably
more than simple
>assigning pointers around. Since each of the variables
are declared as
>different types, the Set calls QueryInterface on that

object for different
Quote:
>interfaces:

>    Set objObject = New ComponentA ' Calls QueryInterface
for interface
>IDispatch, the fundamental building block of ActiveX
>    Set objInterface1 = objObject ' Calls QueryInterface
for interface
>IInterface1
>    Set objInterface2 = objInterface2 ' Calls

QueryInterface for interface
Quote:
>IInterface2

>Since every COM component implements IUnknown, you can

rest assured that Set
Quote:
>will be able to call QueryInterface on the object, no
matter what the
>variable you're assigning *from* is declared as. That is,
the third Set
>above can be either:

>    Set objInterface2 = objInterface2
>    - or -
>    Set objInterface2 = objObject

>As for whether the component you're assigning from

actually implements the
Quote:
>given interface, that's a different story. Consider the
following:

>    Dim adoConn As ADODB.Connection
>    Set adoConn = New ADODB.Recordset

>In this case, since your variable is declared as type

ADODB.Connection, it
Quote:
>will attempt to query for the appropriate interface. When
it does (at
>runtime) it will receive an error (E_NOTIMPL) and will
throw an error
>stating "Type mismatch" or "Interface not supported."
Unfortunately, no
>compiler can make any assumptions about which components
support which
>interfaces. Therefore, the above code will compile

without any problems.

- Show quoted text -

Quote:

>I hope this helps.
>--
>Michael "The" Gnter, MCSD, MCP (VC++, VB)
>Senior Software Engineer
>Infinity Software Development, Inc.
>Phone: (850) 383-1011 . Fax: (850) 383-1015



>> Hi
>> Wanted to know why one needs to use the Set statement in
>> VB to assign a reference to an object to a variable? To
be
>> precise I am looking for what is that the Set statement
>> does that the simple assignment operator doesn't, which
>> means

>> Why do:
>> Set myVar = New Somecomponent

>> and not simply:
>> myVar = New Somecomponent?

>> Hope to see some quick responses to this.

>> Thanks

>.



Mon, 10 May 2004 17:25:37 GMT  
 The Set statement in VB

Dim a As Variant
Set a = fld  'compiles pointer to fld
or
a = fld 'compiles as a = fld.value

the compiler does NOT know whether the programmers wants the actual value or
he wants a pointer????

In VB 7 no default methods exist anymore.

--
Egbert Nierop



Tue, 11 May 2004 03:02:48 GMT  
 The Set statement in VB
Stepping in a little because I don't really see what I had in mind in the
replies.  VB<=6 needs the set method because VB<=6 supports "default"
properties.  Default properties are properties that you don't need to write
down in your code.
Examples :
You write :
myCollection(1) instead of myCollection.Item(1)
You write
thisValue = myRecordset("myField") instead of
myRecordSet.Fields.Item("myField").Value

Now if you have a class myClass with a default property myDefaultProperty
and there was no set statement, what would be the result of

thisValue = myClass

VB would not know if you mean myClass or mtClass.myDefaultProperty (although
you might think that you didn't mention myDefaultProperty.

So when you say
set thisValue = myClass then VB knows you adding a reference to an object.

At least, this is what I have always heard.
--
Van den Driessche Willy
For a work in progress :
http://users.skynet.be/wvdd2/index.html



Wed, 12 May 2004 06:18:44 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. using a set statement to set a variable to another db

2. set variable with an sql statement in VBA

3. How to set txt box to SQL statement

4. '97 Set text box value to SQL Select statement

5. Set next statement problem

6. Set statement fails

7. Setting AllowZeroLength property in an SQL statement and rdo

8. object set statement - HELP

9. SET statement

10. Equivalent to SET Statement

11. Set statement

12. Use a variable name in a set statement

 

 
Powered by phpBB® Forum Software