Results 1 to 5 of 5

Thread: [RESOLVED] Need help with errors when I turned strict option on

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    113

    Resolved [RESOLVED] Need help with errors when I turned strict option on

    I want to convert my vb program to C#. I thought I turned strict option on, but it wasn't. I turned it on and wound up with several errors. I corrected most of them, but now have 3 errors that I'm not sure how to correct. (The 3 errors are the same addhandler error so when I get one fixed, I can fix all three.)
    Code:
    For x = 1 To 80
                Spots(x) = New Windows.Forms.Button With {
                    .Font = New System.Drawing.Font("Tahoma", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)),
                    .ForeColor = System.Drawing.Color.Yellow,
                    .BackColor = System.Drawing.Color.FromArgb(183, 225, 228),
                    .Location = New System.Drawing.Point(aAcross, aDown),
                    .Size = New System.Drawing.Size(54, 54),
                    .Text = x.ToString("D2"),
                    .TextAlign = System.Drawing.ContentAlignment.MiddleCenter,
                    .Tag = x.ToString("D2"),
                    .Name = "Newbutton" & x.ToString("D2"),
                    .Image = Green_ball_50
                              }
                        AddHandler Spots(x).Click, AddressOf Picbuttonhandler()  (this is error BC36663, more explanation about this error below)
    			End Sub
      Private Sub Picbuttonhandler(Sender As Button, e As EventArgs)
            Pick_Pick = CInt(Sender.Tag)
            Buttonvalue(Pick_Pick) = Not Buttonvalue(Pick_Pick)
            If Buttonvalue(Pick_Pick) = True Then
                Sender.Image = Redsphere49
                Sender.ForeColor = Color.Yellow
            
            Else
                Sender.Image = Green_ball_50
                Sender.ForeColor = Color.Yellow
            End If
    I get an error that reads BC36663: Option Strict On does not allow narrowing in implicit type conversions between method '<methodname>' and delegate '<delegatename>'

    So then, I changed the function to change the type from button to integer.
    Code:
    				Private Sub Picbuttonhandler(sender As Object, e As EventArgs)
            j = CType(sender, Integer)
            Pick_Pick = CInt(Spots(j).Tag)
            Buttonvalue(Pick_Pick) = Not Buttonvalue(Pick_Pick)
            If Buttonvalue(Pick_Pick) = True Then
                Spots(j).Image = Redsphere49
                Spots(j).ForeColor = Color.Yellow
    else			 
    			 Spots(j).Image = Green_ball_50
                Spots(j).ForeColor = Color.Yellow
                    End If
    	End Sub
    Now the error is:
    System.InvalidCastException: 'Conversion from type 'Button' to type 'Integer' is not valid.
    Can someone help me figure this out? Thanks
    I don't program, I beat code into submission!!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Need help with errors when I turned strict option on

    Firstly, the formatting of your code is all over the place. Given that VS will format the code for you, there's no excuse for posting ugly code that is hard for us to read. You need to do everything you can to help us help you.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Need help with errors when I turned strict option on

    It should definitely be sender as object, then…
    Dim b as Button = DirectCast(sender, Button)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Need help with errors when I turned strict option on

    As for the issue, this:
    vb.net Code:
    1. AddHandler Spots(x).Click, AddressOf Picbuttonhandler()
    should be this:
    vb.net Code:
    1. AddHandler Spots(x).Click, AddressOf Picbuttonhandler
    You're not calling the method so you don't use an argument list. It's the address of the method itself that you are specifying, not the address of the result of calling the method. Also, this:
    vb.net Code:
    1. Private Sub Picbuttonhandler(sender As Object, e As EventArgs)
    The sender parameter is ALWAYS type Object because the same method can be used to handle any event. You then cast sender as the appropriate type, e.g.
    vb.net Code:
    1. Dim btn = DirectCast(sender, Button)
    You can then access all the members of the Button type via that variable that is type Button.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    113

    Re: [RESOLVED] Need help with errors when I turned strict option on

    Thank you for your help, .Paul and jmcilhinney. Solved my problem.
    I don't program, I beat code into submission!!!

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