如何从vb.net中的对象类继承

发布于 2025-01-22 14:36:29 字数 549 浏览 0 评论 0原文

我从对象类继承,我的类Clsmatrizdb来处理一个数组(,);但是,当我这样做时,它会产生以下内容

public objPubMatriz as clsMatrizDb(,)
objPubMatriz (0,0) = 1

,我会收到一个错误,说您不能从整数转换为clsxarraydb,

我的班级正在继承这样的

Public Class clsMatrizDb
Inherits Object

Private vIntRow As Integer
Private vIntColumn As Integer

Public Sub New()
MyBase.New
Me.vIntRow = 0
Me.vIntColumn = 0
End Sub

Public Sub New(ByVal pvIntRow As Integer, ByVal pvIntColumn As Integer)
Me.vIntRow = pvIntRow
Me.vIntColumn = pvIntColumn
End Sub
End Class

班级?

I inherit from an Object class, my class clsMatrizDb to handle an Array(,); but when I do it generates the following

public objPubMatriz as clsMatrizDb(,)
objPubMatriz (0,0) = 1

I get an error saying that you can't convert from integer to clsXArrayDb

My class is inheriting like this

Public Class clsMatrizDb
Inherits Object

Private vIntRow As Integer
Private vIntColumn As Integer

Public Sub New()
MyBase.New
Me.vIntRow = 0
Me.vIntColumn = 0
End Sub

Public Sub New(ByVal pvIntRow As Integer, ByVal pvIntColumn As Integer)
Me.vIntRow = pvIntRow
Me.vIntColumn = pvIntColumn
End Sub
End Class

I'm missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2025-01-29 14:36:29

我的评论最终太长了,所以这是我的解释者:

我同意清理发表的评论。

  • 无需继承对象。默认情况下,类的实例是对象。
  • 整数值的默认值为0,因此无需在New()sub中明确设置此此功能。如果愿意,请在变量声明中明确设置它。
  • 无需致电mybase.new,因为您实际上不是从基本/抽象类继承的。

您已经表示,错误是从整数转换为clsxarraydb,但您根本没有提及此类。你是说clsmatrizdb吗?

您遇到的错误是因为您正在尝试将整数值分配给clsmatrizdb类型的实例(对象)。

如果要创建一个用行和列值初始化为0,0的Clsmatrizdb的实例,那么您只需要声明:

public objPubMatriz as New clsMatrizDb()

如果要创建具有不同行和列值的Clsmatrizdb的实例(例如2和4),则会声明:

public objPubMatriz as New clsMatrizDb(2,4)

要能够为您的clsmatrizdb对象分配一个整数值,您要么需要默认属性,如tntinmn ...所述,这覆盖了您需要拥有一个行和列值的构造函数。您将代码:

public objPubMatriz as New clsMatrizDb()
objPubMatriz(2,4) = 1

将默认属性声明为:

Default Public Property Item(ByVal pvIntRow As Integer, ByVal pvIntColumn As Integer) As Integer

您将作为本属性实施的一部分设置Vintrow和Vintcolumn的值。

如果要保留参数化的构造函数,则需要在类中添加一个值属性:

public property Value as Integer

使用:

public objPubMatriz as New clsMatrizDb(2,4)
objPubMatriz.Value = 1

My comments ended up too long, so here's my explainer:

I agree with cleaning up comments made.

  • There is no need for Inherits Object. An instance of a class is an Object by default.
  • The default value of an Integer value is 0 so no need to set this explicitly in the New() sub. If you like, set it explicitly in the variable declarations.
  • There is no need to call MyBase.New because you are actually not inheriting from a base/abstract class.

You have stated that the error is converting from integer to clsXArrayDb but you don't mention this class at all. Do you mean clsMatrizDb?

The error you are getting is because you are trying to assign the integer value, 1 to an instance (object) of type clsMatrizDb.

If you want to create an instance of clsMatrizDb with Row and Column values initialised as 0, 0 then you just need to declare:

public objPubMatriz as New clsMatrizDb()

If you want to create an instance of clsMatrizDb with different Row and Column values (for example 2 and 4) then you would declare:

public objPubMatriz as New clsMatrizDb(2,4)

To be able to assign an integer value to your clsMatrizDb object, you will either need a default property as described by TnTinMn... which overrides a need for you to have a constructor that takes the Row and Column values. You would code:

public objPubMatriz as New clsMatrizDb()
objPubMatriz(2,4) = 1

The default property would be declared as:

Default Public Property Item(ByVal pvIntRow As Integer, ByVal pvIntColumn As Integer) As Integer

and you would set the values for vIntRow and vIntColumn as part of this property implementation.

If you want to keep the parameterised constructor then you will need to add a Value property to the class:

public property Value as Integer

With usage:

public objPubMatriz as New clsMatrizDb(2,4)
objPubMatriz.Value = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文