I currently have a class called:
public class HeatmapComponent : GH_Component
I also have another class called:
public class HeatMap
Inside the Heatmap class I have two instance variables declared as:
public int _width;
public int _height;
I would like to be able to access and set the _width and _height variables from the HeatmapComponent class. I know this is a scope issue, but, I am a bit confused as to what needs to be done.
In my HeatmapComponent class, this was what I had in mind:
this._width = width; // width is declared somewhere in this class
this._height = height; // height is same as above
I apologize beforehand if this is a stupid question. Please let me know if I am missing code snippets. I'll be happy to provide.
You want to set the values of those two fields? They are readonly. You can do that only in the constructor.
public class HeatMap
{
private readonly int _width;
private readonly int _height;
public HeatMap(int wid, int hei)
{
_width = wid;
_height = hei;
}
}
And, as it is with passing things through constructor's params, you can use/provide them only when building a new instance. That's why they are called constructor and readonly fields:
public class HeatmapComponent
{
private int widthOfMap;
private int heightOfMap;
void createMapAndDoSomething()
{
var hmap = new HeatMap(widthOfMap, heightOfMap);
hmap.thing();
}
}
If you don't want to create a new HeatMap, and if you want to be able to set the width/height from some 'external' place at any point in time, then:
they cannot be readonly
some public way of changing them must exist
For example:
public class HeatMap
{
private int _width;
private int _height;
public void SetSize(int wid, int hei)
{
_width = wid;
_height = hei;
}
}
public class HeatmapComponent
{
private int widthOfMap;
private int heightOfMap;
private HeatMap oldMap;
void changeTheSizes()
{
oldMap.SetSize(widthOfMap, heightOfMap);
}
}
Or sometimes even better, use properties:
public class HeatMap
{
private int _width;
private int _height;
public int Width { set { _width = value; } }
public int Height { set { _height = value; } }
}
public class HeatmapComponent
{
private int widthOfMap;
private int heightOfMap;
private HeatMap oldMap;
void changeTheSizes()
{
oldMap.Width = widthOfMap;
oldMap.Height = heightOfMap;
}
}
Before I answer your question, you have one major, major issue: readonly. That means that the value of the variable cannot be changed once the object is created. By anyone. Period.
Now, you have a couple ways to do this. The first is to use properties like Snorre said. In effect, you'd get this:
public class HeatMap
{
public int Width { get; set; }
public int Height { get; set; }
}
public class HeatMapComponent
{
private HeatMap myHeatMap; // Must have a reference to the object you want to change!
public void SomeMethod()
{
myHeatMap.Width = 10;
}
}
Now, the obvious downside here is that ANYONE can change the properties of HeatMap. If for some reason you really, really want to make HeatMap's width and height editable only by the HeatMapComponent, you can make HeatMapComponent an inner class, like this:
public class HeatMap
{
private int width;
private int height;
public class HeatMapComponent
{
public HeatMap myHeatMap;
public void SomeMethod()
{
myHeatMap.width = 10;
}
}
}
however, I would strongly advise you to rethink what you're trying to do. Public inner classes are actually quite rare, in my experience, as they can violate OOP principles easily. A different application design may suit you better.
Couple of things:
readonly keyword makes anything settable only in the constructor. Example:
class XYZ
{
private readonly int x;
public XYZ()
{
x = 10; //works
}
public void SomeMethod()
{
x = 100; //does not work since it is readonly
}
}
Then there's the various access modifiers: private is only accessible in the class itself, protected is accessible in inherited classes and public is accessible anywhere. Internal is accessible within the same assembly.
public class HeatMapComponent
{
HeatMap _map;
public HeatMapComponent()
{
_map = new HeatMap();
}
public void SomeMethod()
{
_map.Width = 10; //should work if Width is public and not readonly and if _map was initialized already, ie not null
}
}
This sounds like a homework question, and the problem is you are not understanding the lesson.
Here's a way to create your HeatMap class. It contains an overload so you can either set the Width and Height in the constructor or via a Set method:
public class HeatMap {
public HeatMap() {
Width = 0;
Height = 0;
}
public HeatMap(int width, int height) {
Width = width;
Height = height;
}
public void Set(int width, int height) {
Width = width;
Height = height;
}
public int Width { get; private set; }
public int Height { get; private set; }
}
To use this in your HeatmapComponent class, you only need to create an instance of your HeatMap. Here are two ways of doing that:
public HeatmapComponent() {
}
public void Test1(int width, int height) {
var hm = new HeatMap(width, height);
Console.WriteLine("Width: {0}, Height: {1}", hm.Width, hm.Height);
}
public static void Test2(int width, int height) {
var hm = new HeatMap();
hm.Set(width, height);
Console.WriteLine("Width: {0}, Height: {1}", hm.Width, hm.Height);
}
Be sure you understand what is going on, though.
Related
I'm learning inheritance and I understand the code below.
namespace InheritanceApplication {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Base class PaintCost
public interface PaintCost {
int getCost(int area);
}
// Derived class
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 70;
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
However, why have they created the set height and set width functions. Would it not be better practice to simply just do this:
public int width {get;set;}
public int height {get;set;}
and then in the main class just do something like below:
rect.width = 5;
rect.height = 7;
Many thanks,
Amir
I'm sure others will provide different points, but here are my main 2 reasons for using gets/sets. If these don't apply for a given property, chances are I won't use getters/setters.
1 - Debugging
It makes it significantly easier to debug data propagation (how data gets passed around) if you can debug a setter that you're concerned about. You can easily throw in a Debug.Print call and debug the value being set if you're concerned it's being passed the wrong value. Or you could place break points and actually debug through the stack trace. For example:
class Shape {
public void setWidth(int w) {
if(w < 0)
Debug.Print("width is less than 0!");
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
2 - Value Change Actions
There may be better ways to achieve this, but I like being able to add simple logic to setters to ensure that any logic that needs to run when a value changes does so. For instance I may use the following:
public void SetWindowHeight(int newHeight)
{
if(WindowHeight == newHeight)
return;
WindowHeight = newHeight;
UpdateWindowDisplay();
}
public int GetWindowHeight()
{
return WindowHeight;
}
private int WindowHeight;
public void UpdateWindowDisplay()
{
Window.UpdateHeight(WindowHeight);
// Other window display logic
}
Although personally I prefer to use property gets/sets, but that's just my preference.
public int WindowHeight
{
get
{
return windowHeight;
}
set
{
if(windowHeight == value)
return;
windowHeight = value;
UpdateWindowDisplay();
}
}
private int windowHeight;
public void UpdateWindowDisplay()
{
Window.UpdateHeight(WindowHeight);
// Other window display logic
}
"Create a program named "DemoSquare" that initiates an array of 10 Square objects with sides that have values of 1 -10 and that displays the values for each square. The Square class contains fields for the area and the length of a side, and a constructor that requires a parameter for the area and the length of a side. The constructor assigns its parameter to the length of a Square's side and calls a private method that computes the area field. Also include read-only properties to get a Squares side and area."
Now I think that it is a trick question as I can't get the private method to compute the area because of the read-only assignment but here is my code:
class demoSquares
{
static void Main(string[] args)
{
Square[] squares = new Square[10];//Declares the array of the object type squares
for (int i = 0; i < 10; i++)
{
//Console.WriteLine("Enter the length");
//double temp = Convert.ToDouble(Console.ReadLine());
squares[i] = new Square(i+1);//Initializes the objects in the array
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(squares[i]);
}//end for loop, prints the squares
}//end main
}//end class
This is the Square Class:
public class Square
{
readonly double length;
readonly double area;
public Square(double lengths)//Constructor
{
length = lengths;
area = computeArea();
}
private double computeArea()//getmethod
{
double areaCalc = length * length;
return areaCalc;
}
}
Don't confuse read-only properties with read-only fields.
public class Square
{
public Square(double lengths)
{
Length = lengths;
Area = computeArea();
}
//Read only property for Length (privately settable)
public double Length {get; private set;}
//Read only property for Area (privately settable)
public double Area {get; private set;}
//Private method to compute area.
private double ComputeArea()
{
return Length * Length;
}
}
A read-only variable can indeed be assigned in the constructor, but not in methods called from the constrctor. There are ways to do that, i.e. : link. The correct way would be to calculate the area and store the result in the area variable.
I believe, though, that the meaning was different in the question. Quoting you :
Also include read-only properties to get a Squares side and area.
meaning, the question meant that you use Properties. Meaning, you would use private variables for length and area, and implement a get-only property for each :
public double Area
{
get
{
return area;
}
}
The question mentions readonly properties, not readonly fields.
a readonly field can only be assigned in a constructor or by a field initializer.
A readonly property can only be assigned inside the class.
public class Square
{
// Readonly field, can only be assigned in constructor or initializer
private readonly double _sideLength;
// Readonly property since it only contains a getter
public double SideLength { get { return _sideLength; } }
// Readonly property from outside the class since the setter is private
public double Area {get; private set;}
public Square( double sideLength )
{
_sideLength = sideLength;
CalcSquare();
}
private void CalcSquare()
{
this.Square = _sideLength * _sideLength;
}
}
Consider how you would restructure the code if you didn't try to assign the computed area to the area field, but instead returned the value from computeArea.
As an additional exercise, try making computeArea static and see how that affects the code.
You assignment never said your private function should assign area. It said the constructor should assign area with the result of a private method:
public class Square
{
private readonly double length;
private readonly double area;
public Square(double length)
{
this.length = length;
this.area = computeArea(length); // assignment of area in constructor!
}
private double ComputeArea(double length)
{
return length * length;
}
}
I am converting a VB.Net application to C# (as well as learning C#) and I ran into a problem. One of the functions takes in an object and applies modifications to certain parameters based on what is passed. This way, one function can be used to update any control passed to it, which is working fine in VB.
The full function has a lot more logic behind it, but here is a scaled back version showing the basics:
public void TransformObject(object objObject, int LeftPadding, int TopPadding, int WidthChange, int HeightChange)
{
objObject.Top = TopPadding;
objObject.Left = LeftPadding;
objObject.Width = WidthChange;
objObject.Height = HeightChange;
}
The problem is that 'Top', 'Left', 'Width', 'Height', etc. are not defined, since it is using the object type.
Is there a way to keep the existing structure without having to create a separate function or definition for each possible control type?
EDIT: I am using the .Net 3.5 framework.
You can use dynamic:
public void TransformObject(object objObject, int LeftPadding, int TopPadding, int WidthChange, int HeightChange)
{
dynamic dynObject = (dynamic)objObject;
dynObject.Top = TopPadding;
dynObject.Left = LeftPadding;
dynObject.Width = WidthChange;
dynObject.Height = HeightChange;
}
Or find an interface/base class (possibly Control?) that all these types have in common and use it in method signature, instead of object.
Use a base class, like MyBaseClass that has definitions for Top, Left, Width, and Height
public class MyBaseClass
{
public int Top {get; set;}
public int Left{get; set;}
public int Width {get; set;}
public int Height {get; set;}
}
and every class that you want to pass in, should derive from that base class
public class MyDerivedClass : MyBaseClass
{
}
You will need to determine if the objects passed to the function implement the same interface or inherit from the same base class that exposes those properties. If they do then simply change the interface of the function so you accept the common interface/base class type as the parameter rather than object. If not then you are left with nasty choices.
One of the solutions here is the use of interfaces. For example if I understand your question right you could try this:
public interface IMyInterface
{
int Top { get; set;}
int Left { get; set; }
int Width { get; set; }
int Height { get; set; }
}
public void TransformObject(object objObject, int LeftPadding, int TopPadding, int WidthChange, int HeightChange)
{
if (objObject is IMyInterface)
{
((IMyInterface)objObject).Top = TopPadding;
((IMyInterface)objObject).Left = LeftPadding;
((IMyInterface)objObject).Width = WidthChange;
((IMyInterface)objObject).Height = HeightChange;
}
}
Code is not optimized but this should do the trick :)
I would venture a guess that the object being passed is in fact a Control (assuming this is a WinForms application). You could change the method signature to specify this and see if that is in fact the case, or you could cast the object to a Control and test thoroughly (and/or add logging if the cast fails:
Changing the signature:
public void TransformObject(System.Windows.Forms.Control objObject, int LeftPadding, int TopPadding, int WidthChange, int HeightChange)
{
objObject.Top = TopPadding;
objObject.Left = LeftPadding;
objObject.Width = WidthChange;
objObject.Height = HeightChange;
}
Downcasting internally:
public void TransformObject(object objObject, int LeftPadding, int TopPadding, int WidthChange, int HeightChange)
{
var control = objObject as System.Windows.Forms.Control;
if (control != null)
{
control.Top = TopPadding;
control.Left = LeftPadding;
control.Width = WidthChange;
control.Height = HeightChange;
}
else
{
// Turns out it isn't a control, throw an exception or Log it
}
}
Don't you define the new instance of the object before calling the method.
object obj = new object()
TransformObject(obj,5,5,5,5)
public void TransformObject(object objObject, int LeftPadding, int TopPadding, int WidthChange, int HeightChange)
{
objObject.Top = TopPadding;
objObject.Left = LeftPadding;
objObject.Width = WidthChange;
objObject.Height = HeightChange;
}
By way of an intro, I'm creating a basic Quadtree engine for personal learning purposes. I'm wanting this engine to have the capability of working with many different types of shapes (at the moment I'm going with circles and squares) that will all move around in a window and perform some sort of action when collision occurs.
Here are my shape objects as I have them so far:
public class QShape {
public int x { get; set; }
public int y { get; set; }
public string colour { get; set; }
}
public class QCircle : QShape {
public int radius;
public QCircle(int theRadius, int theX, int theY, string theColour) {
this.radius = theRadius;
this.x = theX;
this.y = theY;
this.colour = theColour;
}
}
public class QSquare : QShape {
public int sideLength;
public QSquare(int theSideLength, int theX, int theY, string theColour) {
this.sideLength = theSideLength;
this.x = theX;
this.y = theY;
this.colour = theColour;
}
}
Now my question is, how do I create a generic list (List<T> QObjectList = new List<T>();) in C# so I can have one list containing all these various shapes that may have different properties (e.g., QCircle has the "radius" property while QSquare has the "sideLength" property)? An example of implementation would be helpful as well.
I just know that there is a stupidly obvious answer to this question but I'd appreciate any help anyway. I'm trying to get back into C#; it has obviously been a while...
You need to use downcasting
Store the objects in a list with the base class
List<QShape> shapes = new List<QShape>
You can then upcast the object safely if you know what it is e.g.
if(shapes[0] is QSquare)
{
QSquare square = (QSquare)shapes[0]
}
You can also implicitly downcast objects
QSquare square = new Square(5,0,0,"Blue");
QShape shape = square
For more information read the Upcasting and Downcasting sections here
You should implement an Interface. For example
public interface IHasLength
{
int Length;
}
Then in the implementation you can do
public class QSquare : QShape, IHasLength {
public int sideLength;
public QSquare(int theSideLength, int theX, int theY, string theColour) {
this.sideLength = theSideLength;
this.x = theX;
this.y = theY;
this.colour = theColour;
}
public int Length { get { return sideLength; } }
}
public class QCircle : QShape, IHasLength {
public int radius;
public QSquare(int theSideLength, int theX, int theY, string theColour) {
this.sideLength = theSideLength;
this.x = theX;
this.y = theY;
this.colour = theColour;
}
public int Length { get { return radius; } }
}
FInally, in your list:
List<IHasLength> shapesWithSomeLength = new List<IHasLength>();
Now your list can hold ANYTHING that implements IHasLength whether it's a QCircle, QShape, or even a QDuck if you want as long as it implements IHasLength.
Is this what you want?
public class QShape
{
protected QShape() { }
public int x { get; set; }
public int y { get; set; }
public string colour { get; set; }
}
public class QCircle : QShape
{
public int radius;
public QCircle(int theRadius, int theX, int theY, string theColour)
{
this.radius = theRadius;
this.x = theX;
this.y = theY;
this.colour = theColour;
}
}
public class QSquare : QShape
{
public int sideLength;
public QSquare(int theSideLength, int theX, int theY, string theColour)
{
this.sideLength = theSideLength;
this.x = theX;
this.y = theY;
this.colour = theColour;
}
}
class Program
{
static void Main(string[] args)
{
List<QShape> list = new List<QShape>();
list.Add(new QCircle(100, 50, 50, "Red"));
list.Add(new QCircle(100, 400, 400, "Red"));
list.Add(new QSquare(50, 300, 100, "Blue"));
foreach (var item in list.OfType<QCircle>())
{
item.radius += 10;
}
foreach (var item in list.OfType<QSquare>())
{
item.sideLength += 10;
}
}
}
You could store them in a List<QShape> but this would mean that you could not access type-specific properties.
Generally, you might approach this by providing a common interface in your base class, and overriding behaviour in subclasses. In this way, a common interface can hide a diverse bunch of behaviours. For instance a Grow method could hide the complexities of growing items of different shape and could be called without explicit knowlege of the shape upon which it is operating.
public abstract class QShape {
public abstract void Grow(int amt);
}
public class QSquare : QShape {
private int sideLength;
public override void Grow(int amt)
{
sideLength+=amt;
}
}
public class QCircle : QShape {
private int radius;
public override void Grow(int amt)
{
radius+=amt;
}
}
I feel like i'm missing something but...
List<QCircle> circleObjects = new List<QCircle>();
and
List<QSquare> squareObjects = new List<QSquare>();
will work perfectly well.
EDIT:
Ah, I didn't understand what was being asked.
Yes, as your QCircle and QSquare classes inherit from QShape, you can just do.
List<QShape> shapes= new List<QShape>();
It's worth noting that if you want to access the radius property of all the QCircle's in that list, then you are going to have to filter the list based on type.
You can use Ian Mercer's comment List<QShape>
And here's how you would fill it:
List<QShape> shapes = new List<QShape>();
QCircle circle = new QCircle();
shapes.Add(circle);
To unbox it:
QCircle circle = (QCircle) shapes[0];
If you need to call a method off the base class, no need to unbox, just use it.
Storing
You're already on the right track with your class definitions. What you have to do is make a List of the superclass (in this case, QShape), which will be able to hold all of your shapes.
Here's an example of how you would make it:
List<QShape> objects = new List<QShape>();
objects.add(new QCircle(...));
objects.add(new QSquare(...));
Accessing
The problem here is differentiating what is what once everything is in the list. That's done with the getType() and typeof() methods of C#. (Jon Skeet has an excellent answer about how to do this). Basically, it looks like this:
if(objects.get(some_position).getType() == typeof(QCircle))
QCircle circle = objects.get(some_position);
else if(/* like above with QSquare */)
QSquare square = objects.get(some_position);
After you do this, you can resume using your objects like normal. But if you try accessing them from the list, you can only use the methods and variables that QShape has, as every object put in the list will be cast to it.
public Class abstract Base<T>
{
public abstract List<T>GetList();
}
then do this
public class className:Base<ObjectName>
{
public override List<T>GetList()
{
//do work here
}
}
I have many constants I need to move around in one of my classes but I'm not allowed to use member variables. What are some options?
Here's my initial try:
private void MyConstants(out int textSize, out int paddingValue, out int borderType, ...)
{
//Set them here
}
private Method1()
{
int textSize = 0;
int paddingValue = 0;
int borderValue = 0;
....
MyConstants(out textSize, out paddingValue, out borderValue)
}
private Method2()
{
int textSize = 0;
int paddingValue = 0;
int borderValue = 0;
....
MyConstants(out textSize, out paddingValue, out borderValue)
}
//Many more methods...Just seems to repetitive.
Use Private Class or Struct.
private class Variables {
public int textSize;
public int paddingValue;
public int borderValue;
}
private Variables MyConstants{
get{ return new Variables(){textSize=1, paddingValue=2, borderValue=3};}
}
If you need to initialize in constructor try using readonly instead on const.
Edit 1:
You can create a class that holds a private Dictionary:like key and value.
put a "object LoadConstant(string)" method in it that will withdraw the data.
public static class ConstantManager {
private static Hashtable _consts = new Hashtable();
private static const keyName = "Name 1";
public static void ConstantManager()
{
_consts[keyName] = ...;
}
public static Hashtable GetConstants()
{
var _copy = new Hashtable(_consts);
return _copy;
}
}
public OtherClass{
public void Method()
{
var consts = ConstantManager.GetConstants();
var a = consts[ConstantManager.keyName];
}
}