Logic problem with absolute value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Ciaccio

    Logic problem with absolute value

    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((horz 1 - horz2) 80)) And (Math.Abs((vert 1 -
    vert2) 101))

    Can anybody enlighten me?

    Thanks
    --
    Jeff Ciaccio
    Physics and AP Physics Teacher
    Sprayberry High School; Marietta, GA
    Blog: http://sprayberry.typepad.com/ciaccio

  • Stephany Young

    #2
    Re: Logic problem with absolute value

    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.


    "Jeff Ciaccio" <noname@noreply .orgwrote in message
    news:eEY5SAQ2IH A.4936@TK2MSFTN GP05.phx.gbl...
    >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((horz 1 - horz2) 80)) And (Math.Abs((vert 1 -
    vert2) 101))
    >
    Can anybody enlighten me?
    >
    Thanks
    --
    Jeff Ciaccio
    Physics and AP Physics Teacher
    Sprayberry High School; Marietta, GA
    Blog: http://sprayberry.typepad.com/ciaccio

    Comment

    • Jeff Ciaccio

      #3
      Re: Logic problem with absolute value

      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)

      Comment

      • Stephany Young

        #4
        Re: Logic problem with absolute value

        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.


        "Jeff Ciaccio" <noname@noreply .orgwrote in message
        news:%23EYAcfS2 IHA.4164@TK2MSF TNGP03.phx.gbl. ..
        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)

        Comment

        • Jeff Ciaccio

          #5
          Re: Logic problem with absolute value

          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.ImageLoc ation = My.Resources."d ie" &x &".bmp" as well as "c:\die" &x
          &".bmp" where x is a variable evaluated at runtime.

          Any thougts?


          "Stephany Young" <noone@localhos twrote in message
          news:%23AiAKvS2 IHA.5472@TK2MSF TNGP06.phx.gbl. ..
          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.
          >
          >
          "Jeff Ciaccio" <noname@noreply .orgwrote in message
          news:%23EYAcfS2 IHA.4164@TK2MSF TNGP03.phx.gbl. ..
          >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)
          >

          Comment

          • Stephany Young

            #6
            Re: Logic problem with absolute value

            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.publi c.dotnet.framew ork.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.


            "Jeff Ciaccio" <noname@noreply .orgwrote in message
            news:%23L3Fe5S2 IHA.3600@TK2MSF TNGP04.phx.gbl. ..
            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.ImageLoc ation = My.Resources."d ie" &x &".bmp" as well as "c:\die"
            &x &".bmp" where x is a variable evaluated at runtime.
            >
            Any thougts?
            >
            >
            "Stephany Young" <noone@localhos twrote in message
            news:%23AiAKvS2 IHA.5472@TK2MSF TNGP06.phx.gbl. ..
            >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.
            >>
            >>
            >"Jeff Ciaccio" <noname@noreply .orgwrote in message
            >news:%23EYAcfS 2IHA.4164@TK2MS FTNGP03.phx.gbl ...
            >>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)
            >>
            >

            Comment

            Working...