473,721 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

indexers, enumerators

Hi, I am receiving this exception

Error 3 Cannot apply indexing with [] to an expression of type
'System.Collect ions.Generic.IE numerable<Visua lSystems.DbObje cts.DbClasses.D bColumn>'
C:\Visual Studio\PROJECTS \VisualSystems. DbObjects.DbCla sses\DbObjectTa ble.cs
51 30 VisualSystems.D bObjects.DbClas ses

on this line:

string primaryKeyName = "ID";
DbColumn primaryKeyColum n = this.DbColumns[primaryKeyColum nName];

I am calling methods of this class:
public class DbColumns {
private Dictionary<Data Column, DbColumn> dictionary;
public DbColumns() {
this.dictionary = new Dictionary<Data Column, DbColumn>();
foreach (DataColumn column in this.table.Colu mns) {
this.dictionary .Add(column, new DbColumn(column )); }

public IEnumerator<DbC olumn> GetEnumerator() {
foreach (KeyValuePair<D ataColumn, DbColumn> keyValuePair in
this.dictionary ) {
yield return keyValuePair.Va lue;

public DbColumn this[string columnName] {
get {
foreach (DataColumn column in this.dictionary .Keys) {
if (column.ColumnN ame == columnName) { return
this.dictionary[column]; }
return null;


I need both access methods - array style [] and iterator via
foreach(DbColum n db in DbColumns)

How to make this compile?

CUIN Kaczy
Dec 23 '05 #1
1 1470
I solved it.
What I didn't shown was the way I used this class:

protected DbColumns dbColumns;
public IEnumerable<DbC olumn> DbColumns {
get {
if (this.dbColumns == null) {
this.dbColumns = new DbColumns(this. Table);
foreach (DbColumn dbColumn in this.dbColumns) {
yield return dbColumn;

You cannot apply brackets to IEnumerable,
promoting the dbColumns to public was the way, I only need to refactor it to
change the names. And even better (just thought about it, there is no need
to build enumerator here, since the DbColumns already is enumerable)

so the way to go would be

protected DbColumns dbColumns;
public DbColumns DbColumns {
get {
if (this.dbColumns == null) this.dbColumns = new DbColumns(this. Table);
return this.dbColumns;


and merry christmas

CUIN Kaczy
Dec 23 '05 #2

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

Similar topics

by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally add a 16-bit register and an 8-bit register since there is no encoding for this on the x86 architecture. My trouble has stemmed from the fact that enumerators are only integers and can be freely cast into other enumerators, and I was using them...
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
by: Jim | last post by:
How does one determine what indexers are available on a given object? The only way I have found is by looking at the Object Browser. But even then it only gives a simple signature like, this. What are those strings? </jim>
by: Tim Davis | last post by:
I am currently writing a class which I would like to make "enumerable" by inheriting from IEnumerable. The documentation says that the IEnumerator.MoveNext method should throw an exception if the object being enumerated has changed since the enumerator was created. Does anyone have any tips on how best to implement this? I can think of 2 approaches, neither of which seem very satisfactory. These are as follows: 1. Take a copy of the...
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. There seems to be other ways of doing the jobs of the two and less confusing. The books I read don't provide good examples of situations when I would need an array or indexer. I don't really need a definition of them as I already have that. I...
by: bonk | last post by:
Hello, IL does not have indexers. Infact the c# compiler compiles indexers to Set_Item and Get_Item (or whatever name I choose via the IndexerNameAttribute ). So how does c# (compiler) know that something in a IL Assembly is supposed to be an indexer ? And more important, what ways do I have to influence that ?
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its function? thanks for all
by: muaddubby | last post by:
Hello all and happy new year. I've seen several posts floating around asking about string enumerators in C#, and generally speaking, they're not supported. I've come up with a way around it that lets you create a class which behaves exactly like the regular enumerators (it offers all the functionality a regular enumerator has), and you can store any type of string in it. The full article can be found here:...
by: nitindel | last post by:
Hi All, May i have an in depth article of enumerators in C#..that explains the Enumerators in C# very precisely.... Thanks, Nitin
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...
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...
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...
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();...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.