Solved

Conversion from base classes

Posted on 2004-08-26
6
166 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
Comment Utility
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
Comment Utility
** 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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Author Comment

by:dirtdart
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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!
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

13 Experts available now in Live!

Get 1:1 Help Now