|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
DIM and SET
Hi Guys,
Everyone knows that you can create objects using the following 2 methods: Method 1: Code:
Dim ExampleObj As New ExampleClass Method 2: Code:
Dim ExampleObj As ExampleClass Set ExampleObj = New ExampleClass But what exactly is the difference?
__________________
LozWare Website Directory Whooo! Free submissions, no recip needed. I'm a nice guy
|
|
#2
|
||||
|
||||
|
I believe that in VB, no difference at all - it's only two ways to do the same thing.
|
|
#3
|
||||
|
||||
|
Quote:
There is no difference. But for a developer you may want to declare all your variables first, then only instantiate them when you need to use them. Hence, the reason you would do Method 2. |
|
#4
|
||||
|
||||
|
Cheers guys!
Also, I have found that with some classes you dont actaully need to instance them in order to use them, eg: Code:
Dim ExampleObj As ExampleClass Is it better to create a New Instance each time? I know that it will have something to do with the Threading. |
|
#5
|
||||
|
||||
|
probably something with "static memory" and "heap memory" but such
stuff is beyond me so I can't really tell. I find it hard to belive that it has to do with Threading though. what make you think that? |
|
#6
|
||||
|
||||
|
I presumed that when the Apartment threading model is selected, then each time a new instance is created, that instance is created on a new thread, and therefor any variables that it stores are isolated from any other instances of the same class.
I thought that it would work like this... (you gotta love these diagrams!)... ![]() ... but I might be wrong. At least you will understand what I'm getting at anyway! |
|
#7
|
||||
|
||||
|
this is way out of my league in VB, sorry. what make you think this
is true? on what information you base your assumptions? |
|
#8
|
||||
|
||||
|
It just makes sense... I'm not 100% sure, that’s why I asked you guys!
I'll have to find an advanced task-manager that shows you all of the different threads that are running, then I can see if a new thread is created each time you use the 'Dim Example as New ClassX'. I think that VB should be able to produce a Threading model diagram of your application once you have compiled it (you know, a bit like the Relationship diagrams that you get in Access... that would be sweet!). |
|
#9
|
||||
|
||||
|
Quote:
Something that gave me a little hunch was when I created 2 Classes without using the New command. The ActiveX Class had a Public String variable in it, and in my example application I did the following experiment... Code:
Dim Example1 as ClassX Dim Example2 as ClassX Example1.exString = "Hello!" msgbox Example2.exString And guess what... it said "Hello!". I thought that this was a pretty solid explanation on what the threading model looked like... so I drew that diagram. |
|
#10
|
||||
|
||||
|
nice, it's kind of "Shared Class" then. can't see how it's related to threading,
but it's good to know such thing exist. ![]() |
|
#11
|
||||
|
||||
|
... because variables can only be shared if they are on the same thread (unless you physically share them by using a SET command, and I think threads can also share variables if the varibles are held in a normal .bas module). If you specify 'New' when you Dim the class, then you cant do what I just did in the example app, therefore each class must be on a different thread (ie apartment threading). :-)
Last edited by LozWare : July 24th, 2006 at 07:30 AM. |
|
#12
|
|||
|
|||
|
Back to the original question, there is a difference if there is any other code between the DIM and the SET statements in method 2. In method 2 the object isn't created until your code reaches the SET statement. So with method 1,
dim obj as new whatever myvar = obj.myproperty will work, but with method 2 dim obj as whatever myvar = obj.myproperty 'The object isn't created yet. set obj = new whatever will fail.
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |