使用数组作为类成员 [英] Using an array as a Class member

查看:56
本文介绍了使用数组作为类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个字符串数组作为类成员或字段,然后

通过从文本文件中读入来填充此数组,但我找不到

适当的语法。 getter和setter对这个想法非常不满意。

编译器拒绝与我所做的任何尝试一起玩球。

构建这样的结构。使用单个字符串时没有问题,但

二维字符串数组似乎是一个非常不同的问题。


在''老旧经典''C''的日子,我只是通过了一个

指向数组的指针,但是'过去的好时光'已经消失了。我只是觉得这个游戏太老了,或者某种灵魂会告诉我它是如何完成的?

-

Peter Hallett

解决方案

Peter Hallett写道:

我想将字符串数组设置为类成员或字段,然后通过从文本文件中读入来填充此数组,但我找不到相应的语法。 getter和setter对这个想法非常不满意。
编译器拒绝与我为构建这样一个结构所做的任何尝试一起玩。使用单个字符串时没有问题但是二维字符串数组似乎是一个非常不同的问题。

在古典''C'的过去的好时光中'',我只是通过了一个指向阵列的指针,但是'过去的美好时光'已经消失了。我只是觉得这个游戏太老了,还是某种灵魂会告诉我它是如何完成的?



public string [,,] Foo

{

get

{

返回新字符串[10,10,10];

}

}


这将返回一个三维数组(谢谢船长明显的:))。

Single = string []

Double = string [,]


HTH


JB


Peter Hallett< Pe ********** @ discussion.microsoft.com>写道:

我想设置一个字符串数组作为类成员或字段,然后通过从文本文件中读入来填充此数组,但我找不到合适的语法。 getter和setter对这个想法非常不满意。
编译器拒绝与我为构建这样一个结构所做的任何尝试一起玩。使用单个字符串时没有问题但是二维字符串数组似乎是一个非常不同的问题。


我不确定你的二维数组

字符串是什么意思。如果你说一个二维(但是参差不齐)的字符数组,

我能理解,但这不是C#用字符串处理

的正常方法。相反,一个单维的字符串数组更符合正常的价值。


--- 8< ---

class Foo

{

string [] _lines;


public void LoadFromFile(string fileName)

{

// .NET 2.0方式:

_lines = File.ReadAllLines(fileName);


//或者老式方式:

字符串行;

ArrayList lines = new ArrayList();

using(TextReader reader = File.OpenText(fileName) ))

while((line = reader.ReadLine())!= null)

lines.Add(line);

_lines = (string [])lines.ToArray(typeof(string));

}

}

---> 8 ---

在古典''C'的美好时光中,我只是通过了一个指向数组的指针,但是过去的美好时光已经消失了。我只是觉得这个游戏太老了,或者某种灵魂会告诉我它是如何完成的?




在上面的代码中,一个字符串[ ]值(例如_lines)是对堆上数组的引用。数组本身是一个对堆上字符串值的引用的
引用数组,因为string也是一个

引用类型。因此,为了传递数组,一个字面上传递

围绕string []类型的值。


- Barry


-
http://barrkel.blogspot.com/


抱歉,如果我困惑你。我的意思是: -

1维数组=字符串[]

2维数组=字符串[,]

3维数组=字符串[ ,,]

源.txt文件由34对逗号分隔值组成,但

值本身因应用程序而异。第一个工作,然后在运行Main之前运行,因此要读取相应的文件,并将值存储为一个类的成员,例如[0.1,2.2]

[1.3,9.0]

[0.7,3.112]

等,



我希望能让事情变得更加清晰。

同时,谢谢你的帮助。我会研究你的回复,看看

,如果我可以再进行更多的工作。

-

Peter Hallett

巴里凯利写道:

Peter Hallett< Pe ********** @ discussion.microsoft.com>写道:

我想设置一个字符串数组作为类成员或字段,然后通过从文本文件中读入来填充此数组,但我找不到合适的语法。 getter和setter对这个想法非常不满意。
编译器拒绝与我为构建这样一个结构所做的任何尝试一起玩。使用单个字符串时没有问题,但是字符串的二维数组似乎是一个非常不同的问题。



我不确定你究竟是什么意思通过二维数组的
字符串。如果你说一个二维(但是参差不齐)的字符数组,我可以理解,但这不是C#用字符串处理
的正常方法。相反,一个单维的字符串数组更为正常。

--- 8< ---
类Foo
{
string [ ] _lines;

public void LoadFromFile(string fileName)
//
// .NET 2.0方式:
_lines = File.ReadAllLines(fileName);
或者老式的方式:
字符串行;
ArrayList lines = new ArrayList();
使用(TextReader reader = File.OpenText(fileName))
while((line = reader.ReadLine())!= null)
lines.Add(line);
_lines =(string [])lines.ToArray(typeof(string)) ;
}
---
---> 8 ---

在经典''C'的美好时光中'',我只是通过了一个指向阵列的指针,但是'过去的美好时光'已经消失了。我只是觉得这个游戏太老了,或者某种灵魂会告诉我它是如何完成的?



在上面的代码中,一个字符串[]值(即_lines,例如)是对堆上数组的引用。数组本身是对堆上字符串值的引用数组,因为string也是
引用类型。因此,为了传递数组,一个字面上传递
围绕string []类型的值。

- Barry

-
http://barrkel.blogspot.com/



I would like to set up a string array as a class member, or field, and then
populate this array by reading in from a text file, but I cannot find the
appropriate syntax. The getter and setter are very unhappy with the idea and
the compiler refuses to play ball with any of the attempts I have made to
build such a structure. There is no problem when using a single string but a
two dimensional array of strings appears to be a very different matter.

In the ''good old days'' of classical ''C'', I would simply have passed a
pointer to the array but the ''good old days'' have gone. Am I simply getting
too old for this game, or will some kind soul show me how it is done?
--
Peter Hallett

解决方案

Peter Hallett wrote:

I would like to set up a string array as a class member, or field, and then
populate this array by reading in from a text file, but I cannot find the
appropriate syntax. The getter and setter are very unhappy with the idea and
the compiler refuses to play ball with any of the attempts I have made to
build such a structure. There is no problem when using a single string but a
two dimensional array of strings appears to be a very different matter.

In the ''good old days'' of classical ''C'', I would simply have passed a
pointer to the array but the ''good old days'' have gone. Am I simply getting
too old for this game, or will some kind soul show me how it is done?


public string[,,] Foo
{
get
{
return new string[10,10,10];
}
}

This will return a three dimensional array (thank you captain obvious :)).
Single = string[]
Double = string[,]

HTH

JB


Peter Hallett <Pe**********@discussions.microsoft.com> wrote:

I would like to set up a string array as a class member, or field, and then
populate this array by reading in from a text file, but I cannot find the
appropriate syntax. The getter and setter are very unhappy with the idea and
the compiler refuses to play ball with any of the attempts I have made to
build such a structure. There is no problem when using a single string but a
two dimensional array of strings appears to be a very different matter.
I''m not sure exactly what you mean by a two-dimensional array of
strings. If you said a two-dimensional (but jagged) array of characters,
I could understand, but that isn''t the normal approach in C# for dealing
with strings. Instead, a single-dimensioned array of strings is more
normal.

---8<---
class Foo
{
string[] _lines;

public void LoadFromFile(string fileName)
{
// The .NET 2.0 way:
_lines = File.ReadAllLines(fileName);

// or the old-fashioned way:
string line;
ArrayList lines = new ArrayList();
using (TextReader reader = File.OpenText(fileName))
while ((line = reader.ReadLine()) != null)
lines.Add(line);
_lines = (string[]) lines.ToArray(typeof(string));
}
}
--->8---
In the ''good old days'' of classical ''C'', I would simply have passed a
pointer to the array but the ''good old days'' have gone. Am I simply getting
too old for this game, or will some kind soul show me how it is done?



In the above code, a string[] value (i.e. _lines, for example) is a
reference to the array on the heap. The array itself is an array of
references to the string values on the heap, since string is also a
reference type. So, to pass around the array, one literally passes
around values of type string[].

-- Barry

--
http://barrkel.blogspot.com/


Sorry if I confused you. I simply meant:-
1 dimensional array = string[]
2 dimensional array = string[,]
3 dimensional array = string [,,]
The source .txt file consists of 34 pairs of comma delimited values but the
values themselves vary according to the application. The first job, before
running Main is thus to read in the appropriate file and store the values as
members of a Class in the form eg [0.1, 2.2]
[1.3, 9.0]
[0.7, 3.112]
etc.,
etc.
I hope that makes things a bit clearer.
In the mean time, thank you for your help. I will study your reply and see
if I can make any more progess.
--
Peter Hallett
"Barry Kelly" wrote:

Peter Hallett <Pe**********@discussions.microsoft.com> wrote:

I would like to set up a string array as a class member, or field, and then
populate this array by reading in from a text file, but I cannot find the
appropriate syntax. The getter and setter are very unhappy with the idea and
the compiler refuses to play ball with any of the attempts I have made to
build such a structure. There is no problem when using a single string but a
two dimensional array of strings appears to be a very different matter.



I''m not sure exactly what you mean by a two-dimensional array of
strings. If you said a two-dimensional (but jagged) array of characters,
I could understand, but that isn''t the normal approach in C# for dealing
with strings. Instead, a single-dimensioned array of strings is more
normal.

---8<---
class Foo
{
string[] _lines;

public void LoadFromFile(string fileName)
{
// The .NET 2.0 way:
_lines = File.ReadAllLines(fileName);

// or the old-fashioned way:
string line;
ArrayList lines = new ArrayList();
using (TextReader reader = File.OpenText(fileName))
while ((line = reader.ReadLine()) != null)
lines.Add(line);
_lines = (string[]) lines.ToArray(typeof(string));
}
}
--->8---

In the ''good old days'' of classical ''C'', I would simply have passed a
pointer to the array but the ''good old days'' have gone. Am I simply getting
too old for this game, or will some kind soul show me how it is done?



In the above code, a string[] value (i.e. _lines, for example) is a
reference to the array on the heap. The array itself is an array of
references to the string values on the heap, since string is also a
reference type. So, to pass around the array, one literally passes
around values of type string[].

-- Barry

--
http://barrkel.blogspot.com/



这篇关于使用数组作为类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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