Logic problem with absolute value

  • Thread starter Thread starter Jeff Ciaccio
  • Start date Start date
J

Jeff Ciaccio

I get an "Ambiguous Match Exception" when I try to run the code below. I
need |x1 - x2| > 80 and |y1 - y2| > 100, and I want to just loop until
this condition is true.

horz1 = Int(Rnd() * 469)
vert1 = Int(Rnd() * 245)

Do
horz2 = Int(Rnd() * 469)
vert2 = Int(Rnd() * 245)

Loop Until (Math.Abs((horz1 - horz2) > 80)) And (Math.Abs((vert1 -
vert2) > 101))

Can anybody enlighten me?

Thanks
 
You are attempting to call Math.Abs with a Boolean as the parameter:

(horz1 - horz2) > 80

and

(vert1 - vert2) > 101

You need to rationalize your parentheses.

Loop Until Math.Abs(horz1 - horz2) > 80 And Math.Abs(vert1 - vert2) > 101

You might also look at using AndAlso instead of And because this will 'short
circuit' the test if the first part of the test resolves to False.
 
That must have been it- Thanks!! It seems to work this way and when I add an
extra set like this
Loop Until (Math.Abs(horz1 - horz2) > 80) And (Math.Abs(vert1 - vert2) >
101)

I never realized that too many parenthesis could cause problems as long as
they were all closed properly.

Thanks again,
Jeff (VB Neophyte)
 
It wasn't so much a matter of too many parentheses - It was a matter of them
being in the wrong places.

Because the logic is left to right you don't actually need any at all.

You really only need them when you need to force something to be evaluated
out of the normal sequence.

Overuse of parentheses can cause a maintenance nightmare.
 
Two more quick questions if you don't mind.
1) Is there a way to rotate a picture box in VB Express 2008? I don't see
an angle or rotation property, but I'm guessing there is a way.
2) I've got 6 .bmps in My Resources, and I've set their Build Action to
"Embedded Resource", but cannot figure out the qualifier to set them at
runtime. I've got die1.bmp, die2.bmp, etc., and I've tried
picDie.ImageLocation = My.Resources."die" &x &".bmp" as well as "c:\die" &x
&".bmp" where x is a variable evaluated at runtime.

Any thougts?
 
First of all, if you post follow-up questions in a thread then they should
be directly related to the original question, otherwise, anyone who wasn't
watching this particular thread will never see them.

But ...

1. Why on earth would you want to rotate a picture box? A PictureBox
control is, in effect, nothing more than a canvas upon which you display a
image. If you mean 'is there a way to rotate an image?' then you will have
better luck postin your 'properly' question to
microsoft.public.dotnet.framework.drawing.


2. My.Resources exposes a number of properties and/or methods that allow you
to retrieve a resource by it's name. F1 and/or intellisense is your friend
for finding which property/method will work best for you.
 
Back
Top