# If conditional prevents placement of object

**URL:** <https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995>\
**Category:** General\
**Created:** [January 21, 2025, 10:53pm UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995 "2025-01-21T22:53:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carl\_Gibson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@Carl\_Gibson](https://forum.xojo.com/u/Carl_Gibson)\
**Post date:** [January 21, 2025, 10:53pm UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/1 "2025-01-21T22:53:30Z")

</div>

Greetings,

I have two nested if loops that generate desktopOval objects. No problem. However, when I put in an if/then conditional to jump out of the inner loop, nothing is displayed; not even earlier objects. I tried oval.refresh. laserDot() is in a set. Here is a bit of the code:

For i, For x

for x=laser(i).left to laser(i).left + 200 paint out 200 ovals (later this will be undefined) therefore why the color.blue conditional

Var d As DesktopOval=New laserDot

laserDot(n).fillcolor=Laser(i).colour  
laserDot(n).Width=1  
laserDot(n).Height=1  
laserDot(n).Visible=true

‘oval is placed on the screen  
laserDot(n).left=x+laser(i).Width/2 '~301  
laserDot(n).top=laser(i).top+ slope\*(x-laser(i).left)+laser(i).Height/2

‘check for a color.blue pixel at the location of the most currently generated oval

‘this does work! However, the if conditional prevents any oval from being painted

pixelColor=rgbs.Pixel(laserDot(n).left, laserDot(n).top)  
blue=pixelColor.Blue.ToString.ToInteger

‘the process is to stop if the line of ovals encounter a blue border  
if blue\>253 then continue For i  
Do loops don’t work, same issue.

next x  
next i

Thanks

---

<div class="post-metadata">

**Author:** ![Keith\_Culotta](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@Keith\_Culotta](https://forum.xojo.com/u/Keith_Culotta)\
**Post date:** [January 21, 2025, 11:30pm UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/2 "2025-01-21T23:30:19Z")

</div>

A thought. I had a similarly mysterious thing happen and it resolved itself when the Var declaration was placed outside the loop.

---

<div class="post-metadata">

**Author:** ![Jerry\_Fritschle](https://forum.xojo.com/user_avatar/forum.xojo.com/jerry_fritschle/32/223_2.png) [@Jerry\_Fritschle](https://forum.xojo.com/u/Jerry_Fritschle)\
**Post date:** [January 21, 2025, 11:45pm UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/3 "2025-01-21T23:45:31Z")

</div>

> [@Keith\_Culotta](#):
>
> I had a similarly mysterious thing happen and it resolved itself when the Var declaration was placed outside the loop.

Probably not “mysterious”, since if you declare within the loop, it will be scrubbed and re-instantiated every time through. 🙂

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [January 22, 2025, 12:52am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/4 "2025-01-22T00:52:39Z")

</div>

Step through the code, and/or add

`System.DebugLog Str(Blue)`

right after

`blue=pixelColor.Blue.ToString.ToInteger`

and whatever the problem is will probably become apparent.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [January 22, 2025, 1:23am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/5 "2025-01-22T01:23:23Z")

</div>

Not mysterious at all. Thats how the language is defined. Any variable you Var within a loop ceases to exist when you exit the loop.

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [January 22, 2025, 1:57am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/6 "2025-01-22T01:57:54Z")

</div>

> [@Tim\_Hare](#):
>
> Not mysterious at all. Thats how the language is defined. Any variable you Var within a loop ceases to exist when you exit the loop.

That’s not relevant here, because the window holds a reference to any UI elements that are placed into them.

> [@Carl\_Gibson](#):
>
> ```auto
> Var d As DesktopOval=New laserDot
> 
> laserDot(n).fillcolor=Laser(i).colour
> 
> ```

This is probably the issue. Instead of referring to laserDot(n), refer to d:

`d.Fillcolor = Laser(i).Color`

That guarantees that you’re always manipulating the object you intend to, eliminating the possibility of slippage if n is not the correct value.

---

<div class="post-metadata">

**Author:** ![Greg\_O](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o/32/22785_2.png) [@Greg\_O](https://forum.xojo.com/u/Greg_O)\
**Post date:** [January 22, 2025, 2:54am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/7 "2025-01-22T02:54:00Z")

</div>

You wouldn’t happen to have a blue line on the left would you? It seems that if that were the case, the first laser dot would hit the left blue area and prevent drawing as you said.

---

<div class="post-metadata">

**Author:** ![Jerry\_Fritschle](https://forum.xojo.com/user_avatar/forum.xojo.com/jerry_fritschle/32/223_2.png) [@Jerry\_Fritschle](https://forum.xojo.com/u/Jerry_Fritschle)\
**Post date:** [January 22, 2025, 3:05am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/8 "2025-01-22T03:05:00Z")

</div>

> [@Eric\_Williams](#):
>
> > [@Tim\_Hare](#):
> >
> > Not mysterious at all. Thats how the language is defined. Any variable you Var within a loop ceases to exist when you exit the loop.
> 
> That’s not relevant here, because the window holds a reference to any UI elements that are placed into them.

Personally, I think the OP should humor Tim (and me), and try doing the Vars outside the loops. 🙂

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [January 22, 2025, 6:05am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/9 "2025-01-22T06:05:10Z")

</div>

> [@Jerry\_Fritschle](#):
>
> Personally, I think the OP should humor Tim (and me), and try doing the Vars outside the loops. 🙂

I agree that it’s generally good practice, but I don’t think it’s the issue here. The variable created in the Var reference is literally never used.

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [January 22, 2025, 6:07am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/10 "2025-01-22T06:07:56Z")

</div>

The glaring error in this code is the missing definition of `n`. The value assignment of this variable is not shown in the code – and definitely isn’t incremented as the loop creates more and more LaserDot objects – so this is probably where the error is. See my post above that shows how to revise the code to properly reference the object you are creating.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [January 22, 2025, 6:57am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/11 "2025-01-22T06:57:08Z")

</div>

I suggest OP to create a sample project that shows the problem. Zip it and upload here. Sometimes that helps to find the problem.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [January 22, 2025, 10:21am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/12 "2025-01-22T10:21:05Z")

</div>

> [@Carl\_Gibson](#):
>
> `For i, For x`

This won’t compile.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [January 22, 2025, 10:30am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/13 "2025-01-22T10:30:34Z")

</div>

There is nothing in the code the OP posted that suggests where/how he is adding d to laserDot. Also, my comment was directed to Keith. Should have quoted him.

Not to mention that the OP is using “laserDot” as both a class name and an array name.

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [January 22, 2025, 11:48am UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/14 "2025-01-22T11:48:13Z")

</div>

this code example is incompleate and chaotic, you should rewrite.

instead of DesktopOval maybe use OvalShape for drawing because you read color from rgb surface.  
[https://documentation.xojo.com/api/graphics/ovalshape.html](https://documentation.xojo.com/api/graphics/ovalshape.html)

---

<div class="post-metadata">

**Author:** ![Carl\_Gibson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@Carl\_Gibson](https://forum.xojo.com/u/Carl_Gibson)\
**Post date:** [January 22, 2025, 3:58pm UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/15 "2025-01-22T15:58:25Z")

</div>

You are soooo right! Learned my lesson about var. I had a similar problem earlier in the code, but didn’t make the connection!

I’m sure somewhere in the millions of words of documentation the locality of var.

Thanks for your help! Have a great programming new year.

Carl

---

<div class="post-metadata">

**Author:** ![Carl\_Gibson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@Carl\_Gibson](https://forum.xojo.com/u/Carl_Gibson)\
**Post date:** [January 22, 2025, 4:00pm UTC](https://forum.xojo.com/t/if-conditional-prevents-placement-of-object/83995/16 "2025-01-22T16:00:22Z")

</div>

Case closed. It’s all about the locality of var!! Thanks for your help.
