Results 1 to 5 of 5

Thread: [2.0] hmm.. embeded class statements?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2.0] hmm.. embeded class statements?

    well.. I don't think u can do this(mostly because it doesn't work) so umm.. what is the snippet for it? umm.. just tell me why this doesn't work. thanks

    Code:
            class Shapes
            {
                public class Area
                {
                    public double Square(double Length)
                    {
                        return Length * Length;
                    }
                    public double Rectangle(double Length, double Width)
                    {
                        return Length * Width;
                    }
                    public double Triangle(double Base, double Height)
                    {
                        return .5 * (Base * Height);
                    }
                    public double Circle(double Radius)
                    {
                        return Math.PI * (Radius * Radius);
                    }
                    public double Parallelogram(double Base, double Height)
                    {
                        return Base * Height;
                    }
                    public double Trapezoid(double Length1, double Length2, double Height)
                    {
                        return .5 * Height * (Length1 * Length2);
                    }
                }
    
                public class Perimeter
                {
                    public double Square(double Length)
                    {
                        return Length * 4;
                    }
                    public double Rectangle(double Length, double Width)
                    {
                        return (Length * 2) + (Width * 2);
                    }
                    public double Triangle(double Length1, double Length2, double Length3)
                    {
                        return Length1 + Length2 + Length3;
                    }
                    public double Circle(double Radius)
                    {
                        return 2 * Math.PI * Radius;
                    }
                    public double Parallelogram(double Length1, double Length2)
                    {
                        return (2 * Length1) + (2 * Length2);
                    }
                    public double Trapezoid(double bottomBase, double leg1, double leg2, double topBase)
                    {
                        return bottomBase + leg1 + leg2 + topBase;
                    }
                }
            }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] hmm.. embeded class statements?

    Nested types are perfectly legal, e.g. the ListViewItem.ListViewSubItem class. If it wasn't legal you'd get an error message from the IDE. if you got an error message then you tell us what it was, right? If you don't get an error message then it's legal. If you simply don't get the results you expect then have simply coded the calculations incorrectly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] hmm.. embeded class statements?

    well.. there are no errors in the errorbox. It's just after I initalize Shapes into an object and call it, nothing comes up

    as in.. I type Shapes and then a period and the only things that come up are that ToString() stuff. None of the methods I made

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] hmm.. embeded class statements?

    You have completely misunderstood what you have done. The code above doesn't mean that an instance of the Shapes class will have properties that return instances of the Area and Perimeter classes. Your Shapes class has no instance members so an instance of the Shapes class will have no members other than those it inherits form the Object class. What you have done is declared the Shapes.Area and Shapes.Perimeter classes, which you can now create instances of, e.g.
    Code:
    Shapes.Area area = new Shapes.Area();
    Shapes.Perimeter perimeter = new Shapes.Perimeter();
    Having said that, your design is all wrong. Either all those methods should be static so that you don't need an instance at all, e.g.
    Code:
    int perimeter = Shapes.Perimeter.Square(10);
    or, more correctly in my opinion. You should have a separate class for each shape, which then has Area and Perimeter properties, e.g.
    Code:
    public abstract class Shape
    {
        public abstract double Area { get; }
        public abstract double Perimeter { get; }
    }
    
    public class Square : Shape
    {
        private double length;
    
        public double Length
        {
            get
            {
                return this.length;
            }
            set
            {
                this.length = value;
            }
        }
    
        public override double Area
        {
            get
            {
                return this.length * this.length;
            }
        }
    
        public override double Perimeter
        {
            get
            {
                return this.length * 4;
            }
        }
    
        public Square(double length)
        {
            this.length = length;
        }
    }
    Note that both the Shape class and its Area and Perimeter properties are declared 'abstract'. That means that you cannot create an instance of the Shape class directly. You must create an instance of a class that inherits Shape, but every class that does inherit Shape is guatanteed to provide an appropriate implementation of the Area and Perimeter properties for that particular shape.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] hmm.. embeded class statements?

    ooo ok thx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width