Solved

Conversion from base classes

Posted on 2004-08-26
6
168 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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