Solved

Conversion from base classes

Posted on 2004-08-26
6
167 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to read json value 2 49
What is "public object" in C#? 3 47
Video Player 2017 5 24
HttpPostedFile 1 26
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

778 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