二维数组的C#NET2008问题 [英] C#NET2008 Problem with Two Dimensional Array
问题描述
你好朋友,:laugh:
我再次需要你的帮助.请帮帮我.
我正在使用C#NET2008开发使用二维数组存储字符串文本和INT数据的应用程序
这些是错误消息:
错误#1索引超出了数组的范围.
错误#2无法将类型"int"隐式转换为字符串"
Hello Good Friends, :laugh:
I need your help again. Please help me.
I am using C#NET2008 to develop application using Two Dimension Array to store String text and INT data
These are the error messages:
Error #1 Index was outside the bounds of the array.
Error #2 Cannot implicitly convert type ''int'' to ''string''
private string[ , ] strColmArray = new string[9, 1];
private void FLoadColmArray()
{
strColmArray[0, 0] = "Order ID";
strColmArray[0, 1] = "Ord487"; <--- Error #1
strColmArray[1, 0] = "Product ID";
strColmArray[1,1] = 750.45; <- Error #2
}
推荐答案
strColmArray[0, 1] = "Ord487"; <--- Error #1
您的数组很小,这就是为什么您会遇到第一个错误的原因.
试试
Your array is small, that''s why you get the first error.
Try
private string[ , ] strColmArray = new string[9, 2]
第二个错误-是因为您不能使用字符串数组来存储整数,请尝试:
The second error - is because you can''t use a string array to store integers, try:
strColmArray[1,1] = "750.45"; - Error #2
附带说明一下,请勿将二维数组用于此类操作.
您可以使用数据表或强类型对象列表来执行此类操作.
As a side note, don''t use a two dimensional array for this kind of things.
You can use a DataTable or a List Of Strongly typed objects to do these kind of things.
您的数组太小了!!数字键分别为9和1,在第一个数组维中为您提供9个插槽,在第二个数组维中为您提供 1 插槽.
索引基于零,因此:
Your array is too small! The nubmers are 9 and 1 giving you 9 slots in the first array dimension and 1 slot in the second array dimension.
Indexes are zero based so:
private string[ , ] strColmArray = new string[9, 1];
private void FLoadColmArray()
{
strColmArray[0, 0] = "Order ID";
strColmArray[0, 1] = "Ord487"; //This is the second slot in the second array but you declared only one slot
strColmArray[1, 0] = "Product ID";
strColmArray[1,1] = 750.45; //should be "750.45" as there is no implicit cast from that to a string it will have to be done explicitely or expressed as a string in the first place
}
我认为您需要阅读一些有关数组和从零开始的索引的信息.
最好的问候,
曼弗雷德(Manfred)
I think you need to read some on arrays and zero based indexes.
Best Regards,
Manfred
如果您的IT高级团队负责人已请求阵列,请给他一个阵列.
但是,它不能是字符串数组,因为其中一条数据是整数,除非您每次想读取或存储该数据时都与字符串进行相互转换.
您可以使用类型为Object
的数组
If your IT Senior Team Leader has requested an Array, then give him an Array.
It cannot, however, be a string Array since one of the pieces of data is an integer, unless you convert to and from a string each time you want to read or store that data.
You could use an Array of type Object
private object[ , ] objColmArray = new object[9, 1];
这将允许您存储任何数据类型,但缺点是要利用数据,在读取数据时必须转换/广播数据.
我的首选项(如果必须是数组)将是使用自定义类的一维数组
which will allow you to store any data type but has the disadvantage that to make use of the data you will have to convert/cast the data when reading it.
My preference, if it has to be an Array, would be to use a one dimensional array of a custom class
internal class MyClass
{
public string Name {get; set;}
public int Value {get; set;}
}
private MyClass[] mcColmArray = new MyClass[100];
我真正的偏好是与您的IT高级团队负责人聊天,以找出为什么他们不希望您使用阵列时的最佳选择.
My real preference would be to have a chat with your IT Senior Team Leader to find out why they want you to use an Array when it is not the best option.
这篇关于二维数组的C#NET2008问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!