Solved

Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.DropDownList'

Posted on 2006-07-03
5
462 Views
Last Modified: 2010-04-16
hi experts,

public void SetDropDownIndex(object sender, System.EventArgs e)
            {
                  string strItem;
                  System.Web.UI.WebControls.DropDownList ed;
                  ed = sender;=========================================================> this being highlighted
                  ed.SelectedIndex = ed.Items.IndexOf(ed.Items.FindByText(strItem));
            }

hi expert,

How do I get around this

Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.DropDownList'
0
Comment
Question by:SirReadAlot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Assisted Solution

by:WinterMuteUK
WinterMuteUK earned 250 total points
ID: 17031229
You need to do:

   ed = sender as DropDownList

then check if it's null!

Wint.
0
 
LVL 3

Accepted Solution

by:
Bowmanje earned 250 total points
ID: 17031239
You need to explicitly cast the object.  Like this...

ed = (DropDownList) sender;

HTH,
-Josh
0
 

Author Comment

by:SirReadAlot
ID: 17031259
will try all

thanks
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17031378
The difference between mine and Bowmanje's solution is that in my case the 'ed' var will be set to 'null' if the Sender isn't a DropDownList, but it won't complain, with Bowmanje's you will get an Exception thrown if sender isn't a DropDownList.

So in my version you should do:

  ed = sender as DropDownList;
  if(ed == null)
  { /* ERROR */ }

and you should wrap Bowmanje's in a try/catch:

  try{
    ed = (DropDownList) sender;
    }
   catch(InvalidCastException)
   {
      /* ERROR */
   }

Wint.
0
 

Author Comment

by:SirReadAlot
ID: 17031604
thanks guys
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Release Dynamically Allocated Memory in C# 3 47
itextsharp with c# 3 18
ASP.NET - Why is accordion not working? 3 50
WPF user control poped up from a window 1 35
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question