Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > need help with dim as form1


Reply
 
Thread Tools Display Modes
  #1  
Old 07-17-2002, 02:56 AM
gizmo83
Guest
 
Posts: n/a
Question need help with dim as form1


ok heres a sniplet of my code:




Sub RefreshPics(FormName As String)

If FormName = "form1" Then
Dim FormName2 As Form1
End If


Dim lFoundPos As Long

Dim lFindLength As Long

Dim MakeSure As Boolean

GoTo Skip
Start:
MakeSure = True
Skip:

lFoundPos = FormName2.txtout.Find(">", 0, , rtfNoHighlight)

End Sub





When i call this i get an error that reads:
Object Varible or With block varible not set

and when i debug it says its on the line that reads:
lFoundPos = FormName2.txtout.Find(">", 0, , rtfNoHighlight)




anyone know why??? what am i doing wrong? now on that line if i change FormName2 to Form1 then it works fine... but i check and FormName2 is set to Form1 (with dim FormName2 as Form1)
Reply With Quote
  #2  
Old 07-17-2002, 03:36 AM
gallicus gallicus is offline
Junior Contributor
 
Join Date: Dec 2001
Location: at work... probably :-(
Posts: 366
Default

Don't you have to use the 'Set' when you create an instance of a form?

Set Formname2 = new Form1
__________________
(A)bort, (R)etry, (G)et a beer?
Reply With Quote
  #3  
Old 07-17-2002, 03:40 AM
polarbear199 polarbear199 is offline
Centurion
 
Join Date: Dec 2001
Location: Oregon
Posts: 161
Default

I'm not the best to answer this one but I'll try. Do you have the properties set to Option Explicit? If you do then I think I see the problem.

The only way this line will work without error...
lFoundPos = FormName2.txtout.Find(">", 0, , rtfNoHighlight)

is if this little bit of code is found true.
If FormName = "form1" Then
Dim FormName2 As Form1
End If

My guess is that FormName not = "form1" and your FormName2 variable is never set.

I hope this is a help. Let me know will you?
Reply With Quote
  #4  
Old 07-17-2002, 11:31 AM
jayceepoo's Avatar
jayceepoo jayceepoo is offline
Senior Contributor
 
Join Date: Jul 2002
Posts: 1,021
Default

To solve your problem you have to instantiate FormName2 like this.

Code:
Dim FormName2  as New Form1
FormName2.Show
If you put the new key word in there it will actually create the form, otherwise, the memory is just being set aside for the form.

And by the way, if you use the code i mentioned before, you do not need to use the Set keyword.

If you want the use Set, you have to do it like this.
Code:
Dim FormName2 as Form1
Set FormName2 = new Form1
FormName2.Show
Hope I helped....
__________________
Jayceepoo

"I recently went to a new doctor and noticed he was located in something called the Professional Building. I felt better right away." - George Carlin

Last edited by jayceepoo; 07-17-2002 at 11:38 AM.
Reply With Quote
  #5  
Old 07-17-2002, 11:37 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default

jayceepoo - while that will work, it is better to use the method that gallicus suggested above, because there is less overhead involved. When you Dim an object variable as New, VB has to check if the object has been created each time it is used. However, if the Set keyword is used instead, this isn't need, speeding up the access.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #6  
Old 07-17-2002, 11:39 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

I hadn't heard that Chief. Why does it need to check each time?
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #7  
Old 07-17-2002, 11:43 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default

Since the New keyword is used in the Dim statement, it is assumed that no Set will be used. So COM has no way of knowing if the object has been instantiated yet, so it has to check first, by doing a QueryInterface, and if it fails - it creates the object.

When the New keyword isn't used with the Dim, no checking is done, because it is assumed that the object will be instantiated via Set.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #8  
Old 07-17-2002, 11:47 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

Surely it should be the other way around. If the New keyword is used as part of the Dim then the object should always be valid (unless you set it to Nothing). If it isn't then it would have check every time to check.
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #9  
Old 07-17-2002, 11:48 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default

But the object is only instantiated the first time it is used. Since COM doesn't know when this is, it has to check every time.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #10  
Old 07-17-2002, 11:55 AM
Volte's Avatar
Volte Volte is offline
Ultimate Contributor

Retired Leader
* Guru *
 
Join Date: Aug 2001
Posts: 5,343
Default

I believe that when using New within the Dim statement,
the instance of the object is not created until the first reference
of it, whereas when using Set, the object in instantiated
immediately.

Edit: Argh. I went off to MSDN to verify it, and Chief already posted.
Reply With Quote
  #11  
Old 07-17-2002, 11:58 AM
Volte's Avatar
Volte Volte is offline
Ultimate Contributor

Retired Leader
* Guru *
 
Join Date: Aug 2001
Posts: 5,343
Default

When an object is created, regardless of the method used, is it
not always in memory? I thought that the QueryInterface only happened
once, and that is when the object is initialized (Set statement or
otherwise).
Reply With Quote
  #12  
Old 07-17-2002, 11:58 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

Ah, now that's the info I was missing. I figured that it was created with the New keyword every time.
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #13  
Old 07-17-2002, 12:01 PM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default

Volte - you're right, once created it is in memory, but - there is no flag set anywhere to say that it has been created, so, COM has to check it via QueryInterface.

When using Set, it is assumed that the programmer will call Set before using the object, so no QueryInterface is needed (only once when it is created). With Dim as New, since it's all done automatically on the first time the object is used, but VB can't tell when the first instance is, it has to check each time if it's been created yet.

I'm not very good at this explaining malarky...
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #14  
Old 07-17-2002, 12:06 PM
Volte's Avatar
Volte Volte is offline
Ultimate Contributor

Retired Leader
* Guru *
 
Join Date: Aug 2001
Posts: 5,343
Default

I understand what you're saying, I just wasn't sure if we both
thought the same thing.
Reply With Quote
  #15  
Old 07-17-2002, 12:07 PM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

And so the "Object variable not set" error that VB throws is just a COM error that is nicely wrapped and handled for you. It is not VB keeping track of which object references are instaniated
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #16  
Old 07-17-2002, 12:12 PM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default

I would assume so, yes.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #17  
Old 07-17-2002, 01:04 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

I believe the overhead is actually in the code generated by the VB
compiler to check each time the object variable is used. I don't
believe it has anything specifically to do with COM. The way that
Chief described it is otherwise correct (to my understanding).
__________________
Posting Guidelines
Reply With Quote
  #18  
Old 07-17-2002, 01:47 PM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Post

I wasn't suggesting that the overhead was part of COM. I was saying that due to the VB implementation, the, when you get an Object variable not set error, it is just a COM error that VB is wrapping nicely for you rather than VB checking if the object is valid. If it was VB doing it then the overhead would be the same regardless of declaration method.
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #19  
Old 07-17-2002, 02:14 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Part of what I am saying is that VB isn't just wrapping some COM
error. The overhead comes from using the New keyword as part
of the object variable declaration. This isn't a COM thing. Let me
give you an example. Look at this code...
Code:
Dim cn As New ADODB.Connection
Private Sub mySub1()
  cn.ConnectionString = "Provider..."
End Sub
Private Sub mySub2()
  cn.ConnectionString = "Provider..."
End Sub
Private Sub mySub3()
  cn.ConnectionString = "Provider..."
End Sub
Since the word New was used in the declaration, code has to be
inserted in all three subs to check if cn has been instantiated, and
if not then automatically do it. It isn't just trying to use the object
and then trapping the error and then instantiating the object.
If New isn't in the declaration, VB will raise the Object variable not
set error, but I don't think it is just passing an error through from
COM, it is actually checking the object pointer to see if it points to
a valid object.
__________________
Posting Guidelines
Reply With Quote
  #20  
Old 07-17-2002, 02:32 PM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Post

No, no. I understand what you are saying. I was refering to the case where you don't use new in the dim statement. In that case VB does not check that the object has been instantiated, correct? So if you don't Set it then you receive an error if you try to use it. I was assuming that the error was not VB generated as that would require VB to check the object's validity, hence making this method as slow as the inline New.
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->