Please register or login. There are 4 registered and 1153 anonymous users currently online. Current bandwidth usage: 100.91 kbit/s July 08 - 11:43pm EDT 
Hardware Analysis
      
Forums Product Prices
  Contents 
 
 

  Latest Topics 
 

More >>
 

    
 
 

  You Are Here: 
 
/ Forums / Programming /
 

  Visual Basic: How do I check whether an Object exists? 
 
 Author 
 Date Written 
 Tools 
Sandy Oct 19, 2005, 10:00am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List

Edited: Oct 19, 2005, 10:02am EDT

Replies: 20 - Views: 2659
The Problem
How can I check whether an Object exists in Visual Basic

I've tried testing whether it equals Nothing, and that doesn't work.

More Info:

I'm making a little script to open and control an ActiveX control within a CAD program, and I'm getting this slightly cryptic error:

Source:'Microsoft VBScript runtime error'
Line 58 Char 1
Error:0x800a01a8 'Object Required: 'SymbolBlock''

And I think the reason is because the object I'm trying to read from hasn't been created at that point. I'd like to check whether the object exists before trying to read the value.

This is the script:

Sub Application_SymbolPreviewed(SymbolBlock) ' An event in this CAD program
scene = ""
For Each A in SymbolBlock.Attributes ' Line 58
If A.Name = "3D" Then
scene = A.Value
End If
Next

Addins(AddinTitle).Control.Scene = scene ' Set an attribute in the activeX control
End Sub

What it actually does: The program is an electronics CAD program, and I'm trying to add a viewer to it so you can see what a component (like a switch or connector) looks like, so you get the right one. The event fires when you select a component in a dialog box before you put it on the sheet, and gives you a reference to the file describing that component. The script then looks up some metadata in the file to find an url to an image file and this is passed to an activeX control which displays the file

The Script actually does what it's supposed to, but you always get this error just after you open the Dialog Box, and at no other time

If I put in a clause to test whether SymbolBlock = Nothing, I get a similar error that says: "Object variable not set"

Any Ideas?

Help would be very much appreciated


Want to enjoy less advertisements and more features? Click here to become a Hardware Analysis registered user.
Ex Member Oct 19, 2005, 10:15am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
Try :

If Not SymbolBlock Is Nothing Then

instead of

If Not SymbolBlock = Nothing Then

Sandy Oct 19, 2005, 11:07am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
Thanks very much, it works now.

Ex Member Oct 19, 2005, 11:23am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
No problem

guru Shane Oct 19, 2005, 11:50am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
I would have to ask why it is being called, if it doesn't exist yet. You might want to rethink your flow or change the scope of your variable.

Ex Member Oct 19, 2005, 12:56pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
It is common in object oriented languages to conditionally call object methods as object states can change often throughout the lifetime of the application. Also where an object does not exist at a certain time, it may be necessary to create it. i.e. a database connection could be either idle or not yet instantiated. So we test.

guru Shane Oct 19, 2005, 02:09pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
Well, if you are within the same scope as the variable itself, it will exist. You can test to see if it contains what it is supposed to contain, you can set up an If statement or a select statement that will test to see if it has been assigned to a value. While you are testing to see if it is equal to nothing, you could also test to see if it is equal to something. I'm not sure, (as I'm not nearly as good with VB as with C#), but your statement might have originally worked if you had use two equals signs. Whenever making a comparison, you use ==. If you only use 1 = sign, then it will try to assign the value on the right side of it to what's on the left side of it.

Ex Member Oct 19, 2005, 02:27pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
Wrong.

Just because code is executing in the same scope as an object variable, does not mean the object variable has been instantiated. Is Nothing is an object variable instantiation test, not an existence test.

Wrong.

This is VB. == is the operator for equality testing in C and Java syntax. "Is" is the correct operator to use in this situation.

The question was answered correctly in the first place. The program now works. Everybody is happy. If by your own admission you don't know VB, why are you so vocal in trying to educate those of us who have been using it professionally for over 7 years?

guru Shane Oct 19, 2005, 02:52pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
"You can test to see if it contains what it is supposed to contain, you can set up an If statement or a select statement that will test to see if it has been assigned to a value."

THIS IS 100% RIGHT! If you were such a professional that has been doing this for 7 years, you should have pointed that out as being right.

"Wrong.

Just because code is executing in the same scope as an object variable, does not mean the object variable has been instantiated. Is Nothing is an object variable instantiation test, not an existence test."

Thank you for clearing that up for the person that didn't understand... LMAO I totally understand, so you must be telling someone else.

"If by your own admission you don't know VB, why are you so vocal in trying to educate those of us who have been using it professionally for over 7 years?"

I didn't say that I didn't know VB. I said that I wasn't as good with it as I was with C#. There is a HUGE difference. Considering that I just composed an e-mail program with VB, I would tend to think that is FAR from not knowing VB...

Since the question was answered already, why do you feel the need to point out things that are wrong that AREN'T wrong, as well as things that are wrong THAT I ALREADY MENTIONED MIGHT BE??? I know many languages, forgive me for getting them confused at times. ;)

guru Shane Oct 19, 2005, 02:56pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
Oh, I almost forgot.

"Just because code is executing in the same scope as an object variable, does not mean the object variable has been instantiated. Is Nothing is an object variable instantiation test, not an existence test."

Considering that the title of this thread is, "Visual Basic: How do I check whether an Object exists?", then you should realize why I mentioned the scope. If there is a question as to whether the object EXISTS, that would primarily be a variable scope issue, would it not there Mr. Professional??? Hmmm?

Ex Member Oct 19, 2005, 03:12pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
If you read the first post properly you will see that what is really being asked is:

How do test that the object variable that I have declared is currently referencing an instantiated object?

I think it is pretty clear what was asked and that's why this thread was cleared up in 3 posts.

I criticised your post because I felt it uneccessarily confused what was actually a very straightforward question. Furthermore, I am not going to argue semantics any further as you seem to me to be the sort of person who won't give it up until you feel you have proved me wrong. Feel free to pick more holes in anything I have posted. I don't care as I will not be reading any further. Goodbye

guru Shane Oct 19, 2005, 03:18pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List

Edited: Oct 19, 2005, 03:23pm EDT

 
>> Re: Visual Basic: How do I check whether an Object exists?
"Also where an object does not exist at a certain time, it may be necessary to create it."

It looks as if it must have been unclear to you then. If you would have read my post PROPERLY, you wouldn't have been confused as to what I was trying to explain.

I think it's hilarious that I proved the professional that has been doing it for 7 years WRONG. LMAO
Good riddance. :P

guru Shane Oct 19, 2005, 03:21pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List

Edited: Oct 19, 2005, 03:24pm EDT

 
>> Re: Visual Basic: How do I check whether an Object exists?
This was written:

"If Not SymbolBlock Is Nothing Then"

You can substitute this:

"If SymbolBlock Is ________ Then"

All you would have to do is test for what has been assigned. Insert the value being tested for in place of the underline, (lol before someone flames me and tells me that the underline isn't proper code...)

Sandy Oct 19, 2005, 06:42pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
Chill, it works, and this is good.

I'm not really sure why the error was really generated in the first place, as the function concerned is an event handler, and should give you a reference to the block that was selected. I assumed that the reason for the error was a poorly written Object model (Mentor Graphics DxDesigner if you care) - with an event handler that doesn't check whether it has valid arguments whenever it's called. The question was only asking about a workaround, which i failed only to implement because of an embarassing lack of VB skill.

Incidentally Blue, i'm not sure the code you suggested would work, as it the object variable would have to be initalised for you to test equality to something other than nothing.

guru Shane Oct 19, 2005, 06:52pm EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List

Edited: Oct 19, 2005, 06:55pm EDT

 
>> Re: Visual Basic: How do I check whether an Object exists?
You're probably right. It might raise an exception. You could always use a Try/Catch with it and that would get you the same results, but it is faster and easier just to use the other version, unless, of course, you are already using a Try/Catch.

Can you create Custom exceptions with VB? The more limitations I see, the less I like Visual Basic.

lol and don't worry about any lack of VB skill. From the looks of its shortcomings, VB kind of sucks.

Sally Lorren Apr 10, 2008, 08:59am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
I think you are *both* acting like Kids (thats an insult to kids tbh). Flaming and USING CAPS UNNECESSARILY. You have tarnished this site for me. I wont be visiting again.

Beavis Khan Apr 10, 2008, 09:05am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
Sally Lorren said:
I think you are *both* acting like Kids (thats an insult to kids tbh). Flaming and USING CAPS UNNECESSARILY. You have tarnished this site for me. I wont be visiting again.


Oh no! Offended by a three year old post, instigated by a former member who is now on permanent vacation from the site! Seriously, find some newer posts to offend you if you want to go that route :)

____
"The American Republic will endure until the day Congress discovers that it can bribe the public with the public's money."

- Alexis de Tocqueville
Rhort Apr 10, 2008, 09:38am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> .
Big Beavis said:
Sally Lorren said:
I think you are *both* acting like Kids (thats an insult to kids tbh). Flaming and USING CAPS UNNECESSARILY. You have tarnished this site for me. I wont be visiting again.


Oh no! Offended by a three year old post, instigated by a former member who is now on permanent vacation from the site! Seriously, find some newer posts to offend you if you want to go that route :)


LOL That was her first post too! :D

Post Edited On: Oct 19, 2078, 00:27 AM
_________________________________________________________________________________
~ The manual said "Requires Windows '95 or better" ...so I installed Linux!
1001000 1100101 1111000 0100000 1010010 1110101
FordGT90Concept Apr 10, 2008, 09:44am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List

Edited: Apr 10, 2008, 09:48am EDT

 
>> Re: Visual Basic: How do I check whether an Object exists?
Aw, you got my rusty old VB gears turning for nothing. :( I hope they still work for when they are needed. *headache follows* XD Can you throw this old dog a bone? Me likes puzzles. :) One is also on drugs so don't mind me :_)

FordGT90Concept Apr 10, 2008, 11:09am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List

Edited: Apr 10, 2008, 11:25am EDT

 
>> Re: Visual Basic: How do I check whether an Object exists?
Not so drugged... When I read "SymbolBlock" I anticipated that it had something to do with symbolic linking in DLLs. How it is worded suggests that it is a variable that is/points to a class or struct of sorts. Basically, "If Not SymbolBlock Is Nothing Then" is checking if there is an instance associated with "SymbolBlock." It is simply checking the memory pointer to see if it points to a valid object or to nothing (aka "null").

I think the error produced is kind of awkward which lead to confusion. The error certainly isn't the typical "Object not set to reference blah blah" error message--it looks like a custom jobbie in an external application.

Additionally, "Is" is dedicated to checking for null values (type comparisons). "=" checks for equality which, in the way VB is, something can never equal nothing. "Is" works around that shortcoming in "=" ... other languages often handle things a little differently so a check for equality in one language might not garner the same results in another language. They all seem to have their perks.

So, um, yeah.


For your information, Sally, guruShane was banned from this site some time ago. No further comment on that subject from me. :X

Dr. Peaceful Apr 10, 2008, 11:19am EDT Reply - Quote - Report Abuse
Send Message - Add to Buddy List  
>> Re: Visual Basic: How do I check whether an Object exists?
WTF? Someone fell in a time worm hole?

____________________________________________
"May the people and the government respect the rights of all. Between individuals, as between nations, peace means respect for the rights of others." - Benito Juarez

Write a Reply >>


 

    
 
 

  Topic Tools 
 
RSS UpdatesRSS Updates
 

  Related Articles 
 
 

  Newsletter 
 
A weekly newsletter featuring an editorial and a roundup of the latest articles, news and other interesting topics.

Please enter your email address below and click Subscribe.