So at begining I like to set up some variables, which will later used. I am building some graph moving parts, where I must set up Step for every movement (if X change for 1 then Value change 1*Step ... bla bla).
I have MainWindowViewModel (short version):
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
// Initialization
Step = 3;
}
}
DiagramObject Class:
public abstract class DiagramObject : ViewModelBase
{
public abstract double X { get; set; }
public abstract string Xmeaning { get; set; }
public abstract double Y { get; set; }
public abstract string Ymeaning { get; set; }
}
So there inside I have defined "Steps":
public class DiagramNode : DiagramObject
{
public int xstep = 3;
public int ystep = 1;
public int xstepvalue = 5;
public int ystepvalue = 5;
private double _x;
public override double X
{
get { return _x; }
set
{
//"Grid Snapping"
_x = (Math.Round(value / xstep)) * xstep;
NotifyPropertyChanged("X");
double minutes = (_x / xstep) * xstepvalue;
TimeSpan interval = TimeSpan.FromMinutes(minutes);
_xmeaning = interval.ToString();
NotifyPropertyChanged("Xmeaning");
}
}
private string _xmeaning;
public override string Xmeaning
{
get { return _xmeaning; }
set
{
//"Grid Snapping"
_xmeaning = value;
NotifyPropertyChanged("Xmeaning");
}
}
private double _y;
public override double Y
{
get { return _y; }
set
{
//"Grid Snapping"
_y = (Math.Round(value / ystep)) * ystep;
NotifyPropertyChanged("Y");
double keks = (_y / ystep) * ystepvalue;
_ymeaning = keks.ToString();
NotifyPropertyChanged("Ymeaning");
}
}
private string _ymeaning;
public override string Ymeaning
{
get { return _ymeaning; }
set
{
//"Grid Snapping"
_ymeaning = value;
NotifyPropertyChanged("Ymeaning");
}
}
}
My question is How to update "xstep", "ystep" and others steps inside DiagramNode class from MainWindowViewModel at beginning of the program?
So when I start the program step will be defined and updated into DiagramNode class - now I have defined direct in class.
I hope that I give enough code for understanding the concept (if not say so). If any question please ask.
Your MainWindowViewModel will need to have an instance of the DiagramNode class instantiated so that it can access the properties to modify them.
public class MainWindowViewModel : ViewModelBase
{
DiagramNode myDiagramNode = new DiagramNode();
public MainWindowViewModel()
{
// Initialization
Step = 3;
myDiagramNode.xstep = 3;
}
}
Typically, though, it is a better practice to have variables like xstep and ystep to be set as private, and have accessors which can handle the setting/getting of the values, like so
public class DiagramNode : DiagramObject
{
private int xstep;
public int XStep
{
get { return this.xstep; }
set { this.xstep = value; }
}
...
}
public class MainWindowViewModel : ViewModelBase
{
DiagramNode myDiagramNode = new DiagramNode();
public MainWindowViewModel()
{
myDiagramNode.XStep = 3;
}
}
You could set them via a constructor overload
public class DiagramNode(int xstep, int ystep)
{
// set your values
}
Related
In the code example below, if HighValue and LowValue properties are not set by the client, how can I pass default values for these properties to the base class?
If there is a design mistake in this example setup, I'd like to thank you in advance for warning me.
public class Foo
{
public Foo(int scaleHigh, int scaleLow)
{
ScaleHigh = scaleHigh;
ScaleLow = scaleLow;
}
public int ScaleHigh { get; }
public int ScaleLow { get; }
}
public class Bar : Foo
{
public Bar(Bar bar)
: base(bar.ScaleHigh, bar.ScaleLow)
{
HighValue = SomeHelper.ReCalculate(bar.HighValue);
LowValue = SomeHelper.ReCalculate(bar.LowValue);
}
public int HighValue { get; }
public int LowValue { get; }
}
public class SomeHelper
{
public static int ReCalculate(int scale)
{
return scale * 5;
}
}
public class Client : Bar
{
public Client(Bar bar) : base(bar) { }
public int Request(bool condition)
{
return condition ? HighValue : LowValue;
}
}
you have a bug in your bar class, since it doesn't have a default constructor, you will never be able to create the object, since it will be a recursion forever - each new instance would neeed another and so on
public class Bar : Foo
{
public Bar(Bar bar)
: base(bar.ScaleHigh, bar.ScaleLow)
{
HighValue = SomeHelper.ReCalculate(bar.HighValue);
LowValue = SomeHelper.ReCalculate(bar.LowValue);
}
public int HighValue { get; }
public int LowValue { get; }
}
you can fix it by adding another constructor like this
public Bar(int scaleHigh=0, int scaleLow=0, int highValue=0, int lowValue=0)
: base(scaleHigh, scaleLow)
{
HighValue = SomeHelper.ReCalculate(highValue);
LowValue = SomeHelper.ReCalculate(lowValue);
}
Try this:
public int LowValue { get; } = 0; //You can change these values.
public int HighValue { get; } = 10;
Sorry if I misunderstand, but I believe you want to set default values of Bar if they are not set? In what case would the constructor not set the values?
In the case that you didn't set those in your constructor, one thing you can consider is making the types int? and then set the variable like this:
public int? HighValue => HighValue ?? (defaultValue)
I was trying to illustrate the Liskov principle with a case where it breaks it and was expecting in the below example that when you set breadth for the sqaure, the length is automatically set to the same length, and vice versa.
However, the area is returned as 0. I was expecting 4x4=16, and 5x5=25, in the second case. What am I doing wrong? I suspect it's in the way I'm overriding the properties of the base class.
using System;
public class Rectangle
{
public int length { get; set; }
public int breadth { get; set; }
public int area()
{
return length * breadth;
}
}
public class Square : Rectangle {
public new int length;
public new int breadth;
public new int Length
{
get
{
return this.length;
}
set
{
this.breadth = this.length = value;
}
}
public new int Breadth
{
get
{
return this.breadth;
}
set
{
this.breadth = this.length = value;
}
}
}
public class Program
{
public static void Main()
{
Square s = new Square();
s.length = 4;
s.breadth = 5;
int xx = s.area();
Console.Write(xx);
s.length = 5;
s.breadth = 4;
xx = s.area();
Console.Write(xx);
}
}
When you inherit from a base class, you will inherit all of its Public and Protected members. When you declare a new member in the derived class with the same name. The compiler will gives you a warning asking you are you intended to hide that member? When you used the new keyword you told the compiler: Yes I want to hide this member please. The implementation of the area Method uses the base class properties, so it will not see your public Fields That's why you get the 0.
So your code will become:
public class Rectangle
{
protected int _length;
protected int _breadth;
public virtual int Length
{
get { return _length; }
set { _length = value; }
}
public virtual int Breadth {
get { return _breadth; }
set { _breadth = value; }
}
public int Area()
{
return Length * Breadth;
}
}
public class Square : Rectangle
{
public override int Breadth
{
get { return _breadth; }
set { _breadth = value;
_length = _breadth;
}
}
public override int Length {
get { return _length; }
set { _length = value;
_breadth = _length;
}
}
}
If you want to override something, you should add the virtual keyword in the base class definition of that thing.
In your example, you will cause a StackOverFlow exception. Because each property setters will call the other. That's why I used a protected member to prevent this thing from happening.
This is a good reference for you to read about inheritance
I have a abstract Class figures which contains Abstract properties
and I am Overriding them in Derived class Rectangle and
Square. Now, iI want to implement this with Interface. But I can't use
constructor and neither I can't Declare the Variable inside the
Interface. So, how to implement this using Interface where Figures Should be Interface and Square and Rectangle should be class?
abstract class Figures
{
int Width;
int _cs;
public Figures(int Width)
{
CS = Width;
}
public abstract int getarea
{
get;
}
public abstract int getperm
{
get;
}
public abstract int CS
{
set;
}
public abstract void display();
}
class Square : Figures
{
int _CsS;
public Square(int c) : base(c)
{
}
public override int getarea
{
get
{
return (_CsS * _CsS);
}
}
public override int getperm
{
get
{
return (2 * _CsS * _CsS);
}
}
public override int CS
{
set
{
_CsS = value;
}
}
public override void display()
{
Console.WriteLine("area={0} and perimeter={1}", getarea, getperm);
}
}
class Rectangle : Figures
{
int H;
int _csr;
public Rectangle(int H, int W) : base(W)
{
this.H = H;
}
public override int getarea
{
get
{
return H * _csr;
}
}
public override int getperm
{
get
{
return 2 * H * _csr;
}
}
public override int CS
{
set
{
_csr = value;
}
}
public override void display()
{
Console.WriteLine("area={0} and perimeter={1}", getarea, getperm);
}
}
so how to implement this using Interface
By definition, an interface won't let you implement anything. You can only specify things.
So you will have to remove the ctor and the fields from the interface IFigures and re-implement them in every class. You could reuse an implementation with a abstract class FiguresBase: IFigures but that's not always the best design.
It all depends on why you want the interface and how you will use it.
You can do something like this:
interface IFigures
{
int getarea
{
get;
}
int getperm
{
get;
}
int CS
{
set;
}
void display();
}
Thenk you can implement this interface from your classes and do your logic inside the class itself. So instead of putting the properties logic inside of your abstract class you will have to write them in your child classes.
class Square : IFigures
{
int _CsS;
public Square(int c)
{
CS = c;
}
public int getarea
{
get
{
return (_CsS * _CsS);
}
}
public int getperm
{
get
{
return (2 * _CsS * _CsS);
}
}
public int CS
{
set
{
_CsS = value;
}
}
public void display()
{
Console.WriteLine("area={0} and perimeter={1}", getarea, getperm);
}
//here you have implemented properties
}
Your abstract class is a good thing. It lets you re-use code.
Interfaces (i.e. contracts) are also good if you want to achieve a loosely coupled system.
You can use abstract classes and interfaces together, to achieve both code reusability and loosely coupled parts.
public interface IFigures
{
int getarea();
}
public abstract class Figures : IFigures
{
public abstract int getarea();
//opportunity for code reuse
protected int getarea_internal()
{
throw new NotimplementedExcpetion();
}
}
public class Square : Figures
public class Rectangle: Figures
here is the answer
with class Diagram
Class Diagram of the Program
interface IFigures
{
int Getarea
{
get;
}
int GetPerm
{
get;
}
int CS
{
//get;
set;
}
}
abstract class Figures:IFigures
{
int _Cs;
public Figures( int _Cs)
{
CS = _Cs;
}
public abstract int Getarea
{
get;
}
public abstract int GetPerm
{
get;
}
public abstract int CS
{
//get;
set;
}
public abstract void display();
}
class Circle:Figures
{
int _r, _csc;
public Circle(int _r):base(_r)
{
CS = _r;
}
public override int Getarea
{
get
{
return (_r * _r);
}
}
public override int GetPerm
{
get
{
return (2* _csc * _csc);
}
}
public override void display()
{
Console.WriteLine("area of Circle={0}", (_r * _r));
Console.WriteLine("perimeter of rectangle={0}", (2 * _r * _r));
}
public override int CS
{
//get
//{
// return _csc;
//}
set
{
_csc = value;
}
}
}
class Rectangle:Figures
{
int _L, _csr;
public Rectangle(int _L,int _W):base(_W)
{
this._L = _L;
CS = _W;
}
public override int Getarea
{
get
{
return _csr * _L;
}
}
public override int GetPerm
{
get
{
return (2* _csr * _L);
}
}
public override void display()
{
Console.WriteLine("area of rectangle={0}", (_csr * _L));
Console.WriteLine("perimeter of rectangle={0}", (2* _csr * _L));
}
public override int CS
{
//get
//{
// return _csr;
//}
set
{
_csr = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Figures f = new Rectangle(3, 4);
f.display();
//f.CS = 5;
f.display();
Console.ReadKey();
}
}
Example:
public class BoundingBox
{
public Vector3Double Positon { get; set; }
public Vector3Double Apothem { get; set; }
public ExtremasForX X;
public BoundingBox(Vector3Double position, Vector3Double size)
{
Positon = position;
X = new ExtremasForX(this);
}
private class ExtremasForX
{
private BoundingBox box;
public ExtremasForX(BoundingBox box)
{
this.box = box;
}
public double Max
{
get { return box.Positon.X + box.Apothem.X ; }
}
public double Min
{
get { return box.Positon.X - box.Apothem.X; }
}
}
}
This code produces an accessibility error: BoundingBox.X has a higher level than it's type.
I would like an inner class that does not have a public constructor, as I only wish to use the class as a namespace for the outer class. How can I do it?
If you really don't want to expose the inner type, you can to have the inner class implement an interface. Then, in the outer class, you expose X as being of the interface type but internally use the inner class' type.
Personally, I would just make the inner class public. Users can't hurt anything by instantiating the class, so it's not a big deal to expose the constructor.
Code for exposing the inner type, without exposing the constructor, via an interface:
public class BoundingBox
{
public Vector3Double Positon { get; set; }
public Vector3Double Apothem { get; set; }
public IExtremasForX X { get { return _x; } }
private ExtremasForX _x;
public BoundingBox(Vector3Double position, Vector3Double size)
{
Positon = position;
_x = new ExtremasForX(this);
}
public interface IExtremasForX {
public double Max { get; }
public double Min { get; }
}
private class ExtremasForX : IExtremasForX
{
private BoundingBox box;
public ExtremasForX(BoundingBox box)
{
this.box = box;
}
public double Max
{
get { return box.Positon.X + box.Apothem.X ; }
}
public double Min
{
get { return box.Positon.X - box.Apothem.X; }
}
}
}
Change the access modifier of class ExtremasForX to public and change its constructor to internal instead of public, like so:
public class BoundingBox
{
public Vector3Double Positon { get; set; }
public Vector3Double Apothem { get; set; }
public ExtremasForX X;
public BoundingBox(Vector3Double position, Vector3Double size)
{
Positon = position;
X = new ExtremasForX(this);
}
public class ExtremasForX
{
private BoundingBox box;
internal ExtremasForX(BoundingBox box)
{
this.box = box;
}
public double Max
{
get { return box.Positon.X + box.Apothem.X ; }
}
public double Min
{
get { return box.Positon.X - box.Apothem.X; }
}
}
}
Sorry for the title i will put here an example of what i want to accomplish:
namespace mdclass
{
class pClass
{
static void Main()
{
tiles<int> tl = new tiles<int>();
tl[0].X = 0;
}
}
class tiles<T> : List<T>
{
public int X
{
//get/set the X-coordonate
}
public int Y
{
//get/set the Y-coordonate
}
}
}
how can i transfer the [0] from the tl[0] in the public int X and work with it?
Create a class for x and y coordinates:
public sealed class Point {
public int X { get; set; }
public int Y { get; set; }
}
Use the Point class to store the coordinates into the list:
public sealed class Program {
public static void Main() {
var tiles = new List<Point>();
tiles.Add(new Point { X = 5, Y = 10 });
tiles[0].X = 15;
}
}
Could you not just make tl public?
Then myInt = mypClass.tl[0].X
A data heirachy like this might work for you (no time to add actual code, sorry!). Then you could implement appropriate getters and setters.
Public class Grid {
List<Row>
}
Public class Row{
List<Point>
}
Public Class Point{
public int X { get; set; }
public int Y { get; set; }
}