Casting RadioButtonList

S

sck10

Hello,

I am converting some code from VB to C# and ran into a conversion error:

Cannot implicitly convert type 'object' to
'System.Web.UI.WebControls.RadioButtonList'. An explicit conversion exists
(are you missing a cast?)

Any help with this would be appreciated...

sck10



VB
======
Protected Sub rblApprovalStatus_DataBinding(ByVal sender As Object, ByVal e
As EventArgs)
Dim rlist As RadioButtonList = sender
End Sub


C#
=======
protected void rblApprovalStatus_DataBinding(object sender, EventArgs e)
{
RadioButtonList rlist = sender;
}

Error Statement
 
G

Guest

In c# you need to explicity cast from type to another. Try this:

protected void rblApprovalStatus_DataBinding(object sender, EventArgs e)
{
RadioButtonList rlist = (RadioButtonList) sender;
}

Jason Vermillion
 
S

Steven Cheng[MSFT]

Hi Steve,

As Jason has mentioned, in C# it always force you to use explicit object
cast and won't let VB style late bind code. For you scenario, you need to
explicitly cast the "sender" to the RadioButtonList control instance.

In addition to the following style cast:

RadioButtonList rlist = (RadioButtonList) sender;

you can also use the "as" keyword to do type casting in C# which can avoid
invalid cast exception when the type mismatches. e.g.

RadioButtonList list = sender as RadioButtonList;

if(list != null)
{
//do .....
}


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sck10

Thanks Jason,

sck10


Jason Vermillion said:
In c# you need to explicity cast from type to another. Try this:

protected void rblApprovalStatus_DataBinding(object sender, EventArgs
e)
{
RadioButtonList rlist = (RadioButtonList) sender;
}

Jason Vermillion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top