0

Hi guys I need some help. I'm completely suck on how to do this with Inheritance.

The aim is to change the value of 'Name' property inherited by the Parent Class (Clothing) of a shirt object.

When a button is clicked withing my C# Web Form by calling the Shirt's method of "ReNameShirt()" to change the name and then display the new 'name'.

My teacher hinted me by saying to use a subroutine. Still lost.

Can you help me out? Much Appreciated.

Clothing Class

using System;
                                 //THE PARENT CLASS 'Clothing'
public class Clothing
{
    public string _name;
    public string _size;

    public string name {get; set;}

    public string size {get; set;}

}
                                //SUBCLASS OF 'Trousers'
public class Trousers : Clothing
{
    public string LegLength { get; set; }
    public Trousers()
    {
        LegLength = "91";
    }

}

public class Shirt : Clothing
{
    public string ReNameShirt()
    {
        Shirt Po = new Shirt();
        Po.name = "blue shirt";
        return ReNameShirt();
    }

within the Inheritance.aspx.cs file:

    protected void Button2_Click(object sender, System.EventArgs e)
    {
        Shirt myShirt = new Shirt();
        myShirt.name = "red polo";
        myShirt.size = "85";
        Label2.Text = "<b>Name:</b> " + myShirt.name + " <b>Size</b> " + myShirt.size;

        myShirt.ReNameShirt();
        Label3.Text = myShirt.size;
    }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Andrew C. Duarte
  • 237
  • 2
  • 15

3 Answers3

1

Your shirt already exists so there is no need for you to create an instance of it. Doing that isn't modifying "this shirt." It's like if you told me "dye this shirt blue" and instead I said "here is another blue shirt" but then threw it away (because the variable goes out of scope). Your next problem is you are doing recursion. Since it sounds like you're new to programming, let me explain. Recursion is when a method (what your professor is calling a subroutine, another term for it) calls itself. That's OK, but you need it to end at some point. In your case, your method will call itself until you overflow the stack (get it... stackoverflow?) and the program can't make any more calls so it will crash. When ever you do recursion, you need to make sure there is a way to end it. Classic recursion problems are like factorial where it is defined as n*(n-1) factorial, but the way it ends is 1! Is just defined as 1, so once n-1 =1, I don't factorial any more, I just return 1.

public class Shirt : Clothing
{
    public void ReNameShirt()
    {
//        Shirt Po = new Shirt(); You are a shirt, there is no need to create one.
        name = "blue shirt";
//        return ReNameShirt(); This will cause infinite recursion and crash.
    }
dmeglio
  • 2,830
  • 1
  • 19
  • 24
  • Thanks for your answer as well as @C-Pound-Guru for your replies. Appreciate it! I have made the changes however the output does not change to "blue shirt". Still displaying as "red polo" – Andrew C. Duarte Apr 30 '15 at 02:02
  • Found the problem using break points within Visual Studio. Thanks for all your help. Really appreciate it. – Andrew C. Duarte Apr 30 '15 at 02:14
  • If your goal is to become a programmer, the debugger will become your best friend! – dmeglio Apr 30 '15 at 02:20
0

Your ReNameShirt method isn't doing what you thing it's doing. It's instantiating a new Shirt and will cause recursion rather than changing the shirt's name. Change it to:

public void ReNameShirt()
{
   this.name = "blue shirt";
}
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
0

Your problem is that you are creating a new instance inside your method and updating the value of this instance. Your function should be something like that:

public class Shirt : Clothing
{
    public void ReNameShirt()
    {
        name = "blue shirt"; 
    }
}
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44