So I'm creating a Snake-game and want to test that the "Snake-food" changes Position when "eaten".
So I have a simple Position-class that looks kinda like this:
public class Position
{
public int XCoordinate {get; set;}
public int YCoordinate {get; set;}
public Position(int x, int y)
{
XCoordinate = x;
YCoordinate = y;
}
}
And in the test, I want to see that the Position of the food updates when eaten. Because there seems to be no good way of comparing objects, I'm doing this with Assert like this(simplified method):
[Test]
public void AssertFoodSpawnsOnNewLocationWhenEaten()
{
int expectedX = _sut.Food.Pos.XCoordinate;
int expectedY = _sut.Food.Pos.YCoordinate;
_sut.FeedSnake();
int resultX = _sut.Food.Pos.XCoordinate;
int resultY = _sut.Food.Pos.YCoordinate;
Assert.AreNotEqual(expectedX, resultX);
Assert.AreNotEqual(expectedY, resultY);
}
Realized that my solution is a bad one, since the Food-Position could have been updated even tough e.g. expectedX and resultXare equal.
Does anyone have a tip how I could test this? Can you do some kind of Assert.AreNotEqual checking if both x-and y-coordinates are not equal?
Thankful for any help!
Made a separate private function that determines if the x and y coordinates are the same. This checks if _sut.FeedSnake()updates the Food-position when eaten.
[Test]
public void AssertFoodSpawnsOnNewLocationWhenEaten()
{
Position expected = _sut.Food.Pos;
_sut.FeedSnake();
Position result = _sut.Food.Pos;
Assert.False(IsPositionEqual(expected, result));
}
private bool IsPositionEqual(Position Pos1, Position Pos2)
{
if (Pos1.XCoordinate == Pos2.XCoordinate &&
Pos1.YCoordinate == Pos2.YCoordinate)
{
return true;
}
return false;
}
Related
This is my object
public class Totals {
public int Total1 { get; set; }
public int Total2 { get; set; }
public int Total3 { get; set; }
public int Total4 { get; set; }
}
Incrementing the values of Total1 and Total2 using calculateTotals method
private Totals calculateTotals(Totals t) {
if (//condition) {
t.Total1 += 1;
} else {
t.Total2 += 1;
}
return t;
}
**Incrementing value of Total3 and Total4 of the same object with same conditions at a different location using different method calculateOtherTotals, at this point I only need to update Total3 and Total4 **
private Totals calculateOtherTotals(Totals t) {
if (//condition) {
t.Total3 += 1;
} else {
t.Total4 += 1;
}
return t;
}
I am new to c# , I need to increment the values Total1,Total2 and Total3,Total4 separately and the code which I have is working fine
Is there a way to improve my code?, how can I avoid creating two different methods which pretty much does the same logic on different properties? is there a way to create only 1 method to achieve my functionality?
You could do it this way, but essentially the amount of code doesn't change.
This adds a judgment:
Totals calculateTotals(Totals t, bool Flag)
{
//function1:
if (Flag)
{
if (true)
{ //(condition) {
t.Total1++;
}
else
{
t.Total2++;
}
}
//function2:
else
{
if (true)
{ //(condition) {
t.Total3++;
}
else
{
t.Total4++;
}
}
return t;
}
Call it like this:
Totals totals = new Totals();
totals.Total1=0;
totals.Total2=0;
totals.Total3=0;
totals.Total4=0;
calculateTotals(totals,true);//function1:
calculateTotals(totals,false);//function2:
Reflection is one way, though its slow and not a Domain Specific Language:
Type totalsType = typeof(Totals);
var totalToIncrement = condition;
PropertyInfo prop = totalsType.GetProperty("Total" + totalToIncrement);
prop.SetValue(null, 76);
Or perhaps you want to abstract the properties you're incrementing:
private Totals calculateTotals(Totals t)
{
bool condition = true;
AbstractAdds(ref t.Total1, ref t.Total2, condition);
return t;
}
private void AbstractAdds(ref int a, ref int b, bool condition = false)
{
if (condition)
{
a++;
}
else
{
b++;
}
}
}
public class Totals
{
public int Total1;//{ get; set; }
public int Total2;//{ get; set; }
public int Total3;//{ get; set; }
public int Total4;//{ get; set; }
}
I'd personally have a List<int> or int[3] and make the condition calculate the index 0-3:
var index = calcCondition;
Totals[index]++;
This way its extensible for more totals and you get inbuilt functions like LINQ, eg Totals.Sum().
Is there a way to improve my code?, how can I avoid creating two different methods which pretty much does the same logic on different properties? is there a way to create only 1 method to achieve my functionality?
Then it depends on how you want your method (function) to be. (E.g., how you define what your function will do and how your class and properties are characteristic—which, currently, many who want to help you still wonder about.)
Let me give another clear example.
Assume that you answer your additional requirement are:
My object has only 4 properties of "Total"
I want these new function to increment value only 1 when call, no need to add more than 1
This function is called from another class to modify my object value
I want my cool function name calculateOtherTotals being private, because of some unexplained reason such as “I don't like others knowing it exists”.
Then
public OtherClass{
Public Totals ExposeThePrivateCalculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4)
{
calculateOtherTotals(t, IncrementT1 , IncrementT2 , IncrementT3, IncrementT4);
}
Private Totals calculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4) {
if( IncrementT1 ) t.Total1 += 1; //choose your style
if( IncrementT2==true ) ++t.Total2;//choose your style
if( IncrementT3!=false ) t.Total3++; //choose your style
t.Total4 += IncrementT4==true?1:0;//choose your style
return t;
}
}
//In main (how to use)
Totals t= new Totals();
OtherClass doMyFunc = new OtherClass();
t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, true, false,false,false); // result of operation => t.total1 += 1;
t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, false, true,false,false); // result of operation => t.total2 += 1;
This question already has answers here:
C# 3.0 Auto-Properties - Is it possible to add custom behaviour?
(6 answers)
Closed 1 year ago.
I have the following class with auto properties:
class Coordinates
{
public Coordinates(int x, int y)
{
X = x * 10;
Y = y * 10;
}
public int X { get; set; }
public int Y { get; set; }
}
As you can see from the constructor I need the value to be multiplied by 10.
Is there anyway to do it without removing autoproperties?
I tried the following not thinking that it causes recursion and then everything goes fubar
public int X { get {return X;} set{ X *= 10;} }
I would like to assign values to X and Y multiplied by 10.
Coordinates coords = new Coordinates(5, 6); // coords.X = 50 coords.Y = 60
coords.X = 7; // this gives 7 to X but I would like it to be 70.
In order to make setter working like that, you'll need to use backing field:
class Coordinates
{
public Coordinates(int x, int y)
{
X = x;
Y = y;
}
private int _x;
public int X
{
get { return _x; }
set { _x = value * 10; }
}
private int _y;
public int Y
{
get { return _y; }
set { _y = value * 10; }
}
}
Given your example:
Coordinates coords = new Coordinates(5, 6); // coords.X = 50 coords.Y = 60
coords.X = 7; // this gives 70
However, I don't recommend you having such setter because it could lead to confusion. It's better to have a dedicated method which will do such multiplication. In the end, your code will be more descriptive and intuitive.
You get a recursion, because you again call the same property, which in turn calls the same property, which in turn calls the same property... you get the point.
public int X { get {return X;} set{ X *= 10;} }
How does auto properties works ?
Behind the scenes Properties are actually methods, which means they don't actually store data. So who saves the data ? AutoProperties generate private backend field to save the data.
So in the simple declaration of auto property
int X { get; set; }
The compiler translate it into something like that
private int <X>k__BackingField;
public int X
{
[CompilerGenerated]
get
{
return <X>k__BackingField;
}
[CompilerGenerated]
set
{
<X>k__BackingField = value;
}
}
So no matter if you use it as Auto Properties or simple property, they are the same.
Now, to answer you question, with paraphrasing, "How do i return the value multiply with 10"
You can solve it with using 2 ways:
1. By saving the data multiply by 10 (setter implementation)
2. By returning the data multiply by 10 (getter implementation)
I won't elavorate, which one you should use, because for this kind of simple scenario, both will be perfectly valid.
I would just say that some of the factors for the choice will be micro(micro micro micro) performence, true state storage.
Here is the setter implementation
private int _x;
public int X
{
get
{
return _x;
}
set
{
return _x*10;
}
}
I am trying to solve or be pointed in the right direction. I am having difficulty determining where to place my Area formula in the Triangle Class (not the main). Area can only have a 'get' and not a 'set'.
Next issue is identifying the type of triangle based on the inputed side and if it is a 'right' triangle, appending the 'type' with '-right' for example (isosceles-right). I have an enum for the triangle types.
I'm not looking for the direct answer to solve this but rather some help and coaching to help better build my skills
Here is the class structure I have generated so far in C#, please keep in mind it is not complete.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TriangleCheck;
namespace TriangleCheck
{
public class Triangle
{
private StringBuilder _ErrorMsg;
private int[] _Sides;
private const int _nSides = 3;
private int _Area;
public Triangle(int[] Sides)
{
//Track amunt of errors recieved.
int nErrors = 0;
//Make sure ErrorMsg is cleared
_ErrorMsg = new StringBuilder();
//Did I get _nSides? If not, append to ErrorMsg and throw exception
if(Sides.Length != _nSides)
{
_ErrorMsg.Append(string.Format("Expected {0} sides but recieved {1}", _nSides, Sides.Length));
nErrors += 1;
}
//Is each side positive? If not, append to ErrorMsg and throw exception
for (int i = 0; i < Sides.Length; i++)
{
if (Sides[i] <= 0)
{
_ErrorMsg.Append(string.Format("{0} side is not a postive integer", Sides[i]));
nErrors += 1;
}
}
//Set input from user to private property _Sides
_Sides = Sides;
_Area = Area;
}
public int Area
{
get { return _Area; }
private set
{
int parameter =
}
}
public string ErrorMsg
{
get
{ return ErrorMsg.ToString();}
}
public bool IsRight
{
get
{
return ;
}
}
public int Sides
{
get
{ return _Sides; }
set
{
if (value > 0)
{
_Sides = value;
}
else
throw new ArgumentOutOfRangeException("Value must be postive!");
}
}
public TriangleTypes TriangleTypes
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
public void ScaleUp(int[] ScaleFactor)
{
throw new System.NotImplementedException();
}
public override string ToString()
{
return "A Triangle with sides " + _Sides + " is Type: " + TriangleTypes + " with Area:" + Area;
}
}
}
You mention that you can't set the Area property... it looks like you're trying to enforce that by making a private set, but why not just exclude the set leaving it as a read-only property?
The Area formula could go a couple places; the key is that it is derived from the sides but only matters when someone asks for it. So you could reasonably:
Apply the formula and update internal state every time sides changes
Apply the formula and return the value every time someone does a get operation on Area
Remember the point of getter and setter being functions is that they could contain logic to execute (to fully update internal state in setter, or to calculate the value of a read-only derived property).
More sophisticated patterns exist if performance of the area calculation were very worrisome, but I wouldn't get into that at this point.
As for determining if the triangle is right... if it is, which side must be the hypotenuse? What relationship do you know between the length of the hypotenuse and the lengths of the other sides, if the triangle is right?
using System;
namespace ConsoleApp
{
class Program
{
static void Main()
{
var t = new Triangle(2, 3, 5);
//var Triangle = new Triangle(2); // won't compile as no Triangle constructor can be found that takes 1 integer
//var Triangle = new Triangle(2, 3, 5, 7); // won't compile as no Triangle constructor can be found that takes 4 integers
//var Triangle = new Triangle(2, -3, 5); // won't compile as the 2nd value is negative - and we've asked for unsigned for all 3 values
Console.WriteLine("The triangle ({0}, {1}, {2}) has an area of {3}.", t.A, t.B, t.C, t.area());
Console.ReadKey();
}
}
public class Triangle
{
public uint A { get; set; }
public uint B { get; set; }
public uint C { get; set; }
public Triangle(uint a, uint b, uint c)
{
this.A = a;
this.B = b;
this.C = c;
}
public uint area()
{
return A * B * C; // this needs fixing ...
}
}
}
Isn't this roughly what you are trying to achieve with your Triangle class - a way of stopping it being used incorrectly with too few or incorrect types of arguments. This one only allows 3 positive (uint) integers. Nothing else will comple - which is what you want. Sorry if I have misunderstood.
I have mini class, which I save in List. Now I must delete in list, lines where numRect = -1. How can I do it?
double pointX, pointY;
int numRect;
public PointsSelectedSources(double x, double y, int numRect)
{
pointX = x;
pointY = y;
this.numRect = numRect;
}
public int NumRect
{
get
{
return numRect;
}
set
{
numRect = value;
}
}
Let miniClassList be the List then you can do something like the following to remove all objects from that list which are having numRect = -1;
miniClassList.RemoveAll(x=> x.numRect == -1);
Working example for more clarification
I got a question about constructors for my Windows Forms Application. First of all I want to say that I am new to programming.
The thing is this. I am making a constructor in another class that should hold different parameter values. In this case it should be int X, int Y, int Length, int Height. What I want to do here is to make the X, Y, Length and Height all random for my picturebox. I send code down below:
class Rechthoekcs
{
Random random = new Random();
public int Xas
{
get;
set;
}
public int Yas
{
get;
set;
}
public int Lengte
{
get;
set;
}
public int Breedte
{
get;
set;
}
public Rechthoekcs(int x, int y, int lengte, int breedte)
{
this.Xas = x;
this.Yas = y;
this.Lengte = lengte;
this.Breedte = breedte;
x = random.Next(x);
y = random.Next(y);
lengte = random.Next(lengte);
breedte = random.Next(breedte);
}
From my Form1 I want to call this class/constructor
But it gives me an error. It says "Does not contain a constructor with 0 arguments" and I know that because I typed the x, y, length and width. But I cannot add just the variables from the other class into the new parameter. I really don't get it. I find constructors very hard. I never know what parameters I should give with it...
I send the code down below from my Form1:
public partial class Form1 : Form
{
Rechthoekcs Rechthoek = new Rechthoekcs(.......);
public Form1()
{
InitializeComponent();
}
It it really frustrating for me. I tried looking up on the web and books and such but all the explanation about which parameters should be given for a constructor is not clear to me. Could someone explain this? Not understanding it drives me insane. Plus I am getting often stuck at these points..
public Rechthoekcs(int x, int y, int lengte, int breedte)
{
this.Xas = x;
this.Yas = y;
this.Lengte = lengte;
this.Breedte = breedte;
x = random.Next(x);
y = random.Next(y);
lengte = random.Next(lengte);
breedte = random.Next(breedte);
}
You are assigning the values of the parameters to your private Data Members before you do anything "randomizing" about them. You are simply changing the values of the parameters in your constructor without assigning them. Swap the order you do them in.
public Rechthoekcs(int x, int y, int lengte, int breedte)
{
x = random.Next(x);
y = random.Next(y);
lengte = random.Next(lengte);
breedte = random.Next(breedte);
this.Xas = x;
this.Yas = y;
this.Lengte = lengte;
this.Breedte = breedte;
}
Now you have successfully randomized your values that will be set to your data member variables ASSUMING that those variable you put in there actually exist, which they should. Better/more modular code would do this randomization where you create your object.
Ex: Object foo = new Object(new Random, new Random, new Random, new Random)
Not the answer OP is looking for, but this is how you solve "I don't yet know the value but I need to construct object anyway" version of the question.
You can delay requesting the data by passing Func<T> for each parameter instead of just T assuming the values actually will be available by the time they needed:
class UseDelayedValues
{
Func<int> x;
public UseDelayedValues(Func<int> x)
{
this.x = x;
}
public UseWithX(int other)
{
return other + x();
}
}
int value = 0;
var r = new UseDelayedValues(() => value);
value = 42;// get some value
Console.WriteLine(r.UseDelayedValues(1));
var delayedFromTextbox = new UseDelayedValues(() => int.Parse(textBox1.Value));
Lines using the UseDelayedValues can be spread over time. I.e. instance constructed in constructor, but value used only when form is shown by button click.