Use a system class with a different name?

I need a way to keep track of the number of rows and columns in a grid. If I use System.Point, I will always forget whether "x" is the number of rows or columns. So I have the class below.

But I'm wondering if there is a way to use System.Point with a different name language? In other words, I << → want to define a generic "NRows" or "NColumns" method in System.Point. But I want to be able to return an object, which the code will see as an "NRowsColumns" object, but actually compiles to System.Point. When accessing the "NRowsColumns" object, we use the "NRows" and "NColumns" fields instead of "x" and "y". But under the hood, it actually compiles to System.Point.

Ideally, this definition is not limited to a single file.

public class NRowsColumns
{
  public int NRows {get;set;}
  public int NColumns {get;set;}
  public NRowsColumns(int nRows, int nColumns)
  {
    this.NRows = nRows;
    this.NColumns = nColumns;
  }
}

      

+3


source to share


3 answers


No, you cannot "rename" such members. You can refer to System.Point

as NRowsColumns

if you really want, since

using NRowsColumns = System.Point;

      

... but it will still have the same elements as System.Point

.

It would be easier to just implement NRowsColumns

by composing System.Point

though:



public class NRowsColumns
{
    private Point point;

    public int NRows
    {
        get { ... } // Code using point
        set { ... } // Code using point
    }

    ...
}

      

Having said that:

  • I don't see what Point

    really has anything to do with the row and column series. Why not just two integers?
  • I would revise your name here ... the prefix N

    is unconventional. I would probably call it GridSize

    Rows

    and Columns

    - although even that seems unnecessary as a separate type, in general. (Why doesn't your mesh reveal its size itself using the Rows

    and properties Columns

    ?)
+3


source


You can use conversion operators so that your code can use yours NRowsColumns

and Point

interchangeable ones.

Please note that this is not a perfect solution. Creating objects back and forth has consequences that you must investigate.

Add implicit operator

transformations to the existing class:

public class NRowsColumns
{
    public int NRows { get; set; }
    public int NColumns { get; set; }
    public NRowsColumns(int nRows, int nColumns)
    {
        this.NRows = nRows;
        this.NColumns = nColumns;
    }

    public static implicit operator NRowsColumns(Point p)
    {
        return new NRowsColumns(p.X, p.Y);
    }

    public static implicit operator Point(NRowsColumns rowsColumns)
    {
        return new Point(rowsColumns.NRows, rowsColumns.NColumns);
    }
}

      



Now you can convert back and forth:

Point point1 = new Point(5, 10);
NRowsColumns nRowsColumns = point1;
Point point2 = nRowsColumns;

      

Keep in mind that each "transformation" is a new object.

+1


source


Why not just inherit from Point?

public struct  NRowsColumns: Point
{
   public int NRows {get {return base.x;}}
   public int NColumns {get {return base.y;}}
   public NRowsColumns(int nRows, int nColumns) 
      : base(nRows, nColumns)
   {
   }
}

      

0


source







All Articles