Click here to Skip to main content
15,884,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing one class library for my projects.
I am having some casting problem please help me to resolve the problem.

here are class list.
IComposite Class
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary_V1._0.Steps
{
    public interface IComposite :IEnumerable
    {
        string Name { set; get; }
        string NameSym { get; }
        string NameWide { get;set;}
        int Count { get;}
        int LastError { get; }
        string Type { get;}
        int Handle {  get; }

        IComposite Parent {  get; }
        IComposite HardwareDatum {  get; }
        EnumEditabilityFlags Editability { get;  set; }
        EnumVisiblityFlags   Visibility { get;  set; }
        
        //IComposite PartStep { get; }
        

        void Add(object AvpThing, int Index, EumCAddOption AddOption);
        void Remove( int Index);
        void Clear();
        
        //IEnumerator GetEnumerator();
    }
}


ICollection Class
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary_V1._0.Steps
{
    public interface ICollection : IComposite
    {

    }
}


istep
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary_V1._0.Steps
{
    public interface IStep : IComposite
    {


        int Index { get; }
        EnumStepCategory Category { get; set; }
        IStep Prev { get; }
        IStep Next { get; }
        ICollection Datums { get; }
        //int RunPassed { get; }
        //int RunStatus { get; }
        EnumStepStateFlags StateFlags { get; set; }
        

        Datum Datum(string DatumSymName);
        DatumList DatumList(EnumDatumCategory Cat);
        IDatum get_Datum(string CymName);
        DatumList get_DatumList(EnumDatumCategory Cat);

        IStep AddStep(object StepOrType, EnumStepCategory WhichCategory, IStep Relative, ECAddOption Option);
        IStep AddStep(string Name);
        IStep AddStep(string Type, string Name);
        IDatum AddDatum(object DatumOrType, EnumDatumCategory WhichCategory, IDatum relative, ECAddOption Option);
        //void RemoveDatum(object Index); 
        void RemoveStep(int Index, int DelChildStep);
        int MoveStep(IStep DtepSrc, IStep DtepDst, int InsertBefore);
        //int CanBeContainedBy(IStep Step);
        //int CanContain(IStep Step);
        //IStep Cut(int NIndex);
        IComposite Find(string NameOrType, EnumFindOption Option, EnumStepCategory WhichCategory); //= EnumAvpStepCategory.S_ALL
        ICollection FindByType(string StepType, int FindInAllChildren);
        StepList GetChildList(EnumStepCategory Cat);
        int ChildCount(EnumStepCategory Category);
        IStep FirstChild(EnumStepCategory Category);



        //void ApplyChanges(bool DoingPaste);
        ////int Run();
        ////int RunStart(int NumIterations);
        ////int RunStop();
        //int Draw();
        //byte[] BinDataGet();
        //void BinDataSet(ref byte[] ByteArray);
        
        
        //bool TryStart(bool BDoAcquire, bool BUseTriggers);
        //int TryStop();
    }
}

iDatum
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary_V1._0.Steps
{
    public  interface IDatum :IComposite
    {


        EnumDatumCategory Category { get; }
        object Value { get;set;}
        string ValueAsString { get;set;}
        object ValueCalibrated { get;set;}
        int NumScalars { get; }
        int Visible { get;set;}
        IDatum AddDatum(object DatumOrType, 
            EnumDatumCategory WhichCategory, IDatum Relative, ECAddOption Option);
        IDatum AddDatum(string Name);
        IDatum AddDatum(string Type, string Name);
        
        //IComposite Owner {  get; }
        //IDatum Prev { get;}
        //IDatum Next { get;}

        //int IsReference { get; }
        //int LastError { get; }
        


        //IComposite FindParent(string StepType);
        //IDatum     Find(string Name,  EnumFindOption Option, EnumDatumCategory WhichCategory );

        //Datum ReferenceGet();
        //void ReferenceSet(IDatum RefDatum);
    }

}


StepClass
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary_V1._0.Steps
{
    public class StepClass : IStep, Step
    {
        internal string mName;
        internal string mNameSym;
        internal string mNameWide;
        internal string mType;

        internal StepList mStepList;
        internal DatumList mDatumList;
        internal int nIndex;
        internal EnumStepCategory mCategory;
        internal EnumStepStateFlags mStateFlags;
        internal EnumVisiblityFlags mVisibility;

        //Why This is here 
        IDatum m_ResultDatum;
        private ICollection RetCollection;
        private int ChildCounter;
        private IStep RetStep;


        public StepClass()
        {
            mStepList = new StepList();
            mDatumList = new DatumList();
        }
        public string Name
        {
            get
            {
                return mName;
            }
            set
            {
                mName = value;
            }
        }

        public string NameSym
        {
            get
            {
                return mNameSym;
            }
            set
            {
                mNameSym = value;
            }
        }

        public string NameWide
        {
            get
            {
                return mNameWide;
            }
            set
            {
                mNameWide = value;
            }

        }

        public string Type
        {
            get
            {
                return "MotherStep";
            }
            set
            {
                mType = value;
            }
        }

        public EnumVisiblityFlags Visibility
        {
            get
            {
                return mVisibility;
            }

            set
            {
                mVisibility = value;
            }
        }


        public int Index
        {
            get
            {
                return nIndex;
            }
        }

        public EnumStepCategory Category
        {
            get
            {
                return mCategory;
            }
            set
            {
                mCategory = value;
            }
        }

        public IStep Prev
        {
            get
            {
                nIndex--;
                if (nIndex >= 0)
                {
                    return mStepList[nIndex];
                }
                else
                {
                    throw new IndexOutOfRangeException("No more Element in StelpList");
                }


            }
        }

        public IStep Next
        {
            get
            {
                nIndex++;
                if (nIndex < mStepList.Count)
                {
                    return mStepList[nIndex];
                }
                else
                {
                    throw new IndexOutOfRangeException("No more Element in StelpList");
                }
            }
        }

        public ICollection Datums
        {
            get
            {
                return (ICollection)mDatumList;
            }
        }

        public EnumStepStateFlags StateFlags
        {
            get
            {
                return mStateFlags;
            }
            set
            {
                mStateFlags = value;
            }
        }



        /**************** Method Implementations *********************/

        public Datum Datum(string DatumSymName)
        {
            return (Datum)get_Datum(DatumSymName);
        }

        public DatumList DatumList(EnumDatumCategory Cat)
        {
            return get_DatumList(Cat);
        }


        public IDatum get_Datum(string CymName)
        {

            for (int i = 0; i < mDatumList.Count; i++)
            {
                if (mDatumList[i].Name == CymName)
                {
                    m_ResultDatum = mDatumList[i];
                }
            }
            return (Datum)m_ResultDatum;
        }

        public DatumList get_DatumList(EnumDatumCategory Cat)
        {
            DatumList mDatumListResult = new DatumList();
            for (int i = 0; i < mDatumList.Count; i++)
            {
                if (mDatumList[i].Category == Cat)
                {
                    mDatumListResult.Add(mDatumList[i]);
                }
                else if (Cat == EnumDatumCategory.D_ALL)
                {
                    mDatumListResult.Add(mDatumList[i]);
                }

            }
            return mDatumListResult;
        }

        public IDatum AddDatum(object DatumOrType, EnumDatumCategory WhichCategory,
            IDatum relative, ECAddOption Option)
        {
            int foundindex;
            DatumClass newdatum = new DatumClass();
            newdatum.Type = (string)DatumOrType;
            newdatum.Category = WhichCategory;
            try
            {
                foundindex = mDatumList.IndexOf(relative);
                mDatumList.Insert(foundindex, newdatum);
            }
            catch (ArgumentOutOfRangeException ae)
            {
                mDatumList.Add(newdatum); // insert at bottom of list 
            }

            return newdatum;
        }

        public IStep AddStep(object StepOrType, EnumStepCategory WhichCategory,
            IStep Relative, ECAddOption Option)
        {
            int foundindex;
            StepClass newstep = new StepClass();
            newstep.Type = (string)StepOrType;
            newstep.Category = WhichCategory;
            nIndex++; // increment index 
            try
            {
                foundindex = mStepList.IndexOf(Relative);
                mStepList.Insert(foundindex, newstep);
                return mStepList[foundindex];
            }
            catch (ArgumentOutOfRangeException ae)
            {

                mStepList.Add(newstep); // insert at bottom of list 
                //throw new ArgumentOutOfRangeException("Could not find relative Step in List");
                return mStepList[mStepList.Count - 1];
            }

            return newstep;
        }

        public IStep AddStep(string Type, string Name)
        {
            IStep step2 = (IStep)AddStep(Type, EnumStepCategory.S_POSTPROC, null, ECAddOption.ADD_UNINIT);
            step2.Name = Name;
            return step2;
        }

        public IStep AddStep(string Name)
        {
            IStep step2 = (IStep)AddStep(null,
                EnumStepCategory.S_POSTPROC, null, ECAddOption.ADD_AFTER);
            step2.Name = Name;
            return step2;
        }

        public void Add(object AvpThing, int Index, EumCAddOption AddOption)
        {
            throw new Exception("The method or operation is not implemented.");

        }

        void RemoveStep(int Index, int DelChildStep)
        {
            mStepList.RemoveAt(Index);
        }

        #region IComposite Members


        public int Count
        {
            get { return mStepList.Count; }
        }

        public int LastError
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public int Handle
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public IComposite Parent
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public IComposite HardwareDatum
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public EnumEditabilityFlags Editability
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }


        public void Remove(int Index)
        {
            mStepList.RemoveAt(Index);
        }

        public void Clear()
        {
            mStepList.Clear();
        }


        public System.Collections.IEnumerator GetEnumerator()
        {


            /*foreach (IStep t in mStepList)
            {
                // Lets check for end of list (its bad code since we used arrays)
                if (t == null) // this wont work is T is not a nullable type
                {
                    break;
                }

                // Return the current element and then on next function call 
                // resume from next element rather than starting all over again;
                yield return t;
            }*/

            foreach (Step stp in mStepList)
                yield return stp;

        }

        #endregion



        public int MoveStep(IStep DtepSrc, IStep DtepDst, int InsertBefore)
        {
            throw new NotImplementedException();
        }

        /*public int CanBeContainedBy(IStep Step)
        {
            return mStepList.FindIndex(new System.Predicate<IStep>((value) => {
                return value.Name == Step.Name || value.NameSym == Step.NameSym ||
                    value.NameWide == Step.NameWide || value.Category == Step.Category ||
                    value.Type == Step.Type;
            }));

            //ref link http://www.codeproject.com/Questions/196826/Findindex-Method-in-System-Generics-List
        }
        */

        /*public int CanContain(IStep Step)
        {
            return mStepList.FindIndex(new System.Predicate<IStep>((value) =>
            {
                return value.Name == Step.Name && value.NameSym == Step.NameSym &&
                    value.NameWide == Step.NameWide && value.Category == Step.Category &&
                    value.Type == Step.Type;
            }));

        }
         */

        /*public IStep Cut(int NIndex)
        {
            throw new NotImplementedException();
        }
         * */

        public IComposite Find(string NameOrType, EnumFindOption Option, EnumStepCategory WhichCategory)
        {
            StepList RetStepList = new StepList();
            for (int i = 0; i < mStepList.Count; i++)
            {
                if (mStepList[i].Category == WhichCategory)
                {

                    if (Option == EnumFindOption.FIND_BY_SYMNAME && mStepList[i].Name == NameOrType)
                    {
                        RetStepList.Add(mStepList[i]);

                    }
                    else if (Option == EnumFindOption.FIND_BY_TYPE && mStepList[i].Type == NameOrType)
                    {
                        RetStepList.Add(mStepList[i]);

                    }
                    else if (Option == EnumFindOption.FIND_BY_USERNAME)
                    {

                    }
                }
            }
            return (IComposite)RetStepList;
        }

        public ICollection FindByType(string StepType, int FindInAllChildren)
        {

            StepList RetStepList = new StepList();
            for (int i = 0; i < mStepList.Count; i++)
            {
                if (mStepList[i].Type == StepType)
                {
                    RetStepList.Add(mStepList[i]);
                }
            }
            return (ICollection)RetStepList;
        }

        public StepList GetChildList(EnumStepCategory Cat)
        {
            //    ICollection RetCollection;
            StepList t = new StepList();
            for (int i = 0; i < mStepList.Count; i++)
            {
                if (mStepList[i].Category == Cat)
                {
                    t.Add(mStepList[i]);
                }
                else if (Cat == EnumStepCategory.S_ALL)
                {
                    t.Add(mStepList[i]);
                }
            }
            return t;

        }

        public int ChildCount(EnumStepCategory Category)
        {
            // int ChildCounter;
            for (int i = 0; i < mStepList.Count; i++)
            {
                if (mStepList[i].Category == Category)
                {

                    ChildCounter += mStepList[i].Datums.Count;
                }
            }
            return ChildCounter;
        }


        public IStep FirstChild(EnumStepCategory Category)
        {
            for (int i = 0; i < mStepList.Count; i++)
            {
                if (mStepList[i].Category == Category)
                {
                    RetStep = mStepList[i];
                    break;
                }
            }
            return RetStep;
        }


        void IStep.RemoveStep(int Index, int DelChildStep)
        {
            mStepList.RemoveAt(Index);
        }
    }
}


C#
public ICollection Datums
       {
           get
           {
               return (ICollection)mDatumList;
           }
       }

In above propery i have casting problem.
Posted
Comments
George Jonsson 16-Sep-14 23:49pm    
Of all the presented code, most of it unnecessary to show, where is the class DatumList?
Member 8825505 17-Sep-14 2:21am    
Its an class for the list<datums>
Member 8825505 17-Sep-14 2:22am    
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary_V1._0.Steps
{
public class DatumList : List<idatum>
{
public DatumList()
{

}

public DatumList(ICollection Collection)
{
foreach (IDatum item in Collection)
{
base.Add(item);
}
}
}
}
George Jonsson 17-Sep-14 2:37am    
Please don't put code in the comment section.
Update your question with this class.
Basically you only need to post this class and the property where it fails.
Too much code will make people not even bother to try to help you.
Member 8825505 17-Sep-14 2:28am    
only thing which i want to do is want to implement enumerable methods for all objects of the list classes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900