473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Creating a strongly-typed c# collection

I am trying to create a c# typesafe collection to avoid
typecasting exceptions and to ease binding of grids to
collections. I already did it in VB.NET but couldn't map
it to C#. In VB.NET you need to define a default property
in order to make binding work. It is something similiar to
this:

Default Public ReadOnly Property Item(ByVal index As
Integer) As LineItem
Get
Return CType(list(index), LineItem)
End Get
End Property

As far as I know there is no Default identifier for C#
properties.

Any help would be appriciated!!!

Sefa

Nov 15 '05 #1
3 8958
Sefa,

Default properties are BAAAAD. =)

Anyways, the equivalent in C# is by creating an indexer, like this:

public LineItem this[int index]
{
get
{
return (LineItem) list[index];
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Sefa Sevtekin" <se******@de.ibm.com.removespam> wrote in message
news:0a****************************@phx.gbl...
I am trying to create a c# typesafe collection to avoid
typecasting exceptions and to ease binding of grids to
collections. I already did it in VB.NET but couldn't map
it to C#. In VB.NET you need to define a default property
in order to make binding work. It is something similiar to
this:

Default Public ReadOnly Property Item(ByVal index As
Integer) As LineItem
Get
Return CType(list(index), LineItem)
End Get
End Property

As far as I know there is no Default identifier for C#
properties.

Any help would be appriciated!!!

Sefa

Nov 15 '05 #2
I found the answer for my own question. I've gotta use an
indexer. For the ones who has the same problem, here is a
little example I found on the web:

public class BeerInventory :
System.Collections.CollectionBase
{

public int Add(Beer value)
{
return List.Add(value);
}

public Beer this[int index]
{
get
{
return (Beer)List[index];
}
set
{
List[index] = value;
}
}
}
-----Original Message-----
I am trying to create a c# typesafe collection to avoid
typecasting exceptions and to ease binding of grids to
collections. I already did it in VB.NET but couldn't map
it to C#. In VB.NET you need to define a default property
in order to make binding work. It is something similiar tothis:

Default Public ReadOnly Property Item(ByVal index As
Integer) As LineItem
Get
Return CType(list(index), LineItem)
End Get
End Property

As far as I know there is no Default identifier for C#
properties.

Any help would be appriciated!!!

Sefa

.

Nov 15 '05 #3
One other question; I am trying to bind it to a data grid
like:

BeerInventory inventory = new BeerInventory();
inventory.Add(new Beer
("Hamms", "Hamms Brewing Co.", 3));
inventory.Add(new Beer
("Budweiser", "Anheuser-Busch", 1000));
inventory.Add(new Beer("Mulholland
Rain", "City Brewers", 113));
button1.Text = inventory[1].name;

dataGrid1.DataSource = inventory;

The grid doesn't show the records.Any idea??


-----Original Message-----
Sefa,

Default properties are BAAAAD. =)

Anyways, the equivalent in C# is by creating an indexer, like this:
public LineItem this[int index]
{
get
{
return (LineItem) list[index];
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Sefa Sevtekin" <se******@de.ibm.com.removespam> wrote in messagenews:0a****************************@phx.gbl...
I am trying to create a c# typesafe collection to avoid
typecasting exceptions and to ease binding of grids to
collections. I already did it in VB.NET but couldn't map
it to C#. In VB.NET you need to define a default property in order to make binding work. It is something similiar to this:

Default Public ReadOnly Property Item(ByVal index As
Integer) As LineItem
Get
Return CType(list(index), LineItem)
End Get
End Property

As far as I know there is no Default identifier for C#
properties.

Any help would be appriciated!!!

Sefa

.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: andrea | last post by:
I was thinking to code the huffman algorithm and trying to compress something with it, but I've got a problem. How can I represent for example a char with only 3 bits?? I had a look to the...
5
by: | last post by:
Hi, i'm trying to run an .asp page to get an xml file off a server. I need to know what object to create. My page just runs till timeout...it bombs on the httpxml.send command. This is the code I...
8
by: cody | last post by:
i basically want to create an object which contains an array (the last element of the class). the size of the array is determined when the object is created. for performance reasons (avoiding...
3
by: Pattnayak | last post by:
Hi, I want to create a DTS package programatically (preferably in C#.net),which will copy all my tables from a oracle database to my sql-server database. Can anybody help me doing this??? Thanks...
4
by: Tamir Khason | last post by:
I have a form. On form there is my control (all of control's assemblies signed by strong key), BUT while running I recieve he located assembly 'MyFooAssembly' is not strongly named. While...
1
by: San | last post by:
Hi, Why strongly named assembly can refer other strongly named assembly ? Thanks with Regards, San.
5
by: Oleg Subachev | last post by:
When I try to use strongly named assembly1 that references non-strongly named assembly2 I get the following error: "The located assembly '<assembly2 name>' is not strongly named." How can I...
1
by: DotNetJunkies User | last post by:
I have a .NET DLL that uses ADO 2.8 DLL. I am not able to "strongly name" the .NET DLL. Any comments, work arounds ? CHDe --- Posted using Wimdows.net NntpNews Component -
11
by: melon | last post by:
Let's say, I have a form class... public class MainForm : Form { private CustomClass cc; public MainForm() { DoSomething() InitializeComponent(); } /// Do something more
0
by: Dave Burns | last post by:
Hi, I have a C++ managed assembly (.dll) which links to a bunch of native libraries. Everything works fine if I don't make the managed assembly a strongly named one. Once I make it a strongly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.