3

I'm relatively new to C# - but come from a C/C++ background.

I need a data type (class) that is similar to DataTable, but allows the stored columns to hold "simple" types (int, float, boolean, [string]) AS WELL as data of the same type (so that a column could hold another table which also has columns that stores tables etc).

In C++ parlance, what I am describing is something along these lines:

typedef union { /*... */ } ValueType;
typedef std::vector<ValueType> ColumnValues; 

class Column
{
    private:
       std::string m_name ;
       ColumnValues m_values;

    public:
       Column(const std::string& name);
       // ...
}

class Table
{
   private:
      std::string m_name;
      std::vector<Column> m_cols;

    public:
       Table(const std::string& name, const std::vector<Column> *cols_ptr = NULL);
       // ...   
};

A column can hold any valid data type - which includes a Table data type (hence the implicit infinite nesting capability).

My initial thought approach was to inherit from DataTable - but thought I'd com in here to check if:

  1. If such a class already exists somewhere (either in the .Net library or elsewhere)
  2. if that is the correct way of going about it

In the event that I will need to "roll my own", I would appreciate some pointers (i.e. code snippets) to help me get started.

[Edit]

Proposed usage: I intend to use this data type primarily to serialize objects in my library (so I can send them in message packets etc), but also, so I can use them as a kind of data dictionary that allows me to store data of arbitrary complexity.

Raidri
  • 17,258
  • 9
  • 62
  • 65
oompahloompah
  • 9,087
  • 19
  • 62
  • 90

2 Answers2

4

I am not sure if I'm understanding corrrectly, but what is preventing you from creating a DataColumn with DataTable as it's type?

Something like the following:

DataTable myTableOfTables = new DataTable("TableOfTables);
myTableOfTables.Columns.Add(new DataColumn("Table", typeof(DataTable)));

Now you can go ahead and do the following:

myTableOfTables.Rows.Add(myTableOfTables.NewRow()[0] = new DataTable("NestedTable"));

And sure enough if you check in the immeadiate window:

? myTableOfTables.Rows[0][0]

You will get:

{NestedTable}
    base {System.ComponentModel.MarshalByValueComponent}: {NestedTable}
    CaseSensitive: false
    ChildRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
    Columns: {System.Data.DataColumnCollection}
    Constraints: {System.Data.ConstraintCollection}
    DataSet: null
    DefaultView: {System.Data.DataView}
    DisplayExpression: ""
    ExtendedProperties: Count = 0
    HasErrors: false
    IsInitialized: true
    Locale: {es-ES}
    MinimumCapacity: 50
    Namespace: ""
    ParentRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
    Prefix: ""
    PrimaryKey: {System.Data.DataColumn[0]}
    RemotingFormat: Xml
    Rows: {System.Data.DataRowCollection}
    Site: null
    TableName: "NestedTable"

So you already have a class that allows this kind of structure: DataTable

If what you want is one DataColumn that is able to contain whatever value, be it an int, a string or what have you, then simply define its type as object.

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • After reading up a bit on DataTables, I think I was possibly trying to reinvent the wheel here... DataTable seems to do all that I wanted to do already .... – oompahloompah Aug 01 '11 at 13:58
0

in theory you could have a table with a string column in which you serialize the nested table as XML, in .NET you can simply call the WriteXML method on the DataSet object...

this in theory, it really depends on "why" you want to do this and "what" you need it for.

Wouldn't a list of tables be enough? for example, using .NET Generics you could be able to create types like this:

List<DataTable>;
List<List<DataTable>>;

the main question would be how will you consume your data structure afterwards?

Davide Piras
  • 43,984
  • 10
  • 98
  • 147