HM1984

hello all
How can I define a class like this:
DataTable1.Rows[0]="1";
DataTable1.Rows.Add(Datarow1);
I mean Rows class have a function that add a DataRow and although it can be use like a List instance.
how can i difine a class like Rows.
Is that really a class or not (Because we can not define Rows like this Rows rw = new Rows())







Re: .NET Base Class Library How Can i have a Class Like Rows Class

Chetan Handa

There is no class to do this directly :

You will have to use dr = Dt.newRow()

where Dt is a datatable.

Is this what you are asking or something else





Re: .NET Base Class Library How Can i have a Class Like Rows Class

HM1984


Hi

I don't have any problem with datatable or dataset object. I want to know how can I have a class or whatever like Rows in datatable.







Re: .NET Base Class Library How Can i have a Class Like Rows Class

BonnieB

The Rows in a DataTable is a collection. Here's a very simple Collection class :

Code Block

public class MyCollection : System.Collections.CollectionBase

{

public void Add(object o)

{

this.List.Add(o);

}

public void Remove(int index)

{

// Check to see if there is an object at the supplied index.

// If no object exists, a messagebox is shown and the operation

// is cancelled.

if (index > Count - 1 || index < 0)

{

System.Windows.Forms.MessageBox.Show("Index not valid!");

}

else

{

List.RemoveAt(index);

}

}

public object Item(int Index)

{

// The appropriate item is retrieved from the List object and

// (explicitly cast if you're using other than objects, such as a Widget type)

// then returned to the caller.

// return (Widget) List[Index];

return List[Index];

}

}






Re: .NET Base Class Library How Can i have a Class Like Rows Class

HM1984

in Rows you can have for example
A: messageBox.show(Rows[0])
and
B: Rows.add(DataRow);
it mean that Rows in A Example is a Collection an in B example is a Class
but in your class we must write MyCollection.item(index) for access to the items of MyCollection. I want somthing like this:
MyCollection[0]="here";
Object o = new Object();
MyCollection.Add(o);






Re: .NET Base Class Library How Can i have a Class Like Rows Class

BonnieB

But you can't use Rows standalone. It's a collection that is part of the DataTable. So, you say MyDataTable.Rows[0].

OK, so try this instead:

Code Block

public class MyCollection : System.Collections.CollectionBase

{

public MyRows this[int index]

{

get { return (MyRows)List[index]; }

set { List[index] = value; }

}

public int Add(MyRows value)

{

return (List.Add(value));

}

}

public class MyRows

{

private string m_MyProperty = "";

public string MyProperty

{

get { return this.m_MyProperty ; }

set { this.m_MyProperty = value; }

}

}

Then you can access it the way you want to:

Code Block

MyCollection Rows = new MyCollection();

Rows.Add(new MyRows());

Rows[0].MyProperty = "One";






Re: .NET Base Class Library How Can i have a Class Like Rows Class

Chetan Handa

HM1984 wrote:
in Rows you can have for example
A: messageBox.show(Rows[0])
and
B: Rows.add(DataRow);
it mean that Rows in A Example is a Collection an in B example is a Class
but in your class we must write MyCollection.item(index) for access to the items of MyCollection. I want somthing like this:
MyCollection[0]="here";
Object o = new Object();
MyCollection.Add(o);

The "System.Collections.CollectionBase" will provide the functionality you are looking for , basically you will be creating a custom collection with entities, this also means your code may have to take responsibilty providing your own functionality of providing sort on the "Object o" E.g you may need to sort "o.name" property ascending or "o.age" descending etc.

If you are creating a collection I'm sure you may come across this requirement.