简体   繁体   中英

How do i access a class variable by subclass C#

I need to access "pointHeight" of the "Bar" class by the "Point" subclass, I can't use interinheritance, as I will have more than one "Point" in my "Bar" class. Alternatives that can give me future problems: a static variable or putting "pointHeight" in the Point constructor. This is possible in java, with the exact same code.

class Bar
    {
        public int pointHeight = 50;
        private List<Point> constructPoints(int size)
        {
            int x = 10; 
            List<Point> points = new List<Point>();
            for(int i = 0; i < size; i++)
            {
                points.Add(new Point(x));
                x += 10;
            }
            return points;
        }
        class Point
        {
            private int x, y;
            public Point(int x)
            {
                this.x = x;
            }
            public void update()
            {
                this.y = pointHeight; //<--- Error
            }
        }
    }
}

Pass it to the method:

public void update(int pointHeight)
            {
                this.y = pointHeight;
            }

then call it this way"

for(int i = 0; i < size; i++)
            {
                var p = new Point(x);
                p.update(pointHeight);
                points.Add(p);
                x += 10;
            }

Point doesn't inherit from Bar.

You've just defined it in Bar, which means you new up instances of Point with:-

// Set point with 3 as
var p = new Bar.Point(3);

Explicitly change Point so that it inherits from Bar.

Change:-

class Point
{
   ...  

  

to

class Point : Bar
{
   ...

Then you can access pointHeight from an instance of Point.

It depends on how you want this to behave. Is Point.y aways equal to pointHeight value even if this changes, or is it initialized with the current value of pointHeight . Is the update() function necessary to sync values at specific times, or is a reference to the current pointHeight value enough.

Scenario 1 - Point is immutable with a derived property, no update needed.

public class Bar
{
    public int PointHeight { get; set; } = 50;

    private List<Point> ConstructPoints(int size)
    {
        int x = 10;
        List<Point> points = new List<Point>();
        for (int i = 0; i < size; i++)
        {
            points.Add(new Point(this, x));
            x += 10;
        }
        return points;
    }
}
public readonly struct Point
{
    readonly Bar bar;
    public int X { get; }
    public int Y { get => bar.PointHeight; }

    public Point(Bar bar, int x)
    {
        this.bar = bar;
        this.X = x;
    }
}

Names have been changed to conform to .NET naming standards and to make the code more readable. Also note the use of properties for greater flexibility and a readonly struct for Point to keep values immutable.

Here each point has a reference the Bar object, and pulls the PointHeight property from it when needed.

Scenario 2 - Point is mutable and sync happens on demand

public class Bar
{
    public Bar(int pointHeight = 50)
    {
        PointHeight = pointHeight;
    }

    public int PointHeight { get; set; }

    public IList<Point> ConstructPoints(int size)
    {
        int x = 10;
        List<Point> points = new List<Point>();
        for (int i = 0; i < size; i++)
        {
            points.Add(new Point(x, PointHeight));
            x += 10;
        }
        return points;
    }

    public void UpdatePoints(IList<Point> points)
    {
        for (int i = 0; i < points.Count; i++)
        {
            points[i].Update(this);
        }
    }
}
public class Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
    public void Update(Bar bar)
    {
        this.Y = bar.PointHeight;
    }
}

internal class Program
{
    static void Main(string[] args)
    {
        var bar = new Bar(50);
        var pts = bar.ConstructPoints(16);
        bar.PointHeight = 32;

        bar.UpdatePoints(pts);
    }
}

This is more similar to the op code, but there might be issues with mutable Point class. I would always recommend readonly struct when appropriate to keep value semantics.

In this scenario (and in the code in question) it is unclear when Update() should be called.

both classes look weird for me, but try this

class Bar
    {
        public int pointHeight = 50;
        private List<Point> constructPoints(int size)
        {
            int x = 10; 
            List<Point> points = new List<Point>();
            for(int i = 0; i < size; i++)
            {
                points.Add(new Point(x,pointHeight));
                x += 10;
            }
            return points;
        }
        class Point
        {
            private int x, y,z;
            public Point(int x,z)
            {
                this.x = x;
                this.z = z;
            }
            public void update()
            {
                y = z;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM