C# Constructor breaks due to another constructor - c#

I have a C# point class with multiple constructors in a library (This is a different class than a System.Drawing.Point in .NET).
Public class Point {
public float X;
public float Y;
public float Z;
//Constructor 1
public Point(float x, float y, float z) {
this.X = x;
this.Y = y;
this.Z = z;
}
//Constructor 2
public Point(Point point) {
this.X = point.X;
this.Y = point.Y;
this.Z = point.Z;
}
//Constructor 3
public Point(PointF point) {
this.X = point.X;
this.Y = point.Y;
this.Z = 0;
}
//Constructor 4
public Point(System.Drawing.Point point) {
this.X = point.X;
this.Y = point.Y;
this.Z = 0;
}
}
When I am trying to create a new Point object using the constructor that takes in float as parameters, everything works fine. When I want to create a new Point using an existing Point object (Constructor 2), I receive an error saying that I need to reference the System.Drawing assembly. I am guessing this is because of constructors 3 and 4 since they take a System.Drawing.Point and a PointF as an argument, but I don't see why they should pose an issue, since the constructor I am trying to use is completely unrelated to them, and constructor 1 works fine when called. How do I get around this issue? Thanks!

There is a workaround for this, if you can accept it.
Add the following to your Point class:
public static Point Clone(Point p)
{
return new Point(p);
}
Then in the code that is currently failing to compile, do this instead:
Point p = new Point(0, 0, 0);
Point q = Point.Clone(p);
(Of course, you don't have to call the method Clone() - call it Copy or CopyCtor or whatever you like.)
As to why the compiler insists that you must include a reference to an assembly defining types that you're not even using, see this answer.
Also see this question.

Related

Why does all members of a class show error 'CS0103'

I'm testing a piece of code in c# and vs2022, but I encounter some problems. I try to track the value of some members in a class, but the VS2022 shows error CS0103.
So I would like to know why VS2022 can't show their values because they are certainly in this context.
class Program
{
static void Main(string[] args)
{
ProtoType p = new ProtoType(100, 200);
p.x = 101;
p.y = 20;
int cnt = p.list.Count;
Console.ReadLine();
}
}
class ProtoType
{
public int x = 0;
public int y = 0;
public List<string> list = new List<string>();
public ProtoType(int x, int y)
{
Console.WriteLine("Execute Constructor ProtoType()");
this.x = x;
this.y = y;
}
public ProtoType Clone()
{
Console.WriteLine("Execute ProtoType.Clone()");
return (ProtoType)this.MemberwiseClone();
}
}
Because x, y and list are not variables in this scope. they are members of the class ProtoType. you need to watch for p.x, p.y and p.list in place of the x, y, list.

Initializing obj with/without default values

In c#:
public class SomeClass
{
int x;
int y;
SomeClass (int x, int y)
{
this.x = x;
this.y = y;
}
}
Is there easy way to make new SomeClass without setting x and y instead to
have default values for them and if I set values to set them else to have the
default values?
Sure, with C#6 you can use auto-implemented properties:
public class SomeClass
{
public int X { get; } = 123;
public int Y { get; } = 456;
public SomeClass(){ }
public SomeClass(int x, int y)
{
this.X = x;
this.Y = y;
}
}
Of course you need a parameterless constructor.
If you instead mean default values of the type, that is done automatically(0 for numerical types).
Sure...
new SomeClass(default(int), default(int))
Or, more simply:
new SomeClass(0, 0)
The default value for int is always 0. So even if you define it with a parameterless constructor:
public SomeClass() { }
Those int class members would still default to 0.
You need to define a parameterless constructor:
public class SomeClass
{
int x;
int y;
public SomeClass {}
public SomeClass (int x, int y)
{
this.x = x;
this.y = y;
}
}
When you create an object like:
var someClass = new SomeClass();
both x and y would be initialized using their default values, which is 0.
If you don't want to do so, you could handle this by passing to the constructor that you have already declared the default values of x and y, as already David has pointed out.
Use a parameterless constructor.
Since the instances have to be created somehow using a new keyword, you can use a parameterless constructor inside your class.
public SomeClass ()
{
x = 0;
y = 0;
}
A default constructor would do that automatically.
You can use optional parameters in the constructor.
Read more on named and optional parameters.
public class SomeClass
{
// You can also write this as
// public SomeClass(int x=default(int), int y=default(int)) if you do
// not want to hardcode default parameter value.
public SomeClass(int x=0, int y=0)
{
this.X = x;
this.Y = y;
}
}
You can call it as
void Main()
{
SomeClass a = new SomeClass();
SomeClass b = new SomeClass(1);
SomeClass c = new SomeClass(2,4);
}

Calling a parent class from a subclass

class experiment
{
int xCoord = 0;
int yCoord = 0;
public experiment(int x, int y) {
this.xCoord = x;
this.yCoord = y;
}
}
class result :experiment{
int zCoord = 0;
public result(int z) : base(x,y)
{
this.zCoord = z;
}
}
Can anyone help me solve this simple problem. I'm having an error base(x,y) it says the name 'x' does not exists in the current context and also goes for the y.
x and y are local fields to class experiment they are not visible in inherited class, you may call the base constructor with default values like:
public result(int z) : base(0,0)
Also please follow General Naming Conventions from Microsoft, so the class names begins with upper case character.
EDIT:
It would be better if your child class has a constructor to receive parameter x and y, and the it calls the base class constructor with those values like:
public result(int x, int y, int z) : base(x,y)
{
this.zCoord = z;
}
There is no x,y in constructor of result class.
You pass to your constructor z but tell your base constructor to recieve x and y. Though there are no x and y at that time.
Try this:
public result(int z, int x, int y) : base(x,y)
{
this.zCoord = z;
}
Or set fix values (no variables):
public result(int z) : base(0, 0)
{
this.zCoord = z;
}

C# - Storing a struct inside a struct of same type

Ok,
It is not possible to store a struct instance inside a struct of the same type. So can anyone help me find a workaround please?
I need to store a vector3 inside a vector3 like this:
public struct Vector3{
float x,y,z;
Vector3 normalized;
}
Obviously, it creates an endless cycle as one would create a new one that creates a new one and so on...
So how would one do that? I would need my normalized to be a Vector3 since it needs to be recognized as such and cannot be any other naming.
Finally, I know this can be achieved with classes but I would not want.
Thanks
Well, a struct is a value type. Declaring a recursive struct would create an infinitely big struct! A workaround would be to declare it as class instead. But here I would simply declare Normalized as a property.
public struct Vector3 {
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public float X { get; private set; }
public float Y { get; private set; }
public float Z { get; private set; }
public float Length {
get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z); }
}
Vector3 Normalized {
get {
float l = Length;
return new Vector3(X / l, Y / l, Z / l);
}
}
}
You cannot store a struct of type X inside a struct of type X. However, you probably don't want to do this anyway, because in general, structs (and classes, for that matter) should only store the data they need to be complete. Instead, you can have a function that builds and returns the 'normalized' version of the struct:
public struct Vector3
{
float x,y,z;
public Vector3 Normalized
{
get
{
... build the normalized struct and return it ...
}
}
}

Manipulating variables in structs in C#

I'm messing around trying to learn C# in Visual Studio. I have only basic coding knowledge, and I bought C# 5.0 in a nutshell. I'm loving the book, and trying to make mini programs out of everything I read to help make it stick. I thought structs were something simple, but for whatever reason I just can't get a struct to work.
So here's a brief and ultra basic example from the book.
public struct Point {
int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
}
Point p1 = new Point();
Point p2 = new Point(1, 1);
It works fine. But now say I want to manipulate the x and y variables in p1 or p2. I've tried so much, and I can't get it to work.
public struct Point {
public int x;
}
Point p1 = new Point();
p1.x = 10;
This won't work. When I try to set p1.x to 10, I get an error. It says p1 is a "field" but is used like a "type."
There's probably something simple I'm missing, but my patience for trial and error has run out. So what am I doing wrong? I understand the basic concept of why a struct is useful, but I need to be able to actually use it once I make it!
Tono Nam's answer is correct but incomplete.
To set the values of x and y the way you want to, you will also need to set the correct access modifier for x and y e.g.
class Program
{
public struct Point
{
public int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
}
static void Main(string[] args)
{
var p1 = new Point();
var p2 = new Point(1, 1);
p1.x = 1;
p1.y = 1;
}
}
You'll want to use public to access x and y from any class.
You'll want to use internal if you only want to access x and y from within the same class where the struct Point is defined.
class Program
{
public struct Point
{
int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
}
static void Main(string[] args)
{
Point p1 = new Point();
Point p2 = new Point(1, 1);
}
}
Just like #pst mentioned. p1 and p2 need to be inside a method. In this case they are inside the main method.

Categories

Resources