如何优化我的代码,什么是c#indexers的vb.net等效?
struct Matrix {
readonly int Rows, Columns;
int[,] _Matrix;
public Matrix(int Rows, int Columns) {
this.Rows = Rows;
this.Columns = Columns;
_Matrix = new int[Rows, Columns]
}
public Matrix(int RowsOrColumns) {
this.Rows = RowsOrColumns;
this.Columns = RowsOrColumns;
_Matrix = new int[RowsOrColumns, RowsOrColumns]
}
public int this[int Row, int Column] {
get => _Matrix[Row, Column];
set => _Matrix[Row, Column] = value;
}
}
我在C#中写了矩阵
结构。我试图用VB编写相同的矩阵
结构,但是我可以找到indexer等效的VB(int this [int row,int column]
)。在VB中定义索引器是否可能?如果可能的话,如何在VB中定义索引器?
Structure Matrix
Private ReadOnly Rows, Columns As Integer
Private _Matrix As Integer(,)
Public Sub New(ByVal Rows As Integer, Columns As Integer)
Me.Rows = Rows
Me Columns = Columns
ReDim _Matrix(Rows, Columns)
End Sub
Public Sub New(ByVal RowsOrColumns As Integer)
Me.Rows = RowsOrColumns
Me Columns = RowsOrColumns
ReDim _Matrix(RowsOrColumns, RowsOrColumns)
End Sub
End Structure
还有一个问题!我的目的是指定_matrix
(二维数组)的尺寸(矩阵(byval lows as Integer),列为整数)
> code>> matrix(byval rowsorcolumns作为integer as integer as integer as integer )(构造方法方法)。我使用redim
来完成。有没有更好的方法来做到这一点,还是我做得很好?
struct Matrix {
readonly int Rows, Columns;
int[,] _Matrix;
public Matrix(int Rows, int Columns) {
this.Rows = Rows;
this.Columns = Columns;
_Matrix = new int[Rows, Columns]
}
public Matrix(int RowsOrColumns) {
this.Rows = RowsOrColumns;
this.Columns = RowsOrColumns;
_Matrix = new int[RowsOrColumns, RowsOrColumns]
}
public int this[int Row, int Column] {
get => _Matrix[Row, Column];
set => _Matrix[Row, Column] = value;
}
}
I wrote a Matrix
structure in C#. I was trying to write the same Matrix
structure with VB but I can find VB equivalent of indexer(int this[int Row, int Column]
). Is defining indexer in VB possible? How to define indexer in VB if it's possible?
Structure Matrix
Private ReadOnly Rows, Columns As Integer
Private _Matrix As Integer(,)
Public Sub New(ByVal Rows As Integer, Columns As Integer)
Me.Rows = Rows
Me Columns = Columns
ReDim _Matrix(Rows, Columns)
End Sub
Public Sub New(ByVal RowsOrColumns As Integer)
Me.Rows = RowsOrColumns
Me Columns = RowsOrColumns
ReDim _Matrix(RowsOrColumns, RowsOrColumns)
End Sub
End Structure
One more question! I aim to specify the sizes of _Matrix
(two-dimensional array) in Matrix(ByVal Rows As Integer, Columns As Integer)
or Matrix(ByVal RowsOrColumns As Integer)
(constructor methods). I use ReDim
to do it. Is there a better way to do this or is my way good to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
c#索引器的VB等效属性是默认属性:
另请参见:
The VB equivalent of a C# indexer is a Default Property:
See also: