?
Solved

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

Posted on 2006-07-03
5
Medium Priority
?
477 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 1000 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 1000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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