如何创建对象数组Visual Basic [英] How to create an array of objects Visual Basic

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

问题描述

是否可以在Visual Basic中创建对象数组?

Is it possible to create an array of objects in visual basic?

我正在制作一个战斗系统,每当战斗开始时,我都可以随机从数组中选择一个怪物对象。

I'm making a battle system and whenever the battle begins, i wanna be able to randomly select a Monster object from an array.

如果可能的话,有人可以告诉我如何将公共蜘蛛存储为新怪物(50、20) ,5)放入数组吗?

If it is possible, could somebody show me how to store Public Spider as New Monster(50, 20, 5) into an array?

谢谢。

怪物等级:

Public Class Monster

  Private hp As Integer
  Private xp As Integer
  Private dmg As Integer

  Sub New(ByVal hitpoints As Integer, ByVal exp As Integer, ByVal damage As Integer)
    hp = hitpoints
    xp = exp
    dmg = damage
  End Sub

End Class

表格类:

Imports Monster
Public Class Form

  Public Spider As New Monster(50, 20, 5)

End Class


推荐答案

A 列表(T)对此非常有用。

Private Monsters As New List(Of Monster)
'later add them into this collection
Monsters.Add(New Monster(50, 20, 5))

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

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