Click here to Skip to main content
15,903,203 members
Home / Discussions / C#
   

C#

 
AnswerRe: User control Name Pin
OriginalGriff1-Jul-10 0:42
mveOriginalGriff1-Jul-10 0:42 
GeneralRe: User control Name Pin
chrisclarke111-Jul-10 2:05
chrisclarke111-Jul-10 2:05 
GeneralRe: User control Name Pin
OriginalGriff1-Jul-10 2:11
mveOriginalGriff1-Jul-10 2:11 
GeneralRe: User control Name Pin
freakyit1-Jul-10 2:05
freakyit1-Jul-10 2:05 
AnswerRe: User control Name Pin
freakyit1-Jul-10 2:27
freakyit1-Jul-10 2:27 
GeneralRe: User control Name Pin
chrisclarke111-Jul-10 7:40
chrisclarke111-Jul-10 7:40 
GeneralRe: User control Name Pin
freakyit2-Jul-10 3:04
freakyit2-Jul-10 3:04 
Questionvirtual/abstract methods and properties Pin
Daley8330-Jun-10 23:27
Daley8330-Jun-10 23:27 
Ok so you may have seen my very basic question yesterday and I'm back today to build the project up and could use some pointers on virtual and abstract and how to apply them.

In a nutshell my project has a FruitBase class that simply establishes private fields and then public properties that each individual fruit will have.
public class FruitBase
    {
        //Private fields
        private string _name;
        private string _colour;

        //Constructor
        public FruitBase()
        {
        }
        
        //Properties
        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string Colour
        {
            get { return _colour; }
            set { _colour = value; }
        }
    }

I then have individual fruit classes eg. apple, banana etc. that set the default values within thier constructors, eg.
public class Apple: FruitBase
   {
       //Constructor
       public Apple()
       {
           //Set the default values of the class
           this.Name = "Apple";
           this.Colour = "Red";
       }
   }

The idea is the user selects a radio button for a fruit type and we then return the defaults on a button press, which runs the function GetValues().
<asp:RadioButtonList runat="server" ID="radList" RepeatDirection="Horizontal">
            <asp:ListItem Text="Apple" Value="Apple" />
            <asp:ListItem Text="Banana" Value="Banana" />
            <asp:ListItem Text="Orange" Value="Orange" />
            <asp:ListItem Text="Pear" Value="Pear" />
        </asp:RadioButtonList>
        <asp:Button runat="server" ID="btnFruit" Text="Get" OnClick="GetValues" />
        <br />
        <br />
        <asp:TextBox runat="server" ID="txtOuput" TextMode="MultiLine" Width="250px" />

The issue is I am trying to avoid is a long-winded, repetative piece of code such as
public void GetValues(object sender, EventArgs e)
        {
string Ftype = FruitBase.GetType();

if(Ftype = Apple)
{
//GetDefaults
}
else if(Ftype = Banana)
{
//GetDefaults
}
else.........
}


As you can imagine if I include every type of fruit in a supermarket as an option I'll quickly end up with hundreds of lines of code.

So, to my (admittedly basic) knowledge, I should be able to create a virtual method within the FruitBase class that takes an object of FruitBase as a parameter and will then retrieve the default values of the users chosen fruit without any explicit mention of the chosen class (Basically avoiding any long if statements).

Immediately I'm stuck however.
First of all, how do I use the radio buttons selectedValue to specify the chosen fruit class, without another if(value = "Apple") else.....
Secondly how do I then pass this as an object and have the virtual method retrieve the default values from the correct class without being explicit and again avoiding the if..else situation mentioned above.

I realise this is a little long winded and I'm not great with the correct terminology but this was an example technical test at a recent job interview and I didn't get it so now I want to get my head round it.

Thanks for reading and any help is greatly appreciated Smile | :)
AnswerRe: virtual/abstract methods and properties Pin
Peace ON30-Jun-10 23:51
Peace ON30-Jun-10 23:51 
GeneralRe: virtual/abstract methods and properties Pin
Daley831-Jul-10 0:00
Daley831-Jul-10 0:00 
AnswerRe: virtual/abstract methods and properties Pin
Peace ON1-Jul-10 0:20
Peace ON1-Jul-10 0:20 
AnswerRe: virtual/abstract methods and properties Pin
Łukasz Nowakowski1-Jul-10 0:22
Łukasz Nowakowski1-Jul-10 0:22 
GeneralRe: virtual/abstract methods and properties Pin
Daley831-Jul-10 1:04
Daley831-Jul-10 1:04 
AnswerUPDATE: virtual/abstract methods and properties Pin
Daley831-Jul-10 1:02
Daley831-Jul-10 1:02 
GeneralRe: UPDATE: virtual/abstract methods and properties Pin
Łukasz Nowakowski1-Jul-10 1:29
Łukasz Nowakowski1-Jul-10 1:29 
GeneralRe: UPDATE: virtual/abstract methods and properties Pin
Daley831-Jul-10 1:45
Daley831-Jul-10 1:45 
GeneralRe: UPDATE: virtual/abstract methods and properties Pin
Łukasz Nowakowski1-Jul-10 1:54
Łukasz Nowakowski1-Jul-10 1:54 
GeneralRe: UPDATE: virtual/abstract methods and properties Pin
harold aptroot1-Jul-10 3:47
harold aptroot1-Jul-10 3:47 
AnswerRe: virtual/abstract methods and properties Pin
Ennis Ray Lynch, Jr.1-Jul-10 3:44
Ennis Ray Lynch, Jr.1-Jul-10 3:44 
QuestionHow to join 2 string[] objects Pin
Chesnokov Yuriy30-Jun-10 23:00
professionalChesnokov Yuriy30-Jun-10 23:00 
AnswerRe: How to join 2 string[] objects Pin
R. Giskard Reventlov30-Jun-10 23:03
R. Giskard Reventlov30-Jun-10 23:03 
AnswerRe: How to join 2 string[] objects Pin
freakyit30-Jun-10 23:19
freakyit30-Jun-10 23:19 
AnswerRe: How to join 2 string[] objects Pin
Ayman Kouzayha30-Jun-10 23:31
Ayman Kouzayha30-Jun-10 23:31 
AnswerRe: How to join 2 string[] objects Pin
Laxman Auti1-Jul-10 0:19
Laxman Auti1-Jul-10 0:19 
GeneralRe: How to join 2 string[] objects Pin
Chesnokov Yuriy1-Jul-10 0:41
professionalChesnokov Yuriy1-Jul-10 0:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.