类中的对象数组 [英] array of objects inside a class

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

问题描述

如何在类中创建对象数组?当我按照以下方式执行此操作时,我收到一条错误消息:''对象引用未设置为对象'

实例'。


公共类犯人

私有strName为字符串

私有strSSN为字符串

私有strDOB为字符串

私人myMugshotPicture()如MugshotPicture

结束班级


公共类MugshotPicture

私人strPictureName字符串

私人strDateTaken为字符串

结束类

How would I create an array of objects inside a class? When I do it the
following way I get an error that says: ''Object reference not set to an
instance of an object''.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class

推荐答案

我的VB.NET非常生疏,但我认为你的问题是一个数组实际上是一个对象本身就是一个对象本身,你需要创建一个它的实例

(可能在Inmate的构造函数中)访问它。
My VB.NET is pretty rusty, but I think your problem is that an Array is
actually an object itself, and you''ll need to create an instance of it
(probably in the contructor for Inmate) before accessing it.
如何在类中创建一个对象数组?当我按照以下方式执行此操作时,我收到一条错误消息:''对象引用未设置为对象的实例'。

Public Class Inmate
私有strName为字符串
私有strSSN为字符串
私有strDOB为字符串
私有myMugshotPicture()如MugshotPicture
结束类

Public Class MugshotPicture
私有strPictureName为字符串
私有strDateTaken为字符串
结束类
How would I create an array of objects inside a class? When I do it the
following way I get an error that says: ''Object reference not set to an
instance of an object''.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class








CLEAR-RCIC < CL ******* @ discussions.microsoft.com>在消息中写道

新闻:2D ********************************** @ microsof t.com ...

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
如何在类中创建对象数组?当我按照以下方式执行此操作时,我收到一条错误消息:''对象引用未设置为对象的实例'。

Public Class Inmate
私有strName为字符串
私有strSSN为字符串
私有strDOB为字符串
私有myMugshotPicture()如MugshotPicture
结束类

Public Class MugshotPicture
私有strPictureName为字符串
私有strDateTaken为字符串
结束类
How would I create an array of objects inside a class? When I do it the
following way I get an error that says: ''Object reference not set to an
instance of an object''.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class




将您提供的代码放在类库项目中并编译这个

不会产生任何错误,我猜你在尝试从其他代码中引用myMugShotPicture时会发现错误吗?

在尝试时,你是否用实际的

MugshotPicture对象变量填充了数组?如果不是,那就是你的问题。


-

Peter [MVP视觉开发者]

所有行业的杰克没有人。



As putting the code you provide in a class library project and compiling it
produces no errors, I''ll guess you''re getting the error when attempting to
reference myMugShotPicture from other code?
At the time of the attempt, have you populated the array with actual
MugshotPicture object variables? If not, that''s your problem.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.


这就是我的想法。我正在谷歌搜索它,但有人知道

他们头顶的语法。感谢Cam的帮助!


" Cam"写道:
That''s what I was thinking. I''m googling it right now but does anyone know
that syntax of the top of their head. Thanks for the help Cam!

"Cam" wrote:
我的VB.NET非常生疏,但我认为你的问题是数组实际上是一个对象本身,你需要创建一个它的实例
(可能在Inmate的构造函数中)访问它之前。
My VB.NET is pretty rusty, but I think your problem is that an Array is
actually an object itself, and you''ll need to create an instance of it
(probably in the contructor for Inmate) before accessing it.
如何在类中创建一个对象数组?当我按照以下方式执行此操作时,我收到一条错误消息:''对象引用未设置为对象的实例'。

Public Class Inmate
私有strName为字符串
私有strSSN为字符串
私有strDOB为字符串
私有myMugshotPicture()如MugshotPicture
结束类

Public Class MugshotPicture
私有strPictureName为字符串
私有strDateTaken为字符串
结束类
How would I create an array of objects inside a class? When I do it the
following way I get an error that says: ''Object reference not set to an
instance of an object''.

Public Class Inmate
Private strName As String
Private strSSN As String
Private strDOB As String
Private myMugshotPicture() As MugshotPicture
End Class

Public Class MugshotPicture
Private strPictureName As String
Private strDateTaken As String
End Class



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

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