如何使用数组创建对象 [英] How to create an object with array

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

问题描述

我正在尝试创建一个类似于结构的对象。无法弄清楚如何做到这一点



我想要这样的东西:Buffer(4).Tray(10).Cell(99).SerialNumber =test



缓冲区是以下类别的对象



类托盘



Class Cell





感谢您的指导

解决方案

问题不明确。您可以继续使用对象数组,而不是使用数组语法创建类型。此外,您可以使用对象集合,例如 System.Collections.Generic.List<>

此外,您可以创建 indexed property ,它使用类似数组的语法通过索引实现访问:

https://www.informit.com/guides/content.aspx?g=dotnet&seqNum=149 [ ^ ],

http://support.microsoft.com/kb/311323 [ ^ ]。



-SA


< blockquote>亲爱的谢尔盖:谢谢你的回复。对不起,我的问题不明确。



让我试着更好地解释一下。



我可以轻松地创建一个结构并使用REDIM来创建所需的数组:



这样的东西:



公共类clsBufferClass

公共结构Traydef

Dim ChipSerialNumber()字符串

结束结构

结束类



DIM缓冲区(10)as clsBufferClass.BufferDef



对于intI As Integer = 0到10

Buffer(intI)= new clsBufferClass.BufferDef

'为托盘阵列重新分配存储空间

ReDim Buffer(intI).Tray(10)

对于intJ As Integer = 0到10

ReDim Buffer(intI).Tray(intJ).ChipSerialNumber (99)

下一个intJ

下一个intI





现在,我很容易参考给定的缓冲器(4),托盘(10)和芯片(99)。



缓冲区(4).Tray(10).Cell(99).SerialNumber



我会喜欢用类创建类似的东西。有一个课我可以使用例如INotifyPropertyChanged。



有什么想法吗?谢谢。


I'm trying to create an object similar to a structure. Can't figure out how to do this

I want something like this: Buffer(4).Tray(10).Cell(99).SerialNumber = "test"

where Buffer is an object of the following classes

Class Tray

Class Cell


Thank your for your guidance

解决方案

The question is not clear. You can keep using arrays of objects instead of creating a type with the array syntax. Also, you can use collections of objects, such as System.Collections.Generic.List<>.
Also, you can create indexed property which implements access by index using array-like syntax:
https://www.informit.com/guides/content.aspx?g=dotnet&seqNum=149[^],
http://support.microsoft.com/kb/311323[^].

—SA


Dear Sergey: thanks for the reply. I'm sorry that my questions is not clear.

Let me try to explain better.

I easily can create a structure and use REDIM to create the required array:

Something like this:

Public Class clsBufferClass
Public Structure Traydef
Dim ChipSerialNumber() As String
End Structure
End class

DIM Buffer(10) As clsBufferClass.BufferDef

For intI As Integer = 0 To 10
Buffer(intI) = New clsBufferClass.BufferDef
'Reallocate storage space for tray array
ReDim Buffer(intI).Tray(10)
For intJ As Integer = 0 To 10
'Reallocate storage space for chips serial number
ReDim Buffer(intI).Tray(intJ).ChipSerialNumber(99)
Next intJ
Next intI


Now, I can easily reference to a given buffer (4), tray (10) and chip (99).

Buffer(4).Tray(10).Cell(99).SerialNumber

I would like to create something similar using classes. With a class I could for example use INotifyPropertyChanged.

Any ideas? Thank you.


这篇关于如何使用数组创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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