将结构放置在类中 [英] Place structures in a class

查看:71
本文介绍了将结构放置在类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字段数未知且分隔符类型未知的文本文件读入结构并在类中进行编辑?
文件示例:
文本文件的名称未知,可能包含一个到无限个字段.
1.汤姆,自行车
2.汽车",建筑物",女士"
3.办公桌< tab>椅子< tab>男人< tab>吃
4.对于瓦特·约翰·水而言,
5.黄色砖路很长

我可以创建一个结构,但我知道字段数和定界符.可以将它放在类中吗?

How to read a text file with unknown number of fields and unknown type of delimiters into a structure and do it in a class?
Examples of files:
The text file has an unknown name and could have from one to unlimited fields.
1. tom,bicycle
2. "Car""building""lady"
3. desk <tab> chair <tab> man <tab> eat
4. for watt John water noway
5. The-Yellow-Brick-road-is-long

I can create a structure but I know the number of fields and the delimiter. Can this be put into a class?

推荐答案

可以在运行时创建新属性,因此可以为每个字段列表创建一个专门的类(如果尚未创建)创建.
http://msdn.microsoft.com/en-us/library/h1zby21a.aspx [ ^ ]

可能存在一个问题,可能使其变得毫无用处.我的意思是,您在运行时使用编译时不知道的属性创建类定义.这意味着您实际上很难使用它.

更好的解决方案可能是具有索引属性的通用类,该属性用于按索引读取每个字段,而count属性用于获取可用的字段数.此类由读取文件的类使用,并且该类还具有用于按索引读取每一行的indexed属性(该属性返回先前描述的通用类的对象),还具有count属性以获取行数.有关索引属性的更多信息,请参见链接.

http://www.vb-helper.com/howto_net_indexed_property.html [
It is possible to create new properties at runtime and therefor create a specialized class for each list of fields if it was not yet created.
http://msdn.microsoft.com/en-us/library/h1zby21a.aspx[^]

There could be a problem that might make it somewhat useless. What I mean is that you create a class definition at runtime with properties that weren''t known at compile time. This means that you actually make it quite hard on yourself to use it.

A better solution would probably be a generic class with an index property for reading each field by index and a count property to get the number of fields available. This class is used by a class that reads the file and also has an indexed property for reading each line by index (which returns an object of the previous described generic class) and also has a count property to get the number of lines. See the link for more info about an indexed properties.

http://www.vb-helper.com/howto_net_indexed_property.html[^]


Good luck!


这篇关于将结构放置在类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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