1

I'm a beginner going through the examples in a book which are somewhat hard to follow. I'm going through the book and compiling the code to get a look at what it does. I'm in a section of structures, specifically structure variables. The following code has the error point does not take two arguments. Can someone help me spot what's missing/incorrect here? Thanks.

    using System;

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

            // Create an initial Point.
            Point myPoint;
Point p1 = new Point(10, 10);
            myPoint.X = 349;
            myPoint.Y = 76;
            myPoint.Display();
            // Adjust the X and Y values.
            myPoint.Increment();
            myPoint.Display();
            Console.ReadLine();
        }
        // Assigning two intrinsic value types results in
        // two independent variables on the stack.
        static void ValueTypeAssignment()
        {
            Console.WriteLine("Assigning 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();
        }
    }


    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);
        }

    }
1
  • 3
    You don't have a constructor for Point that takes any arguments. Commented Sep 11, 2012 at 12:56

6 Answers 6

7

You need to add a two-parameter constructor to Point, because you call it with arguments (10, 10).

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

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

Alternatively you can construct it with the built-in nullary (no-parameters) constructor and then set the properties:

Point myPoint = new Point();
myPoint.X = 349;
myPoint.Y = 76;

A shorthand for that is:

Point myPoint = new Point { X = 349, Y = 76 };

Or even shorter:

var myPoint = new Point { X = 349, Y = 76 };

Finally, it is generally good practise to make structs immutable: once constructed it should be impossible to modify their contents. This helps to avoid a lot of other pitfalls in the language.

Sign up to request clarification or add additional context in comments.

1 Comment

spot on. thanks :) +1 for going the extra mile and showing equivalencies of different declarations.
4

Instead of constructing a point using a constructor with two arguments, instantiate the properties as part of the same call. For example:

Point p = new Point { X = 1, Y = 2 };

You gain the advantage of single line construction without having to write additional code.

1 Comment

@DanielEarwicker Thanks. I typed the code too quickly without checking it. Code has been corrected.
0

You have no constructor on Point that takes two parameters.

You need:

public Point(int x, int y){ // assign these to your properties. }

Comments

0

You are missing a constructor for the Point struct:

public Point(int x, int y)
{
    X = x;
    Y = y;
}

Comments

0

Dont you have to initialize the point in the main?

 Point myPoint = new Point();

Comments

0

You dont have a constructor with two parameters defined. Your point struct should be like this to be valid

public Point(int x, int y)
{
    X = x;
    Y = y;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.