Home > Enterprise >  How do i access a class variable by subclass C#
How do i access a class variable by subclass C#

Time:02-03

I need to access "pointHeight" of the "Bar" class by the "Point" class, 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
            }
        }
    }
}

[Edit] Thanks for the answers, but what I was looking for is apparently not possible in C#. But I solved my problem in two ways:

pass my "Bar" as a parameter in the method:

public void update(Bar bar)
                {
                    this.y = bar.pointHeight; //<--- Solved
                }

or in the class:

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

Once again: thanks for the help.

the reason not to use static: ill have more than one "Bar" object; the reason not to use interinheritance: list in my "Bar" class

CodePudding user response:

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;
            }

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

both classes look weird for me, but you You could wrap pointHeight inside a class and store a reference to the class

public class IntWrapper
{
    public IntWrapper(  int val ) { Value = val; }
    public int Value { get; set; }
}

 class Bar
{
    public int pointHeight
    {
        get { return _intWrapper.Value; }
        set {_intWrapper.Value=value;}
    } 
    
    private IntWrapper _intWrapper = new IntWrapper(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, _intWrapper));
            x  = 10;
        }
        return points;
    }
    class Point
    {
        private int x, y;
        
        private IntWrapper _wrapper;
    
        
        public Point(int x, IntWrapper wrapper)
        {
            this.x = x;
            
            _wrapper=wrapper;
        }
        public void update()
        {
            y = _wrapper.Value;
        }
    }
}

or you can use the whole Bar class as a wrapper as well, but I don' t know what fits better for you

 points.Add(new Point(x, this));
  •  Tags:  
  • Related