Paolo Ursini
Paolo Ursini

Reputation: 280

About inheritance and cast

I am facing a problem that I can't figure out how to manage.

I have developed a class that implements a non unique index on a DataTable; for handling commodity, I was thinking to develop my IndexedTable that, obviousvly, inherit from DataTable, adding a method to perform the search on the table using the non unique index

I wrote:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DWH_Core
{
    class IndexedTable : DataTable
    {
        //private  DataTable m_tbl;
        //public Dictionary<Int32, Int32> idx;

        private Index_NonUnique m_idx = null;
        public void buildNonUniqueIndex(string keyField)
        {
            m_idx = new Index_NonUnique();
            m_idx.buildIndex(this, keyField);

            return;
        }

        public DataRow[] search (string key)
        {
            return m_idx.search(key);
        }

        public DataRow[] search(int key)
        {
            return m_idx.search(key);
        }

        public DataRow[] search(short key)
        {
            return m_idx.search(key);
        }

    }
}

and the related class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DWH_Core
{
    class Index_NonUnique
    {
        private string m_KeyField;
        private ILookup<object, DataRow> m_IdxByKey;

        public void buildIndex(DataTable tbl, string keyField)
        {
            m_KeyField = keyField;

            IEnumerable<DataRow> enumerable = tbl.AsEnumerable();
            m_IdxByKey = enumerable.ToLookup(o => o[keyField]);
        }


        public DataRow[] search(string keyValue)
        {
            return m_IdxByKey[keyValue].ToArray();
        }

        public DataRow[] search(int keyValue)
        {
            return m_IdxByKey[keyValue].ToArray();
        }

        public DataRow[] search(short keyValue)
        {
            return m_IdxByKey[keyValue].ToArray();
        }
    }
}

The problem is that when I generate the DataTable class in one of the usual way, and try to cast it to IndexedTable, I got the runtime error "Impossible to execute cast from objects of type 'System.Data.DataTable' on type 'DWH_Core.IndexedTable'.". I don't want to use a private DataTable m_tbl member, because I would like to apply all the DataTable member to my IndexedTable object, but I cannot understand how to manage this error. At the end, my goal is to write something like this:

 IndexedTable tmpTable = null;
 SqlCommand cmd = "SELECT * FROM SOME_TABLE"
 SqlDataReader rdr = cmd.ExecuteReader();

 m_SAP_Allocazione = (IndexedTable)rdr.ToTable();
 m_SAP_Allocazione.buildNonUniqueIndex("PERNR");

Upvotes: 0

Views: 78

Answers (1)

aleha_84
aleha_84

Reputation: 8539

Here is some answers about casting objects to derived classes.

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?

Convert base class to derived class

Answer is no, this is not possible.

The simplest way to do this would be to do what you suggested: create a DerivedClass(BaseClass) constructor

Upvotes: 1

Related Questions