PC Review


Reply
Thread Tools Rate Thread

Casting RadioButtonList

 
 
sck10
Guest
Posts: n/a
 
      10th Feb 2007
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
-----------------
Cannot implicitly convert type 'object' to
'System.Web.UI.WebControls.RadioButtonList'. An explicit conversion exists
(are you missing a cast?)


 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
Guest
Posts: n/a
 
      10th Feb 2007
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

"sck10" wrote:

> 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
> -----------------
> Cannot implicitly convert type 'object' to
> 'System.Web.UI.WebControls.RadioButtonList'. An explicit conversion exists
> (are you missing a cast?)
>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      12th Feb 2007
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.

 
Reply With Quote
 
sck10
Guest
Posts: n/a
 
      12th Feb 2007
Thanks Jason,

sck10


"Jason Vermillion" <(E-Mail Removed)> wrote in
message news:F1D0088A-5D45-420E-B7CB-(E-Mail Removed)...
> 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
>
> "sck10" wrote:
>
>> 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
>> -----------------
>> Cannot implicitly convert type 'object' to
>> 'System.Web.UI.WebControls.RadioButtonList'. An explicit conversion
>> exists
>> (are you missing a cast?)
>>
>>
>>



 
Reply With Quote
 
sck10
Guest
Posts: n/a
 
      12th Feb 2007
Thanks for the further explanation Steven,

sck10



"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RadioButtonList dancer Microsoft ASP .NET 3 6th Aug 2007 04:26 PM
RadioButtonList Dot Net Daddy Microsoft ASP .NET 2 5th Aug 2006 12:56 AM
Down-casting of Typed Collection (Casting Generic Types?) conchur Microsoft C# .NET 1 3rd Jul 2006 11:45 AM
RadioButtonList J Microsoft ASP .NET 1 27th Sep 2004 09:56 PM
about radiobuttonlist fatboycanteen Microsoft ASP .NET 0 16th Dec 2003 06:27 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.