Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conversion from base classes

Posted on 2004-08-26
6
Medium Priority
?
171 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 750 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 750 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
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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