-
Mar 21st, 2023, 06:21 PM
#1
Thread Starter
Lively Member
[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!!!
-
Mar 21st, 2023, 06:35 PM
#2
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.
-
Mar 21st, 2023, 06:38 PM
#3
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2023, 06:40 PM
#4
Re: Need help with errors when I turned strict option on
As for the issue, this:
vb.net Code:
AddHandler Spots(x).Click, AddressOf Picbuttonhandler()
should be this:
vb.net Code:
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:
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:
Dim btn = DirectCast(sender, Button)
You can then access all the members of the Button type via that variable that is type Button.
-
Mar 21st, 2023, 09:55 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|