修改C#中的结构 [英] Modify a structure in C#

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

问题描述

大家好,



在VB Net中我曾经使用'Redim'语句来声明一个多维数组,当我知道它需要多少元素时。现在我正在使用C#,我正在使用List< t>控制类型Struct以分配相同的结构;我在这里找不到如何调整多维数组的大小,并且我的程序用固定大小声明它是不方便的。



现在,我必须添加数值到结构中的几个字段,但应用程序向我发送了一条错误消息它不可能更改返回值,因为它不是变量或类似的东西(它是西班牙语)。 br />


这样做是否可行?或者您认为使用数据表应该更好吗?



如果我想解决这个问题,从结构类型构建我的列表是否正确?



我尝试过:



我想我要更改List< struct>控制DataTable,它应该做我期望的事情(更新colums dinamycally),但我不想怀疑。

Hi Everybody,

In VB Net I used to use ´Redim´statement to declare a multidimentional array, when I did know how many elements it needed. Now I´m working with C# and I am using a List<t> control of type Struct to allocate the same structure; I couldn´t find how to resize a multidimentional array here and it is not convenient to my program to declare it with a fixed size.

Now, I have to add numeric values to a couple of the fields in the structure, but the app sends me a error message "it is not posible to change the return value, because is not a variable" or something like that (it´s in spanish).

Is it posible to do this ?, or do you think it should be better to work with a Data Table ?

Is it correct to built my List from a Struct Type, if I want to acieve this ?

What I have tried:

I think I am going to change the List<struct> control for a DataTable, and it should do what I expect (update colums dinamycally), but I don´t want to stay in doubt.

推荐答案

C#中的数组VB.NET是不可变的。创建后,无法更改。 ReDim在VB.NET中所做的就是愚弄你认为它就是这么做的。它欺骗了你。它真正做的是用新维度创建一个全新的数组,然后将旧数组中的内容复制到新数组,然后杀死旧数组。



列表< t>没有这个限制,但它确实在幕后做了类似的事情。列表< yourstructtype>比DataTable要好得多。 DataTables更重,更慢,用于表示数据库表,而不是对象列表。



如果没有看到您编写的代码涉及此问题,那么不可能说出你做错了什么。
Arrays in C# AND VB.NET are immutable. Once created they cannot be changed. What ReDim does in VB.NET is fool you into thinking it did just that. It lied to you. What it really did was create an entirely new array with the new dimension and then copied the content from the old array to the new one, then killed the old array.

A List<t> doesn't have that limitation, but it does do something similar behind the scenes. A List<yourstructtype> is far better than a DataTable. DataTables are much heavier, slower, and used to represent database tables, not a list of objects.

Without seeing the code you've written that's involved in this problem it's impossible to say what you're doing wrong.


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

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