How do I put my four shapes into the object array I have created? Using
shapeArray[0] = (1,2,3,4)
is all i can think to do and this is obviously incorrect...
struct Shapes
{
private int width;
private int height;
private int xAxis;
private int yAxis;
}
Shapes[] shapeArray = new Shapes[4];
You should add a new constructor for your Shape:
struct Shape
{
public Shape(int width, int height, int xAxis, int yAxis)
{
this.width = width;
this.height = height;
this.xAxis = xAxis;
this.yAxis = yAxis;
}
private int width;
private int height;
private int xAxis;
private int yAxis;
public int Width { get { return width; } }
public int Height { get { return height; } }
public int XAxis { get { return xAxis; } }
public int YAxis { get { return yAxis; } }
}
Then you can use that to create it:
Shape[] shapes = new Shape[]{
new Shape(1, 2, 3, 4),
new Shape(2, 4, 6, 8),
new Shape(1, 2, 3, 4),
new Shape(4, 3, 2, 1)
};
Since you didn't create a constructor for Shapes you'll have to set the properties explicitly
shapeArray[0] = new Shapes;
shapeArray[0].width = 1;
shapeArray[0].height = 2;
shapeArray[0].xAxis = 3;
shapeArray[0].yAxis = 4;
HOWEVER the proper way to do this (and avoid having a mutable struct) is to privatize the public fields and add a constructor to your struct:
public Shapes(int width, int height, int xAxis, int yAxis)
{
this.width = width;
this.height = height;
this.xAxis = xAxis;
this.yAxis = yAxis;
}
Then you would just use
shapeArray[0] = new Shapes(1, 2, 3, 4);
Related
I began setting up my system by taking the Grid system supplied by Pack publishing (). This is how it's initialized:
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 override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is Point)
{
Point p = obj as Point;
return this.X == p.X && this.Y == p.Y;
}
return false;
}
public override int GetHashCode()
{
unchecked
{
int hash = 6949;
hash = hash * 7907 + X.GetHashCode();
hash = hash * 7907 + Y.GetHashCode();
return hash;
}
}
public override string ToString()
{
return "P(" + this.X + ", " + this.Y + ")";
}
}
public enum CellType
{
Empty,
Road,
Structure,
SpecialStructure,
None
}
public class Grid
{
private CellType[,] _grid;
private int _width;
public int Width { get { return _width; } }
private int _height;
public int Height { get { return _height; } }
private List<Point> _roadList = new List<Point>();
private List<Point> _specialStructure = new List<Point>();
private List<Point> _houseStructure = new List<Point>();
public Grid(int width, int height)
{
_width = width;
_height = height;
_grid = new CellType[width, height];
}
public enum CellType
{
Empty,
Road,
Structure,
SpecialStructure,
None
}
public class Grid
{
private CellType[,] _grid;
private int _width;
public int Width { get { return _width; } }
private int _height;
public int Height { get { return _height; } }
private List<Point> _roadList = new List<Point>();
private List<Point> _specialStructure = new List<Point>();
private List<Point> _houseStructure = new List<Point>();
public Grid(int width, int height)
{
_width = width;
_height = height;
_grid = new CellType[width, height];
}
public void Update()
{
Console.Write("GRID IS RUNNING");
}
// Adding index operator to our Grid class so that we can use grid[][] to access specific cell from our grid.
public CellType this[int i, int j]
{
get
{
return _grid[i, j];
}
set
{
if (value == CellType.Road)
{
_roadList.Add(new Point(i, j));
}
if (value == CellType.SpecialStructure)
{
_specialStructure.Add(new Point(i, j));
}
if (value == CellType.Structure)
{
_houseStructure.Add(new Point(i, j));
}
_grid[i, j] = value;
}
}
After I initialized it, I called it from another function like this.
public int width, height;
Grid placementGrid;
private void Start()
{
placementGrid = new Grid(width, height);
Debug.Log(placementGrid.GetRandomRoadPoint());
}
It seems to initialize perfectly. But when I call the functions inside it, I'm returned a null value. I tried calling this function for example:
public Point GetRandomRoadPoint()
{
if (_roadList.Count == 0)
{
return null;
}
return _roadList[UnityEngine.Random.Range(0, _roadList.Count)];
}
I have no idea what I'm doing wrong. I'm basically trying to create a pedestrian AI system for my procedurally generated town.
I tried debugging several times, but I can't seem to find my way around it.
I am using the TPL library to parallelize a 2D grid operation. I have extracted a simple example from my actual code to illustrate what I am doing. I am getting the desired results I want and my computation times are sped up by the number of processors on my laptop (12).
I would love to get some advice or opinions on my code as far as how my properties are declared. Again, it works as expected, but wonder if the design could be better. Thank you in advance.
My simplified code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Gridding
{
public abstract class Base
{
/// <summary>
/// Values representing the mesh values where each node in the gridis assigned som value.
/// </summary>
public float[,] Values { get; set; }
public abstract void Compute();
}
public class Derived : Base
{
/// <summary>
/// Make the mesh readonly. Is this necessary?
/// </summary>
readonly Mesh MyMesh;
Derived(Mesh mesh)
{
MyMesh = mesh;
}
public override void Compute()
{
Values = new float[MyMesh.NX, MyMesh.NY];
double[] xn = MyMesh.GetXNodes();
double[] yn = MyMesh.GetYNodes();
/// Paralellize the threads along the columns of the grid using the Task Paralllel Library (TPL).
Parallel.For(0, MyMesh.NX, i =>
{
Run(i, xn, yn);
});
}
private void Run(int i, double[] xn, double[] yn)
{
/// Some long operation that parallelizes along the columns of a mesh/grid
double x = xn[i];
for (int j = 0; j < MyMesh.NY; j++)
{
/// Again, longer operation here
double y = yn[j];
double someValue = Math.Sqrt(x * y);
Values[i, j] = (float)someValue;
}
}
static void Main(string[] args)
{
int nx = 100;
int ny = 120;
double x0 = 0.0;
double y0 = 0.0;
double width = 100;
double height = 120;
Mesh mesh = new Mesh(nx, ny, x0, y0, width, height);
Base tplTest = new Derived(mesh);
tplTest.Compute();
float[,] values = tplTest.Values;
Console.Read();
}
/// <summary>
/// A simple North-South oriented grid.
/// </summary>
class Mesh
{
public int NX { get; } = 100;
public int NY { get; set; } = 150;
public double XOrigin { get; set; } = 0.0;
public double YOrigin { get; set; } = 0.0;
public double Width { get; set; } = 100.0;
public double Height { get; set; } = 150.0;
public double DX { get; }
public double DY { get; }
public Mesh(int nx, int ny, double xOrigin, double yOrigin, double width, double height)
{
NX = nx;
NY = ny;
XOrigin = xOrigin;
YOrigin = yOrigin;
Width = width;
Height = height;
DX = Width / (NX - 1);
DY = Height / (NY - 1);
}
public double[] GetYNodes()
{
double[] yNodeLocs = new double[NY];
for (int i = 0; i < NY; i++)
{
yNodeLocs[i] = YOrigin + i * DY;
}
return yNodeLocs;
}
public double[] GetXNodes()
{
double[] xNodeLocs = new double[NX];
for (int i = 0; i < NX; i++)
{
xNodeLocs[i] = XOrigin + i * DX;
}
return xNodeLocs;
}
}
}
}
With only 100 to 150 array elements, parallelism setup overhead would offset the gains. You could cache the result of GetXNodes() and GetYNodes() and invalidate the cached values when XOrigin, NX, YOrigin or NY are modified.
How can I call the parameter which is inside the method instance in the other class, as I want to make a calculation with the method and display it.
class Box
{
int width = 10;
int height = 15;
public int Area(int Area)
{
Area = width * height;
return Area;
}
public int Perimeter(int Para)
{
Para = 2 * (height + width);
return Para;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
static void Main(string[] args)
{
Box b = new Box();
b.Area(Area);
b.Perimeter(Para);
Console.ReadLine();
}
It is giving me en error on b.Area(Area); and b.Perimeter(Para);
Maybe you wanted to do this:
class Program
{
static void Main(string[] args)
{
Box box = new Box(10, 15);
Console.WriteLine("Area is: " + box.CalculateArea());
Console.WriteLine("Perimeter is: " + box.CalculatePerimeter());
Console.ReadLine();
}
}
public class Box
{
public int Width { get; set; }
public int Height { get; set; }
public Box(int width, int height)
{
Width = width;
Height = height;
}
public int CalculateArea()
{
return Width * Height;
}
public int CalculatePerimeter()
{
return 2 * (Width + Height);
}
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 4 years ago.
So in a few words i'm getting an error whenever i run this
System.NullReferenceException
Ball.boxContent.get returned null
i get this error on this line
box1.Content.Add(Myballs[5]);
what I'm doing is creating balls of different colors,weight and material
adding them into a list called MyBalls
then
adding them from that list into a box(that has a property that is a list called Content)
after this is done I'm supposed to call some of the box function and calculate the weight and print what is inside. Not yet reached this stage as i cannot
any help would be greatly appreciated
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Ball
{
class Ball
{
private string color;
private string material;
private double weight;
public string Color { get; set; }
public string Material { get; set; }
public double Weight { get; set; }
public Ball() { }
public Ball(string color, double weight, string material)
{
this.Color = color;
this.Material = material;
this.Weight = weight;
}
public bool changecolorbymorethanweight(string c, double w)
{
if (Weight >= w)
{
Color = c;
return true;
}
else return false;
}
public void printall()
{
Console.WriteLine("Η μπάλα είναι χρώματος {0}\nυλικού {1}\nκαι βάρους {2}", Color, Material, Weight);
}
}
-
class Box
{
private double height;
private double width;
private double length;
private string material;
public List<Ball> content = new List<Ball>();
private double weight;
public double Height { get; set; }
public double Width { get; set; }
public double Length { get; set; }
public string Material { get; set; }
public List<Ball> Content { get; set; }
public double Weight
{
get
{
double varos = 0;
for (int i = 0; i < content.Count; i++)
{
varos = varos + content[i].Weight;
}
weight = varos;
return weight;
}
set
{
Weight = weight;
}
}
public void removefirst3()
{
Content.RemoveAt(0);
Content.RemoveAt(1);
Content.RemoveAt(2);
}
public void removeall()
{
Content.Clear();
}
public void removeallbycolor(string rbc)
{
for (int i = 0; i < Content.Count; i++)
if (Content[i].Color == rbc)
Content.RemoveAt(i);
}
public int getnumberbycolor(string gnbc)
{
int counter = 0;
for (int i = 0; i < Content.Count; i++)
if (Content[i].Color == gnbc)
counter++;
return counter;
}
public bool removeallmorethanbyweight(double rbwb)
{
bool result = false;
for (int i = 0; i < Content.Count; i++)
if (Content[i].Weight >= rbwb)
{
Content.RemoveAt(i);
result = true;
}
return result;
}
public bool removealllessthanbyweight(double rbws)
{
bool result = false;
for (int i = 0; i < Content.Count; i++)
if (Content[i].Weight >= rbws)
{
Content.RemoveAt(i);
result = true;
}
return result;
}
public bool removeallbymaterial(string rabm)
{
bool result = false;
for (int i = 0; i < Content.Count; i++)
if (Content[i].Material == rabm)
{
Content.RemoveAt(i);
result = true;
}
return result;
}
public void changecolorbyposition(int p, string c)
{
for (int i = 0; i < Content.Count; i++)
if (i == p)
Content[i].Color = c;
}
public void printall()
{
Console.WriteLine("Tο κουτί μας έχει \n{0} ύψος\n{1} πλάτος\n{2} μήκος\nείναι φτιαγμένο από {3}\nτο βάρος του είναι {4}\n και μέσα του έχει:\n{5}", Height, Width, Length, Material, Weight, Content);
}
public Box() { }
public Box(double length, double width, double height, string material, double weight, List<Ball> content)
{
this.Length = length;
this.Width = width;
this.Height = height;
this.Material = material;
this.Weight = weight;
this.Content = content;
}
public Box(double length, double width, double height, string material)
{
this.Length = length;
this.Width = width;
this.Height = height;
this.Material = material;
}
}
-
class Program
{
static void Main(string[] args)
{
Box box1 = new Box(1.2, 1.3, 1.8, "χάρτινο");
Box box2 = new Box(1.2, 2.3, 2.5, "ξύλινο");
Ball b1 = new Ball("Red", 2.5, "μεταλλική");
Ball b2 = new Ball("Red", 2.5, "μεταλλική");
Ball b3 = new Ball("Red", 2.5, "μεταλλική");
Ball b4 = new Ball("Red", 1.5, "μεταλλική");
Ball b5 = new Ball("Red", 1.5, "μεταλλική");
Ball b6 = new Ball("Black", 0.5, "πλαστική");
Ball b7 = new Ball("Black", 0.5, "πλαστική");
Ball b8 = new Ball("Black", 0.5, "πλαστική");
Ball b9 = new Ball("Black", 0.5, "πλαστική");
Ball b10 = new Ball("Black", 0.5, "πλαστική");
Ball b11 = new Ball("Άσπρο", 1.1, "λαστιχένια");
Ball b12 = new Ball("Άσπρο", 1.1, "λαστιχένια");
Ball b13 = new Ball("Άσπρο", 1.1, "λαστιχένια");
Ball b14 = new Ball("Άσπρο", 1.1, "λαστιχένια");
Ball b15 = new Ball("Άσπρο", 1.1, "λαστιχένια");
List<Ball> Myballs = new List<Ball>() { b1, b2 };
Myballs.Capacity = 15;
Myballs.Add(b1);
Myballs.Add(b2);
Myballs.Add(b3);
Myballs.Add(b4);
Myballs.Add(b5);
Myballs.Add(b6);
Myballs.Add(b7);
Myballs.Add(b8);
Myballs.Add(b9);
Myballs.Add(b10);
Myballs.Add(b11);
Myballs.Add(b12);
Myballs.Add(b13);
Myballs.Add(b14);
Myballs.Add(b15);
box1.Content.Add(Myballs[5]);
box1.Content.Add(Myballs[6]);
box1.Content.Add(Myballs[7]);
box1.Content.Add(Myballs[8]);
box1.Content.Add(Myballs[9]);
box1.Content.Add(Myballs[10]);
box1.Content.Add(Myballs[11]);
box1.Content.Add(Myballs[12]);
box1.Content.Add(Myballs[13]);
box1.Content.Add(Myballs[14]);
box2.Content.Add(Myballs[0]);
box2.Content.Add(Myballs[1]);
box2.Content.Add(Myballs[2]);
box2.Content.Add(Myballs[3]);
box2.Content.Add(Myballs[4]);
Console.ReadKey();
}
}
}
Initialize the Content property, either in the Box class:
public List<Ball> Content { get; set; } = new List<Ball>();
...or in the Main method before you add any balls:
box1.Content = new List<Ball>();
box1.Content.Add(Myballs[5]);
...
I have the following piece of code I wish to do without using the Bitmap Class
I am not sure where to start can someone give me some code samples or examples to work from
public class PgmImage
{
public int Width { get; set; }
public int Height { get; set; }
public int MaxVal { get; set; }
public byte[][] Pixels { get; private set; }
public PgmImage(int width, int height, int maxVal,
byte[][] pixels)
{
this.Width = width;
this.Height = height;
this.MaxVal = maxVal;
this.Pixels = pixels;
}
}
private static Bitmap MakeBitmap(PgmImage pgmImage, int mag)
{
int width = pgmImage.Width * mag;
int height = pgmImage.Height * mag;
Bitmap result = new Bitmap(width, height);
Graphics gr = Graphics.FromImage(result);
for (int i = 0; i < pgmImage.Height; ++i)
{
for (int j = 0; j < pgmImage.Width; ++j)
{
int pixelColor = pgmImage.Pixels[i][j];
Color c = Color.FromArgb(pixelColor, pixelColor, pixelColor);
SolidBrush sb = new SolidBrush(c);
gr.FillRectangle(sb, j * mag, i * mag, mag, mag);
}
}
return result;
}
The reason behind this is that NetStandard doesn't have the Bitmap class to use