Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Import Reflection Example (C#)

[C#]
/* COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
 * All rights reserved.
 *
 * Use of this copyright notice is precautionary only, and does not imply
 * publication or disclosure. The content of this work contains confidential
 * and proprietary information of Simplexar Software Limited. Any duplication,
 * modification, distribution, or disclosure in any form, in whole, or in part,
 * is strictly prohibited without express prior written permission.
 */

// Get namespaces.
using System;
using Simplexar.Statsar;

// Start namespace.
namespace Simplexar.Examples.Statsar
{
    /// <summary>
    /// This example loads an array of objects using reflection,
    /// and illustrates how to bind to a custom object model.
    /// </summary>
    public static class Example
    {
        /// <summary>
        /// The application entry point.
        /// </summary>
        public static void Main()
        {
            try
            {
                RunExample();
            }
            catch(Exception exception)
            {
                Console.WriteLine(exception);
            }
            Console.WriteLine("Press ENTER to terminate.");
            Console.ReadLine();
        }

        private static void RunExample()
        {
            // Create a calculator.
            StatsCalculator calculator = new StatsCalculator();

            // Load an array of a custom data type. The calculator
            // will use reflection to achieve this.
            DataSheet sheet = calculator.Load(
                new Product[]{
                    new Product("Milk", 52, 12.8),
                    new Product("Cheese", 58, 64.8),
                    new Product("Bread", 62, 66.9) });
            Console.WriteLine(sheet);
        }

        /// <summary>
        /// Product is a class with properties name, size and cost.
        /// </summary>
        private class Product
        {
            /// <summary>
            /// Creates a new product instance.
            /// </summary>
            public Product(string name, double size, double cost)
            {
                _name = name;
                _size = size;
                _cost = cost;
            }

            /// <summary>
            /// Gets or sets the product name.
            /// </summary>
            public string Name
            {
                get{ return _name; }
                set{ _name = value; }
            }

            /// <summary>
            /// Gets or sets the product size.
            /// </summary>
            public double Size
            {
                get{ return _size; }
                set{ _size = value; }
            }

            /// <summary>
            /// Gets or sets the product cost.
            /// </summary>
            public double Cost
            {
                get{ return _cost; }
                set{ _cost = value; }
            }

            private double _size = 0;
            private double _cost = 0;
            private string _name = null;
        }
    }
}