Solved

Conversion from base classes

Posted on 2004-08-26
6
169 Views
Last Modified: 2010-04-15
I have written a class, ObjectBase, that is the base for two other classes, OrganizationalPerson and OrganizationalUnit.  OrganizationalUnit is an Enumerable class that can either contain a list of OrganizationalPersons or other OrganizationalUnits.   The following is my implementation of Current:

public object Current
{
      get { return new ObjectBase(_searchResult[_index].GetDirectoryEntry()); }
}

Everything works until I try the following:

foreach(OrganizationalUnit o in ou)
      Response.Write(String.Format("{0}<br>", o.name));

I get a "Specified Cast is not valid" error when trying to run the above code.  When I try to write an implicit or an explicit conversion, the code will not compile.  How can I accomplish this?
0
Comment
Question by:dirtdart
[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
6 Comments
 
LVL 3

Assisted Solution

by:GrumbleBot
GrumbleBot earned 250 total points
ID: 11909087
You want variable o to be Object?

if so you would want

foreach(Object o in ou)
    Response.Write(String.Format("{0}<br>", o.Name.ToString()));

Hope this helps
Good luck
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 11909302
** NOT FOR POINTS ***

  foreach(ObjectBase o in ou)
    Response.Write(String.Format("{0}<br>", o.Name.ToString()));

* Note that it is ObjectBase, not object (assuming that ObjectBase.Name is a valid method).
0
 
LVL 1

Expert Comment

by:johanjohansson
ID: 11910518
Why do you use the 'new' keyword when getting an item from the list? Why not

public object Current
{
     get { return _searchResult[_index].GetDirectoryEntry(); }
}

or

public ObjectBase Current
{
     get { return (ObjectBase)_searchResult[_index].GetDirectoryEntry(); }
}
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 5

Author Comment

by:dirtdart
ID: 11911184
johanjohansson:  That is done because _searchResult[_index].GetDirectoryEntry() does not cast directy to ObjectBase.  It is passed as a parameter to the constructor, and all information loaded into the ObjectBase through it.
0
 
LVL 5

Author Comment

by:dirtdart
ID: 11911206
GrumbleBot:  No, I want o to be type OrganizationalUnit.

eternal_21:  That would work for the name property.  Unfortunately, there are some other properties that are not present in ObjectBase that I have to access. However....
0
 
LVL 5

Author Comment

by:dirtdart
ID: 11911324
Well, the two of you got me close.  I created a new constructor for OrganizationalUnit that acccepts an ObjectBase as an argument.  Then did this:

foreach(ObjectBase o in ou)
{
      OrganizationalUnit u = new OrganizationalUnit(o);

I still can't data bind it, but I can work around that.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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…

696 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