Demonstrate the use of a nested class to contain data : Class Definition « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » Class DefinitionScreenshots 
Demonstrate the use of a nested class to contain data
Demonstrate the use of a nested class to contain data

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// Nested.cs -- demonstrate the use of a nested class to contain data
//
//              Compile this program with the following command line
//                  C:>csc Nested.cs
//
namespace nsReadOnly
{
    using System;
    
    public class Nested
    {
        static double DegreeFactor = 1;
        static double MilFactor = 0.05625;
        static double RadianFactor = 57.29578;
        static public void Main ()
        {
            double angle = 90;
            double radius = 50;
            
            // Declare an instance of the nested class
            clsArea.clsData data = new clsArea.clsData (angle, radius,
                                                        DegreeFactor);
            clsArea InDegrees = new clsArea (data);

            // Change the values to mils
            data.Factor = MilFactor;
            data.Angle = angle * 17.77778;
            clsArea InMils = new clsArea (data);

            // Change the values to radians
            data.Angle = angle / 57.29578;
            data.Factor = RadianFactor;
            clsArea InRadians = new clsArea (data);

            Console.WriteLine ("Area of pie of {0,0:F3} degrees is {1,0:F1}",
                               InDegrees.Data.Angle, InDegrees.Area);
            Console.WriteLine ("Area of pie of {0,0:F3} radians is {1,0:F1}",
                               InRadians.Data.Angle, InRadians.Area);
            Console.WriteLine ("Area of pie of {0,0:F3} mils is {1,0:F1}",
                               InMils.Data.Angle, InMils.Area);
        }
    }
    class clsArea
    {
        public class clsData : ICloneable
        {
            public clsData (double angle, double radius, double factor)
            {
                m_Angle = angle;
                m_Radius = radius;
                m_Factor = factor / 57.29578;
            }
            public double Angle
            {
                get {return(m_Angle);}
                set {m_Angle = value;}
            }

            public double Radius
            {
                get {return(m_Radius);}
                set {m_Radius = value;}
            }
            public double Factor
            {
                get {return(m_Factor);}
                set {m_Factor = value / 57.29578;}
            }
            private double m_Angle = 0;
            private double m_Radius = 0;
            private double m_Factor = 1;
            public object Clone ()
            {
                clsData clone = new clsData (m_Angle, m_Radius,
                                             m_Factor * 57.29578);
                return (clone);
            }
        }

        public clsArea (clsData data)
        {
            // Clone the data object to get a copy for ourselves
            m_Data = (clsDatadata.Clone();
        }

        public clsData Data
        {
            get {return (m_Data);}
        }

        private clsData m_Data;
        private const double pi = 3.14159;
        private const double radian = 57.29578;

        public double Area
        {
            get
            {
               return (m_Data.Radius * m_Data.Radius * pi
                       * m_Data.Angle * m_Data.Factor /  (* pi));
            }
        }
    }
}


           
       
Related examples in the same category
1. A simple inventory exampleA simple inventory example
2. Declaring and Defining Classes
3. Declaring Class Instances
4. Assign value to classAssign value to class
5. Declare class and use itDeclare class and use it
6. Illustrates how to declare classes, object references, and create objectsIllustrates how to declare classes, object references, and create objects
7. Illustrates how to assign default values to fields using initializersIllustrates how to assign default values to fields using initializers
8. illustrates how to use a 'has a' relationshipillustrates how to use a 'has a' relationship
9. Illustrates nested classesIllustrates nested classes
10. Multiple constructors in a class definitionMultiple constructors in a class definition
11. Show name hiding in a derived classShow name hiding in a derived class
12. Illustrates hidingIllustrates hiding
13. Variable in and out a classVariable in and out a class
14. Create classCreate class
15. A program that uses the Building classA program that uses the Building class
16. This program creates two Building objectsThis program creates two Building objects
17. Return an objectReturn an object
18. Uses a class from Example16_3a.cs
19. A Simple C# ClassA Simple C# Class
w__w__w__.___j___a___va2___s___.c__o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.