Value Types and Reference Types

Ecal

New member
Joined
Feb 22, 2016
Messages
3
Programming Experience
1-3
[XCODE]
using
System;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
System.Linq;
using
System.Text;
 
namespace
ValueAndReferenceTypes
{

class Program
{

static void Main(string[] args)
{
}

struct Point
{

//Fields of the structure.

public int X;

public int Y;

//Add 1 to the (X,Y) position.

public void Increment()
{
X++; Y++;
}

// Subtract 1 from the (X,Y) position.

public void Decrement()
{
X--; Y--;
}

//Display the current position.

public void Display()
{

Console.WriteLine("X = {0}, Y = {1}", X, Y);
}
}

struct Point2
{

//Fields of the structure.

public int X;

public int Y;

// A custom constructor.

public Point2(int XPos, int YPos)
{
X = XPos;
Y = YPos;
}

//Display the current position.

public void Display()
{

Console.WriteLine("X = {0}, Y = {1}", X, Y);
}
}

static void ValueTypeAssignment()
{

Console.WriteLine("Assignment value types \n");

Point p1 = new Point(10, 10);

Point p2 = p1;

//Print both points
p1.Display();
p2.Display();

//Change p1.X and print again. p2.X is not changed.
p1.X = 100;

Console.WriteLine("\n=> Changed p1.X\n");
p1.Display();
p2.Display();
}
}
}

[/XCODE]

Not sure what I am doing Wrong but on line 66 I have an error that comes up that says:

'ValueAndReferenceTypes.Program.Point' does not contain a constructor that takes 2 arguments

Is there something I am over looking other than I am not calling it method in my main?
 
Firstly, you have made your code harder to read. When you use xcode tags you must specify an option, i.e. how to format the code. On this forum, only c# is supported as an option. You then must paste plain text inside the tags.

As for the issue, it's spelled out in the error message. Can you see a constructor with two parameters in your Point type? I can see one in your Point2 type but that's a different type so it doesn't help.
 
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
 
namespace ValueAndReferenceTypes
{

class Program
{
static void Main(string[] args)
{
}

struct Point
{
//Fields of the structure.
public int X;
public int Y;

//Add 1 to the (X,Y) position.
public void Increment()
{
X++; Y++;
}
// Subtract 1 from the (X,Y) position.
public void Decrement()
{
X--; Y--;​
}

//Display the current position.

public void Display()
{
Console.WriteLine("X = {0}, Y = {1}", X, Y);
}
}​

structPoint2
{
//Fields of the structure.
public int X;
public int Y;

// A custom constructor.
public Point2(int XPos, int YPos)
{
X = XPos;​
Y = YPos;
}

//Display the current position.
public void Display()
{
Console.WriteLine("X = {0}, Y = {1}", X, Y);
}
}
static void ValueTypeAssignment()
{
Console.WriteLine("Assignment value types \n");
Point p1 = new Point(10, 10);
Point p2 = p1;

//Print both points
p1.Display();
p2.Display();

//Change p1.X and print again. p2.X is not changed.
p1.X = 100;

Console.WriteLine("\n=> Changed p1.X\n");
p1.Display();
p2.Display();
}​
}
}

I hope this is more readable not sure How to use the C# Icon but I just pasted it in here again. Think I got it. The code was pointing to Point instead of Point2

Out Put console

Assignment value types
X = 10, Y = 10
X = 10, Y = 10
=> Changed p1.X
X = 100, Y = 10
X = 10, Y = 10

 
Back
Top Bottom