用变量命名结构 [英] Naming structs with a variable

查看:59
本文介绍了用变量命名结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和结构新手所以这可能很容易或者只是没有

可能。

我有一个定义为Branch的结构

如果我使用Branch myBranch = new Branch(i); //一切正常

如果我使用Branch(myBranch + x)= new Branch(i); //它没有


x是一个循环迭代器,我是构造函数的int来定义一个

数组。


我在这里做错了什么。

I am new to C# and to structs so this could be easy or just not
possible.
I have a struct defined called Branch
If I use Branch myBranch = new Branch(i); // everything works
If I use Branch (myBranch + x) = new Branch(i); // it doesn''t

x is a loop iterator, i is an int for the constructor to define an
array.

What am I doing wrong here.

推荐答案

首先,让Branch成为一个类,而不是一个struct 。


其次,我不确定你在第二行

行中究竟要做什么。如果你想初始化一个Branch''es数组,那么你需要这样的东西:


int branchArraySize = 20;

Branch [] branchArray = new Branch [branchArraySize];

for(int i = 0; i< branchArray.Length; i ++)

{

branchArray [i] = new Branch(i);

}


第二行声明并创建一个引用数组可能

指向Branch''es,但数组为空:每个条目都为空,所以

还没有存储实际的分支。


循环内的行为数组中的每个条目创建一个新的Branch对象,并指定数组条目以引用新的分支

刚刚创建。


这就是你想做的事情吗?

First of all, make Branch a class, not a struct.

Second, I''m not sure exactly what you''re trying to do in the second
line. If you''re trying to initialize an array of Branch''es, then you
need something like this:

int branchArraySize = 20;
Branch[] branchArray = new Branch[branchArraySize];
for (int i = 0; i < branchArray.Length; i++)
{
branchArray[i] = new Branch(i);
}

The second line declares and creates an array of references that could
point to Branch''es, but the array is empty: each entry is null, so
there are no actual branches stored in it yet.

The line inside the loop creates a new Branch object for each entry in
the array and assigns the array entry to refer to that new Branch that
was just created.

Is that sort of what you wanted to do?


谢谢你回复。

我所拥有的是一堆逗号分隔的文本文件。他们

有很多分支各种长度。我不知道有多少分支

或每个分支有多长,因为它们都不同。我是什么

试图结束的是

文件中每个分支的分支实例。我对branch的定义包含一个数组(array(length,3)),它将
设置为分支的长度。这样的事情:


Branch1,长度

item1,item2,item3

item1,item2,item3

item1,item2,item3

Branch2,长度

item1,item2,item3

item1,item2,item3

Branch3,长度

item1,item2,item3

item1,item2,item3

item1,item2,item3

item1,item2,item3

item1,item2,item3

依旧...


当我读到分支的开始我创建了一个分支实例。然后我

用该行的信息填充该实例中的数组。所以我

在我所有的无限智慧中想到我每次开始时都可以创建一个新的分支

结构并使用我的迭代器来命名

struct。因此名称(MyBranch + x)。然后我可以随机处理

中的数据。

我选择使用结构,因为我只需要存储每个

分支的信息而且我不需要任何类的继承属性。但是,如果我需要使用一个班级,或者我会更好,那只是我读书的新手选择中的一个。

Thanks for the reply.
What I have is a bunch of comma delimited text files for a source. They
have many "Branches" of various lengths. I don''t know how many branches
or how long each branch is because they are all different. What I am
trying to end up with is an instance of a Branch for each branch in the
file. My definition of branch contains an array (array(length,3)) that
gets set to the length of the branch. Something like this:

Branch1, length
item1, item2, item3
item1, item2, item3
item1, item2, item3
Branch2, length
item1, item2, item3
item1, item2, item3
Branch3, length
item1, item2, item3
item1, item2, item3
item1, item2, item3
item1, item2, item3
item1, item2, item3
And so on ...

When I read the start of a branch I create a branch instance. Then I
fill the array in that instance with the info for each line. So I
figured in all my infinite wisdom that I could create a new Branch
struct every time I hit the start and use my iterator to name that
struct. Hence the name (MyBranch + x). Then I could work on the data in
each randomly.
I chose to use structs because I only need to store information of each
branch and I don''t need any inheritance properties of a class. But if I
need to use a class or that''s better I will, it was just one of those
newbie choices I made reading a book.


" Bruce Wood" <峰; br ******* @ canada.com>写道:
"Bruce Wood" <br*******@canada.com> wrote:
首先,让Branch成为一个类,而不是一个结构。
First of all, make Branch a class, not a struct.




我很好奇 - 你为什么建议那个?


-

Lucian



I''m curious -- why did you advise that?

--
Lucian


这篇关于用变量命名结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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