Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2004, 10:28 AM
Registered User
 
Join Date: Oct 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default getter and setters (" Properties problem")

 got to print out a square shape on console. The class square should contain two constructors and an instance property Side that has get and set accessors for private data. The first constructor is a default constructor and takes no arguments and another takes a side length as a value.

Please assist me as i am new to this properties concept and i want to get the problem done.

Thanks

 
Old October 11th, 2004, 12:10 PM
Friend of Wrox
 
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

public class Square
{
    private int m_Side; // m_Side is a member variable with the value of side
    public Square() // Default Constructor
    {
      m_Side = 0;
    }
    public Square(int SideValue) // Constructor which takes side as argument
    {
      m_Side = SideValue;
    }
    public int Side
    {
      get
        {
          return m_Side;
        }
      set
        {
          if(value>0)
            m_Side = value;
          else
            m_Side = 0;
        }
    }
}

Hope this helps!



It is not how much we do,
but how much love we put in the doing.

-Mother Theresa
 
Old October 11th, 2004, 04:44 PM
Registered User
 
Join Date: Oct 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now the next step i wanted to do is write a test application to print a asterix of square on the console by basically passing the refernece of the square functions we have defined earlier in our previous source code..

How i can do that .. Plzz help

Thanks in advance

 
Old October 11th, 2004, 08:06 PM
Registered User
 
Join Date: Oct 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the way i am going but couldnt get the solution ..

Hey ,

I got to print out a square on a console. In which class Square should contain two constructors and an instance property, Side that has a get and set accessors for private data. The first constructor that takes no arguments and another takes a side length as a value.
Then define a test application to test the Square functions which are been defined.

The way i am going through this problem is :

using System;

namespace ConsoleApplication2
{
    public class Square
    {
        public int m_Side; // m_Side is a member variable with the value of side
        public Square() // Default Constructor
        {
            m_Side = 0;
        }
        public Square(int SideValue) // Constructor which takes side as argument
        {
            m_Side = SideValue;
        }
        public int Side
        {
            get
            {
                return m_Side;
            }
            set
            {
                if(value>0)
                    m_Side = value;
                else
                    m_Side = 0;
            }
        }
    }

}

The test appliation i define is :

using System;

namespace ConsoleApplication2
{
    /// <summary>
    /// Summary description for SquareTest.
    /// </summary>
    public class SquareTest
    {

        static void Main ()
        {
            Console.WriteLine( "Enter Side Value ");

            Square S1= new Square();
            S1.m_Side=Int32.Parse( Console.ReadLine());
            if ( S1.m_Side>0)
            {
                for ( int S1=0; S1<m_Side; S1++)
                {
                  for ( int S1=0; S<m_Side; S1++)
                    {
                        System.Console.WriteLine +="*";
                    }
                System.Console.WriteLine +="\n";
                }
            }
        }
    }
}



The test application doesnt works as there are loads of errors..

Please update me with a solution as i am new to C# and couldn't figure it out as how to get through with this problem.


Cheers!!




 
Old October 13th, 2004, 06:13 AM
Friend of Wrox
 
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Full program is given below which will draw a square on Console based on size of side.

using System;

namespace ConsoleApplication2
{
    public class Square
    {
        private int m_Side; // m_Side is a member variable with the value of side
        public Square() // Default Constructor
        {
            m_Side = 0;
        }
        public Square(int SideValue) // Constructor which takes side as argument
        {
            m_Side = SideValue;
        }
        public int Side
        {
            get
            {
                return m_Side;
            }
            set
            {
                if(value>0)
                    m_Side = value;
                else
                    m_Side = 0;
            }
        }

        public void DrawSqure()
        {
            if (m_Side>0)
            {
                for ( int S1=0; S1<m_Side; S1++)
                {
                    for ( int S2=0; S2<m_Side; S2++)
                    {
                        System.Console.Write("*");
                    }
                    System.Console.WriteLine("");
                }
            }
        }


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine( "Enter Side Value ");
            Square s1 = new Square ();
            int side =Int32.Parse( Console.ReadLine());
            s1.Side = side;
            s1.DrawSqure ();
            Console.ReadLine ();
        }
    }
}



It is not how much we do,
but how much love we put in the doing.

-Mother Theresa





Similar Threads
Thread Thread Starter Forum Replies Last Post
control properties and methods access problem erictamlam C# 2005 8 April 16th, 2007 02:23 PM
having problem Using Indexed properties with Multi kris_rathi79 Struts 0 June 27th, 2006 07:56 AM
Problem in accessing the UserControl properties anujrathi ASP.NET 1.0 and 1.1 Professional 1 June 8th, 2006 02:13 PM
Problem in accessing the usercontol properties anujrathi ASP.NET 1.0 and 1.1 Basics 1 June 8th, 2006 02:09 PM
Problem setting request properties Einherj J2EE 0 January 22nd, 2006 11:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.