4 Replies - 1422 Views - Last Post: 27 April 2011 - 01:16 PM Rate Topic: -----

#1 d3rkn3ss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-April 11

Declaring an array of objects using a method and constructor?

Posted 27 April 2011 - 01:02 PM

Hello!

First with the code!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Squares
{
    class Square
    {
        private double area;
        private int sideLength;

        public double Area
        {
            get
            {
                return area;
            }

        }

        public int SideLength
        {
            get
            {
                return sideLength;
            }
        }
        public Square(int sideLength)
        {
            SquareArea();
        }
        private void SquareArea()
        {
            area = Math.Pow(sideLength, 2);
        }
    }
}

namespace Squares
{
    class DemoSquares
    {
        static void Main(string[] args)
        {
            Square[] Square1 = {new Square(1), new Square(2), new Square(3), new Square (4),
                                   new Square(5), new Square(6), new Square(7), new Square(8),
                                   new Square(9), new Square(10)};

            for (int x = 0; x < Square1.Length; ++x)
                Square1[x] = new Square();

            Console.WriteLine("The length of one side of the square is {0}", Square1);

            Console.ReadKey();
        }
    }
}



my errors include:

sideLength in my Square class is never assigned to.

"new Square();" in my for loop "Does not contain a constructor that takes 0 arguments"

I have tried overloading new Square(); with my public SideLength as follows: new Square(SideLength), but then it returns an error "SideLength does not exist in this context"

Any help on how I correctly define new Square(); in my Main() would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Declaring an array of objects using a method and constructor?

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Declaring an array of objects using a method and constructor?

Posted 27 April 2011 - 01:06 PM

this looks like C# to me, moved.
Was This Post Helpful? 1
  • +
  • -

#3 d3rkn3ss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-April 11

Re: Declaring an array of objects using a method and constructor?

Posted 27 April 2011 - 01:11 PM

View Postishkabible, on 27 April 2011 - 01:06 PM, said:

this looks like C# to me, moved.


My apologies, my brain is obviously fried.
Was This Post Helpful? 0
  • +
  • -

#4 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Declaring an array of objects using a method and constructor?

Posted 27 April 2011 - 01:11 PM

Hi,

Your Square class has one constructor. That constructor MUST take an 'int' as an argument.

This statement:

... new Square();



calls the constructor of the square class. However, remember, the constructor is expecting an 'int'. Therefore, you need to pass it an int. Like this, for example:

Square1[x] = new Square(6);


Just like you've done in your Square1 array :)

What int you pass in depends on the number of sides you want the Square object to have!

This post has been edited by [email protected]: 27 April 2011 - 01:18 PM

Was This Post Helpful? 1
  • +
  • -

#5 d3rkn3ss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-April 11

Re: Declaring an array of objects using a method and constructor?

Posted 27 April 2011 - 01:16 PM

View Post[email protected], on 27 April 2011 - 01:11 PM, said:

Hi,

Your Square class has one constructor. That constructor MUST take an 'int' as an argument.

This line:

... new Square();



calls the constructor of the square class. However, remember, the constructor is expecting an 'int'. Therefore, you need to pass it an int. Like this, for example:

Square1[i] = new Square(6);


Just like you've done in your Square1 array :)

What int you pass in depends on the number of sides you want the Square object to have!


Thank you! I'm just getting into constructors at the moment. Your help is most valuable!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1