Solved

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

Posted on 2006-07-03
5
455 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now