Jump to content

if vs.GetObject is None: drives me crazy


Recommended Posts

I just am coding sth to change PIO and Symbolnames.

Objectnames in VW-Files are supposed to be unique. So, if the Name already exists, the script is supposed to crash.

To prevent that, I write a condition-code to check, wether an object of that name already exists.

 

Problem: all my tests produce endless loops and I don't understand why.

For example: try that code in an empty file.

Insert any name. The loop DOESN'T end.

 

Every object I try to address with "vs.GetObject" turns out to "exist"

 

I suppose a "while vs.GetObject(TESTNAME) is not None:"-loop to finish when no object in the file turns out to have the TESTNAME, and, else the loop get's finished.

 

WHAT'S WRONG THERE?

 

 

TESTNAME = 'START'
while vs.GetObject(TESTNAME) is not None:
	TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME)
	if vs.GetObject(TESTNAME) is not None:
		vs.AlrtDialog('Object with Name '+TESTNAME+'already exists')
	else:
		break

 

 

or

 

TESTNAME = 'START'
while True:
	TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME)
	TESTNAMEh = vs.GetObject(TESTNAME)
	TESTNAMETEST = vs.GetName(TESTNAMEh)
	if TESTNAMETEST =='':
		vs.AlrtDialog('Object with Name '+TESTNAME+'already exists')
	else:
		break

 

NAMETESTv2024.zip

Link to comment

doesn't work. 

This one instead works:

 

TESTNAME = 'START'
while True:
	TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME)
	if vs.GetObject(TESTNAME) != vs.Handle(0):
		vs.AlrtDialog('Object with Name '+TESTNAME+' already exists')
	if vs.GetObject(TESTNAME) == vs.Handle(0):
		break

 

Link to comment

Hello,

 

Marisas code works fine. Its just that the while loop is not running at all if you dont have an object with the name "START" to begin with, wich may not be what you want.

 

One could say that the two loops you posted in the beginning are two different kind of while loops.

The first one is a normal while loop that runs as long as a condition is true, but if the condition is false right from the beginning, than the body of loop never gets executed.

The second loop behaves more like a "do-while" loop. Its mostly the same as the while loop, with the difference that its body is executed at least one time.

 

However in your last example you dont realy need the second if statement. Something like this:

TESTNAME = 'START'
while True:
	TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME)
	if vs.GetObject(TESTNAME) != vs.Handle(0):
		vs.AlrtDialog('Object with Name '+TESTNAME+' already exists')
	else:
		break

 or this:

TESTNAME = 'START'
while True:
	TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME)
	if vs.GetObject(TESTNAME) != vs.Handle(0):
		vs.AlrtDialog('Object with Name '+TESTNAME+' already exists')
		continue
	break

also works.

 

Regards,

Letti

Link to comment

yeah, now it works, don't know what I made wrong yesterday...

 

funny, that neither the Script Editor nor the function reference on the developer page know vs.Handle(). Seems to be not a very importan function, however it exists.

 

what does it do and how does it work? Why is there a 0 in the brackets?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...