singleton ienumerable,需要添加更多... [英] singleton ienumerable, need to add more...

查看:50
本文介绍了singleton ienumerable,需要添加更多...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个可以从数据库中收集数据的单件ienumerable。我有

列出下面的代码。它有通常的电流,移动和重置方法。


我需要去某个位置或设置过滤器或许多其他东西我

don我不知道该怎么做。我刚刚学会了单身和无数,所以

奇迹般地让这个到目前为止工作。我可以用一些帮助来进一步使用它来获得
。如果需要,我可能需要更改类型以实现

目标。


我接下来要做的是通过传递选择特定行在索引中。我不知道如何做到这一点,无论我尝试什么,我都不能...... / b
编译。

我的来源如下......


所有帮助表示赞赏。

公共类文件夹:System.Collections.IEnumerable,
System.Collections.IEnumerator

{

private static Folders mObj; //这用于管理单例。

私有DataTable DT;


System.Collections.IEnumerator ienum;


//这是单身构造函数

私人文件夹()

{

InitData();

}


//设置单例。

公共静态文件夹SiteFolders

{

get

{

if(mObj == null)//只创建一个实例

mObj = new文件夹();


返回mObj;

}

}


private void InitData()

{


//从数据库中获取数据并传入ienum

ienum = DT.Rows.GetEnumerator();


}


public System.Collections.IEnumerator GetEnumerator()

{


//将此对象变形为IEnumerator接口

return(System.Collections.IEnumerator)this;


}


公共voi d重置()

{

ienum.Reset();

}


公共布尔MoveNext()

{

返回ienum.MoveNext();

}


公共对象当前

{

get

{

返回新文件夹((DataRow)ienum.Current);

}

}

#endregion


公共对象SelectFolder(int myRow)

{

//获取

// {

返回新文件夹((DataRow)myRow);

//}

}


}

我也试过使用方括号作为选择文件夹,但只是

不会编译。如何选择特定行?顺便提一下,选择文件夹引用的文件夹

只需要我的数据表并将其传递给

一些属性。


因为我有刚刚了解了单身和无数,我真的这么做了。不知道怎么继续。 : - (


祝你好运,

Dave Colliver。
http://www.AshfieldFOCUS.com

~~
http://www.FOCUSPortals.com - 提供本地特许经营权

解决方案

好的,一次一件事。


我认为您正在寻找的索引器语法是:


public这个[ int rowIndex]

{

get

{

返回新文件夹(DT.Rows [rowIndex]);

}

}


然后你就可以访问Folders.SiteFolders [4];(等等)


请注意,我调整了返回类型是有用的。但是,更大的

积分:


*这是不寻常的可枚举和枚举;我

怀疑你只是意味着IEnumerable或IEnumerable<文件夹>

* T hread安全性:因为你看来是C#的新手,这可能不会是一个

问题,但一般情况下静态方法应该是线程安全的,这是

SiteFolders isn'' t $ / b $ b *可以放弃获得的普查员,而不是

smoke''n''mirrors?

*每次调用Current返回一个不同的对象;这可能会变得非常,非常令人困惑,并导致各种乐趣

*次要评论问题:这只是一个演员,而不是多态?

*不确定这是否真的适合单身人士模式 - 虽然这可能只是因为我对背景知之甚少,所以我会给予

你怀疑的好处


鉴于你似乎有一个包装器对象(文件夹),可能会有很多好处吗?移动到List< Folderimplementation;这将是

允许你在没有大惊小怪的情况下移交一个简单的枚举器,并且

你的Folder对象只会存在一次 - 你只需要循环遍历

DataTable。

如果您需要更多帮助,请告诉我。


Marc


谢谢Marc,


我在网上找到的最初的可枚举信息,以及

单身人士。 />

我已经做了很多让他们两个都工作的战斗,


我的背景是经典ASP,已经进入.net几年了以前,

但我的知识仍然存在很大的差距。


我尝试过类似公共文件夹这个[int rowIndex],但保留了

收到错误。但是,我不认为我有这个。字。我会试试。


对于你的积分......

1.我假设这是由该类派生的两个项目。我可以

只是删除IEnumerable它会起作用吗?

2.目前,线程安全不是问题,但很可能是

项目进展。我应该如何写它才能安全?

3.(通过已故的调查员)你能给我一些指示吗?代码

示例或URL?

4.再次,这只是被复制了。据我所知,一个不可数项必须有3个项目中的最少
。 reset,movenext和current。我应该在这做什么?

5. polymorph ...这是一个直接副本。虽然没有涵盖多态,但我不能真的质疑这个,虽然我可以非常清楚地看到(现在你已经提到它了)它是一个演员。 />
6.我在aspnet中有一个24消息线程试图实现这一点。我想要的是一个全球的东西。枚举的数据表。我希望能够只需简单地分配一个变量而不需要新的运营商。一个例子......


我想从数据库中加载文件夹。我不希望每次都要进入数据库,因为数据库调用很昂贵。


我想要所有的控件,Page,UserControls和自定义控件能够

来调用相同的数据实例。拥有新字样运算符每次都会实例化数据
,从而每次都调用数据库。如果您熟悉Microsoft CMS,我想做类似的事情......


频道MyChannels = CmsHttpContext.Current.Channel.Channels;


foeach(MyChannels中的频道ch)

{

Response.Write ch.DisplayName;

}


WantedChannel = MyChannels.Channel.GetByGuid(guid-string);


注意,不要新。


谢谢。


问候,

Dave Colliver。
http://www.AshfieldFOCUS.com

~~
http://www.FOCUSPortals.com - 门户网站特许经营权

Marc Gravell ;写道:


好​​的,一次一件事。


我认为索引器你正在寻找的语法是:


公共文件夹这个[int rowIndex]

{

get

{

返回新文件夹(DT.Rows [rowIndex]);

}

}


然后你可以访问Folders.SiteFolders [4]; (等)


请注意,我调整了返回类型是有用的。然而,更大的

积分:


*对于可枚举的东西和一个枚举器来说是不寻常的;我怀疑你只是意味着IEnumerable或IEnumerable<文件夹>

*线程安全:因为你看起来很新C#这可能不会是一个

问题,但一般情况下静态方法应该是线程安全的,

SiteFolders不是
*可以直接传递获得的枚举数,而不是< br $> b $ b smoke''n''mirrors?

*每次调用Current都会返回一个不同的对象;这可能会变得非常,非常令人困惑,并导致各种乐趣

*次要评论问题:这只是一个演员,而不是多态?

*不确定这是否真的适合单身人士模式 - 虽然这可能只是因为我对背景知之甚少,所以我会给予

你怀疑的好处


鉴于你似乎有一个包装器对象(文件夹),可能会有很多好处吗?移动到List< Folderimplementation;这将是

允许你在没有大惊小怪的情况下移交一个简单的枚举器,并且

你的Folder对象只会存在一次 - 你只需要循环遍历

DataTable。


如果您需要更多帮助,请告诉我们。


Marc


谢谢Marc,


我在网上找到的最初的可枚举信息,以及

singleton。


我为了让他们两个都工作而进行了相当大的战斗,


我的背景是经典的ASP,有几年前进入.net,

但我的知识仍然存在很大的差距。


我尝试过类似于公共文件夹的东西[此int rowIndex]但保持

得到错误。但是,我不认为我有这个。字。我会试试。


对于你的积分......

1.我假设这是由该类派生的两个项目。我可以

只是删除IEnumerable它会起作用吗?

2.目前,线程安全不是问题,但很可能是

项目进展。我应该如何写它才能安全?

3.(通过已故的调查员)你能给我一些指示吗?代码

示例或URL?

4.再次,这只是被复制了。据我所知,一个不可数项必须有3个项目中的最少
。 reset,movenext和current。我应该在这做什么?

5. polymorph ...这是一个直接副本。虽然没有涵盖多态,但我不能真的质疑这个,虽然我可以非常清楚地看到(现在你已经提到它了)它是一个演员。 />
6.我在aspnet中有一个24消息线程试图实现这一点。我想要的是一个全球的东西。枚举的数据表。我希望能够只需简单地分配一个变量而不需要新的运营商。一个例子......


我想从数据库中加载文件夹。我不希望每次都要进入数据库,因为数据库调用很昂贵。


我想要所有的控件,Page,UserControls和自定义控件能够

来调用相同的数据实例。拥有新字样运算符每次都会实例化数据
,从而每次都调用数据库。如果您熟悉Microsoft CMS,我想做类似的事情......


频道MyChannels = CmsHttpContext.Current.Channel.Channels;


foeach(MyChannels中的频道ch)

{

Response.Write ch.DisplayName;

}


WantedChannel = MyChannels.Channel.GetByGuid(guid-string);


注意,不要新。


谢谢。


问候,

Dave Colliver。
http://www.AshfieldFOCUS.com

~~
http://www.FOCUSPortals.com - 门户网站特许经营权

Marc Gravell ;写道:


好​​的,一次一件事。


我认为索引器你正在寻找的语法是:


公共文件夹这个[int rowIndex]

{

get

{

返回新文件夹(DT.Rows [rowIndex]);

}

}


然后你可以访问Folders.SiteFolders [4]; (等)


请注意,我调整了返回类型是有用的。然而,更大的

积分:


*对于可枚举的东西和一个枚举器来说是不寻常的;我怀疑你只是意味着IEnumerable或IEnumerable<文件夹>

*线程安全:因为你看起来很新C#这可能不会是一个

问题,但一般情况下静态方法应该是线程安全的,

SiteFolders不是
*可以直接传递获得的枚举数,而不是< br $> b $ b smoke''n''mirrors?

*每次调用Current都会返回一个不同的对象;这可能会变得非常,非常令人困惑,并导致各种乐趣

*次要评论问题:这只是一个演员,而不是多态?

*不确定这是否真的适合单身人士模式 - 虽然这可能只是因为我对背景知之甚少,所以我会给予

你怀疑的好处


鉴于你似乎有一个包装器对象(文件夹),可能会有很多好处吗?移动到List< Folderimplementation;这将是

允许你在没有大惊小怪的情况下移交一个简单的枚举器,并且

你的Folder对象只会存在一次 - 你只需要循环遍历

DataTable。


如果您需要更多帮助,请告诉我们。


Marc


Hi all,

I have a singleton ienumerable that collects data from a database. I have
listed the code below. It has the usual methods of current, move and reset.

I need to go to a certain position or set filters or many other things and I
don''t know how to do this. I have just learned singleton AND ienumerable, so
miraculously got this to work so far. I could do with a bit of help to go
further with it. If need by, I may have to change the type to achieve the
aim.

What I want to do next is to select a specific row by passing in an index. I
don''t know how to do this at all and no matter what I try, I just can''t
compile.

My source is below...

All help is appreciated.
public class Folders : System.Collections.IEnumerable,
System.Collections.IEnumerator
{
private static Folders mObj; // This is used to manage the singleton.
private DataTable DT;

System.Collections.IEnumerator ienum;

// This is the singleton constructor
private Folders()
{
InitData();
}

// This sets up the singleton.
public static Folders SiteFolders
{
get
{
if(mObj == null) // only one instance is created
mObj = new Folders();

return mObj;
}
}

private void InitData()
{

// Get my data from the database and pass into ienum
ienum = DT.Rows.GetEnumerator();

}

public System.Collections.IEnumerator GetEnumerator()
{

// Polymorph this object into an IEnumerator interface
return (System.Collections.IEnumerator)this;

}

public void Reset()
{
ienum.Reset();
}

public bool MoveNext()
{
return ienum.MoveNext();
}

public object Current
{
get
{
return new Folder((DataRow)ienum.Current);
}
}
#endregion

public object SelectFolder(int myRow)
{
// get
// {
return new Folder((DataRow)myRow);
// }
}

}
I have tried using square brackets for the selectfolder as well, but just
won''t compile. How can I select a specific row? Incidentally, the Folder
that selectfolder is referencing just takes my datatable and passes it into
some properties.

Because I have just learned about the singleton and ienumerable, I really do
not know how to continue. :-(

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

解决方案

OK, one thing at a time.

I think the "indexer" syntax you are looking for is:

public Folder this[int rowIndex]
{
get
{
return new Folder(DT.Rows[rowIndex]);
}
}

you can then access Folders.SiteFolders[4]; (etc)

Note that I tweaked the return type to be useful. However, the bigger
points:

* it is unusual for something to be enumerable and an enumerator; I
suspect you just mean IEnumerable or IEnumerable<Folder>
* Thread safety: since you appear new to C# this probably won''t be an
issue, but in general static methods should be thread safe, which
SiteFolders isn''t
* could just pass up the obtained enumerator, rather than the
smoke''n''mirrors?
* each call to Current returns a different object; this could get very,
very confusing, and lead to all sorts of fun
* minor comment issue: this is merely a cast, not polymorphism
* not sure this is really suited to a singleton pattern - although that
might just be that I don''t know enough about the context, so I''ll give
you the benefit of the doubt

Given that you seem to have a wrapper object (Folder), there may be a
lot of benefit in moving to a List<Folderimplementation; this would
allow you to hand over a simple enumerator without all the fuss, and
your Folder objects would only exist once each- you''d just loop over
the DataTable.
Let me know if you want more help,

Marc


Thank you Marc,

The initial enumerable info I found on the net somewhere, as also the
singleton.

I have had quite a battle to get them both to work,

My background is classic ASP, having moved into .net a couple of years ago,
but still having large gaps in my knowledge.

I did try something similar to the public Folder this[int rowIndex] but kept
getting errors. However, I don''t think I had the "this" word. I will try that.

For your points...
1. I assume this is with the two items that the class is derived from. Can I
just remove the IEnumerable and it will work?
2. At the moment, thread safety is not an issue, but is likely to be as the
project progresses. How should I write it to be safe?
3. (Pass the obtoned enumerator) Can you give me some pointers? code
examples or URLs?
4. Again, this was just copied. I understand that an ienumerable has to have
a min of 3 items. reset, movenext and current. What should I be doing here?
5. polymorph... This is a direct copy. Having not covered polymorphing, I
couldn''t really question this, though I can quite clearly see (now you
mention it) that it is a cast.
6. I have had a 24 message thread in aspnet trying to acheive this. What I
am wanting is a "global" datatable that is enumerated. I want to be able to
just simply assign a variable without having the "new" operator. An example...

I wanted to have the folders loaded from the database. I DID NOT want to
have to go to the database every time, as a DB call is expensive.

I wanted all my controls, Page, UserControls and Custom Controls to be able
to call the same instance of data. Having a "new" operator would instantiate
the data each time, thereby calling the database each time. If you are
familiar with Microsoft CMS, I want to do something like...

Channels MyChannels = CmsHttpContext.Current.Channel.Channels;

foeach (Channel ch in MyChannels)
{
Response.Write ch.DisplayName;
}

WantedChannel = MyChannels.Channel.GetByGuid("guid-string");

Notice, no "new".

Thank you.

Regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
"Marc Gravell" wrote:

OK, one thing at a time.

I think the "indexer" syntax you are looking for is:

public Folder this[int rowIndex]
{
get
{
return new Folder(DT.Rows[rowIndex]);
}
}

you can then access Folders.SiteFolders[4]; (etc)

Note that I tweaked the return type to be useful. However, the bigger
points:

* it is unusual for something to be enumerable and an enumerator; I
suspect you just mean IEnumerable or IEnumerable<Folder>
* Thread safety: since you appear new to C# this probably won''t be an
issue, but in general static methods should be thread safe, which
SiteFolders isn''t
* could just pass up the obtained enumerator, rather than the
smoke''n''mirrors?
* each call to Current returns a different object; this could get very,
very confusing, and lead to all sorts of fun
* minor comment issue: this is merely a cast, not polymorphism
* not sure this is really suited to a singleton pattern - although that
might just be that I don''t know enough about the context, so I''ll give
you the benefit of the doubt

Given that you seem to have a wrapper object (Folder), there may be a
lot of benefit in moving to a List<Folderimplementation; this would
allow you to hand over a simple enumerator without all the fuss, and
your Folder objects would only exist once each- you''d just loop over
the DataTable.
Let me know if you want more help,

Marc


Thank you Marc,

The initial enumerable info I found on the net somewhere, as also the
singleton.

I have had quite a battle to get them both to work,

My background is classic ASP, having moved into .net a couple of years ago,
but still having large gaps in my knowledge.

I did try something similar to the public Folder this[int rowIndex] but kept
getting errors. However, I don''t think I had the "this" word. I will try that.

For your points...
1. I assume this is with the two items that the class is derived from. Can I
just remove the IEnumerable and it will work?
2. At the moment, thread safety is not an issue, but is likely to be as the
project progresses. How should I write it to be safe?
3. (Pass the obtoned enumerator) Can you give me some pointers? code
examples or URLs?
4. Again, this was just copied. I understand that an ienumerable has to have
a min of 3 items. reset, movenext and current. What should I be doing here?
5. polymorph... This is a direct copy. Having not covered polymorphing, I
couldn''t really question this, though I can quite clearly see (now you
mention it) that it is a cast.
6. I have had a 24 message thread in aspnet trying to acheive this. What I
am wanting is a "global" datatable that is enumerated. I want to be able to
just simply assign a variable without having the "new" operator. An example...

I wanted to have the folders loaded from the database. I DID NOT want to
have to go to the database every time, as a DB call is expensive.

I wanted all my controls, Page, UserControls and Custom Controls to be able
to call the same instance of data. Having a "new" operator would instantiate
the data each time, thereby calling the database each time. If you are
familiar with Microsoft CMS, I want to do something like...

Channels MyChannels = CmsHttpContext.Current.Channel.Channels;

foeach (Channel ch in MyChannels)
{
Response.Write ch.DisplayName;
}

WantedChannel = MyChannels.Channel.GetByGuid("guid-string");

Notice, no "new".

Thank you.

Regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
"Marc Gravell" wrote:

OK, one thing at a time.

I think the "indexer" syntax you are looking for is:

public Folder this[int rowIndex]
{
get
{
return new Folder(DT.Rows[rowIndex]);
}
}

you can then access Folders.SiteFolders[4]; (etc)

Note that I tweaked the return type to be useful. However, the bigger
points:

* it is unusual for something to be enumerable and an enumerator; I
suspect you just mean IEnumerable or IEnumerable<Folder>
* Thread safety: since you appear new to C# this probably won''t be an
issue, but in general static methods should be thread safe, which
SiteFolders isn''t
* could just pass up the obtained enumerator, rather than the
smoke''n''mirrors?
* each call to Current returns a different object; this could get very,
very confusing, and lead to all sorts of fun
* minor comment issue: this is merely a cast, not polymorphism
* not sure this is really suited to a singleton pattern - although that
might just be that I don''t know enough about the context, so I''ll give
you the benefit of the doubt

Given that you seem to have a wrapper object (Folder), there may be a
lot of benefit in moving to a List<Folderimplementation; this would
allow you to hand over a simple enumerator without all the fuss, and
your Folder objects would only exist once each- you''d just loop over
the DataTable.
Let me know if you want more help,

Marc


这篇关于singleton ienumerable,需要添加更多...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆