473,659 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(inde x), 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 8973
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************* *@exisconsultin g.com

"Sefa Sevtekin" <se******@de.ib m.com.removespa m> 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(inde x), 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.Collecti ons.CollectionB ase
{

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(inde x), 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(n ew Beer
("Hamms", "Hamms Brewing Co.", 3));
inventory.Add(n ew Beer
("Budweiser" , "Anheuser-Busch", 1000));
inventory.Add(n ew Beer("Mulhollan d
Rain", "City Brewers", 113));
button1.Text = inventory[1].name;

dataGrid1.DataS ource = 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************* *@exisconsultin g.com

"Sefa Sevtekin" <se******@de.ib m.com.removespa m> 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(inde x), 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
1440
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 compression modules but I can't understand them much... Thank you very much Any good link would be appreciated of course :)
5
9789
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 have: set httpxml = Server.CreateObject("Msxml2.XMLHTTP.3.0") httpxml.open "GET", "http://www.txdps.state.tx.us/mpch/sb1063.xml", false ' get the requested XML data from the remote location ' change the URL as per your feed. httpxml.send
8
2024
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 cache misses) the whole objekt should be in one linear chunk of memory, that is the array starts where the objekt ends in memory. class Cool { Cool (int arraysize) { .. }
3
8374
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 Patnayak
4
1666
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 looking into References node in project the assembly 'MyFooAssembly' is strong key = true. So what cna be a ptoblem??? TNX
1
1390
by: San | last post by:
Hi, Why strongly named assembly can refer other strongly named assembly ? Thanks with Regards, San.
5
1535
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 force strongly named assembly1 to reference non-strongly named assembly2 ? --
1
1436
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
1761
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
1573
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 named assembly by adding the following attribute: ;
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.