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?
CodePudding user response:
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 Index(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