Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


How can I know if an object exists?


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-19-2008, 05:19 PM
Junior Member
 
Join Date: Jul 2007
Posts: 32
Question How can I know if an object exists?

Hi everyone,

I've got an error when I close a form and checks a method from an object. As it hasn't been created yet, I got an error (as expected). Not always the object is needed, so sometimes it doesnīt exists.

Is there a way to check if an object exists prior to use it? (similar to FileExist function).

Thanks in advance,

Jesus.
Reply With Quote
  #2 (permalink)  
Old 05-19-2008, 05:34 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,770
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

I assume that you mean an object from a library that has not yet had its' New method invoked. I can think of an arcane method that might work by assigning a B4PObject to the ControlRef of a HashTable from my Collections library but it is probably easier if you just New any existing objects in App_Start.
Reply With Quote
  #3 (permalink)  
Old 05-19-2008, 05:37 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

You can use something like:
Code:
Sub ObjectExist(obj)
 ErrorLabel(ObjectExistErr)
  dim a
  a = ControlType(obj)
  return true
ObjectExistErr:
 return false
End Sub
Reply With Quote
  #4 (permalink)  
Old 05-19-2008, 05:43 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,770
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

I've been silly, Erel's is a good idea - I always look for the complicated way of doing things. It should also be returned in the control collection from GetControls("")
Reply With Quote
  #5 (permalink)  
Old 05-19-2008, 06:03 PM
Junior Member
 
Join Date: Jul 2007
Posts: 32
Unhappy Objet exists

Hi Erel and Agraham,

to Agraham: Yes, it was my first approach, but as the object not always exists, is not always needed to 'New' it.

to Erel: Tried your code, but I canīt parse the objet to Objetexist function.

I'll try to explain myself better. It's a Serial objet, and I got the 'undeclared array' error using Erel's code.

The error I got is in the following sentence:

Sub Form_Close
If Serial1.PortOpen=true Then Serial1.PortOpen = false
End Sub


If Serial1 hasn't been created with the New method, I can't use it (obviously). What I was looking for is something like this:

Sub Form_Close
If ObjetExist(Serial1.) Then
If Serial1.PortOpen=true Then Serial1.PortOpen = false
End if
End Sub


or

If Serial1.Exists then...

but this method doesnīt exists, isnīt it?

Thank you.
Reply With Quote
  #6 (permalink)  
Old 05-19-2008, 06:16 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

You should write:
Code:
If ObjectExist("Serial1") Then ...
Also note that I fixed a small mistake in my previous example (obj instead of crl).
Reply With Quote
  #7 (permalink)  
Old 05-19-2008, 06:18 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,770
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Erel's method does work but the result differs between the IDE and the optimised compiler for a pre-defined object. In the IDE the control exists even if it has not been Newed but when optimised it exists only after being Newed so this following code shows a "Yes" message box and Object appears in the list in the IDE, but optimised compiled there is no Yes message box and Object is absent from the control list.
Code:
Sub Globals
	'Declare the global variables here.
	Dim Ctls (0) 
End Sub

Sub App_Start
	'Object.New1
	If ObjectExist("Object") Then
		Msgbox("Yes")
	End If
	Ctls() = GetControls("")
	For i = 0 To ArrayLen(Ctls())-1
		msg = msg & Ctls(i) & crlf
	Next
	Msgbox(msg)
End Sub

Sub ObjectExist(obj)
 ErrorLabel(ObjectExistErr)
  Dim a
  a = ControlType(obj)
  Return true
ObjectExistErr:
 Return false
End Sub
EDIT: Note that "Object.New1" is commented out.

Last edited by agraham : 05-19-2008 at 06:23 PM.
Reply With Quote
  #8 (permalink)  
Old 05-20-2008, 11:57 AM
Junior Member
 
Join Date: Jul 2007
Posts: 32
Thumbs up Working.

With the new code, it's working fine now.

Once again, thank you both, gentlemans.
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking if registry key exists? N1c0_ds Questions & Help Needed 3 10-21-2008 06:30 AM
SQL Table Exists? tsteward Questions & Help Needed 3 05-02-2008 01:18 PM
Object Reference not set to an instance of an object monster9999 Questions & Help Needed 9 01-15-2008 10:56 PM
object reference dennishea Questions & Help Needed 6 08-15-2007 05:48 PM
Tab Object anansath Basic4ppc Wishlist 1 06-08-2007 03:58 PM


All times are GMT. The time now is 02:47 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0