Results 1 to 5 of 5

Thread: Option Strict On causing error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    28

    Option Strict On causing error

    Hi All.. when I use Option Strict On with this code, I get errors. Can someone please tell me what is wrong with this code?

    Code:
    Dim dynamicButton As New Button
    dynamicButton.Location = New Point(Me.PictureBox2.PointToClient(MousePosition))
    The error shows on the 'Point' in New Point. I am not sure how to fix that.
    Attached Images Attached Images  
    Last edited by mikeg71; Apr 2nd, 2021 at 09:43 AM.

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    626

    Re: Option Strict On causing error

    You cannot call directly

    Code:
    dynamicButton.Location = New Point(Me.PictureBox2.PointToClient(MousePosition))
    the New point method ask for coordinate or size type, not a point.

    You can write :
    Code:
    dynamicButton.Location = Me.PictureBox2.PointToClient(MousePosition)
    
    or
    
    dynamicButton.Location = New Point(Me.PictureBox2.PointToClient(MousePosition).X,Me.PictureBox2.PointToClient(MousePosition).Y)
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    28

    Re: Option Strict On causing error

    Thanks Delaney, this does the trick.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,976

    Re: Option Strict On causing error

    Option Strict on does not cause an error... It reveals your error

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,928

    Re: Option Strict On causing error

    Quote Originally Posted by .paul. View Post
    Option Strict on does not cause an error... It reveals your error
    Indeed. The error here was that you were trying to create a new Point when you didn't need to. PointToClient returns a Point so why would you need to create a new one at all? Think of all the junky code like this that you might write without it being picked up by the compiler when you have Option Strict Off? In such cases, the system makes a best guess at what you were actually trying to achieve. On many occasions, it will guess correctly. On those that it doesn't get it right, your application will quite possibly just continue chugging along and using the wrong data. That's why you should ALWAYS have Option Strict On and learn how to write good code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width