PC Review


Reply
Thread Tools Rate Thread

How design a class?

 
 
Anders Eriksson
Guest
Posts: n/a
 
      22nd Sep 2012
If I have a structure that look like this

foo
bar1 or
bar2 or
bar3

I. e. foo can contain either a bar1 or a bar2 or a bar3.

How would class foo look like?

// Anders

--
English isn't my first language.
So any error or strangeness is due to the translation.
Please correct my English so that I may become better.
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      22nd Sep 2012
On 9/22/2012 1:07 PM, Anders Eriksson wrote:
> If I have a structure that look like this
>
> foo
> bar1 or
> bar2 or
> bar3
>
> I. e. foo can contain either a bar1 or a bar2 or a bar3.
>
> How would class foo look like?


The obvious would be an abstract base class BarBase or an interface
IBar.

Arne


 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      22nd Sep 2012
On 9/22/2012 1:17 PM, Arne Vajhøj wrote:
> On 9/22/2012 1:07 PM, Anders Eriksson wrote:
>> If I have a structure that look like this
>>
>> foo
>> bar1 or
>> bar2 or
>> bar3
>>
>> I. e. foo can contain either a bar1 or a bar2 or a bar3.
>>
>> How would class foo look like?

>
> The obvious would be an abstract base class BarBase or an interface
> IBar.


Demo:

using System;

namespace E
{
public abstract class Bar
{
public abstract void RealM();
public void M()
{
RealM();
}
}
public class Bar1 : Bar
{
public override void RealM()
{
Console.WriteLine("Bar1 here");
}
}
public class Bar2 : Bar
{
public override void RealM()
{
Console.WriteLine("Bar2 here");
}
}
public class Foo
{
private Bar bar;
public Foo(Bar bar)
{
this.bar = bar;
}
public void MM()
{
bar.M();
}
}
public class Program
{
public static void Main(string[] args)
{
Foo foo1 = new Foo(new Bar1());
foo1.MM();
Foo foo2 = new Foo(new Bar2());
foo2.MM();
Console.ReadKey();
}
}
}

Arne

 
Reply With Quote
 
Brian Cryer
Guest
Posts: n/a
 
      24th Sep 2012
"Anders Eriksson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If I have a structure that look like this
>
> foo
> bar1 or
> bar2 or
> bar3
>
> I. e. foo can contain either a bar1 or a bar2 or a bar3.
>
> How would class foo look like?


Given that you used the word "structure", I wonder whether you are thinking
of what in C/C++ would be a union? Union's are not supported in C# (although
if you google for it you can find some good simulations - although I suspect
these are only viable with value types not reference types). So you are left
with inheritance as the only viable way forward - see Arne's post.
--
Brian Cryer
http://www.cryer.co.uk/brian

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i apply an Axis design or radial design from design templa. =?Utf-8?B?bmFkaWE=?= Microsoft Powerpoint 1 3rd Apr 2005 02:21 AM
Hyperlinks Inserted in Design Mode inactive once Exited Design Mod =?Utf-8?B?Q3JhaWc=?= Microsoft Excel Programming 0 16th Mar 2005 05:53 PM
How do I design a Powerpoint design template for office 2003? =?Utf-8?B?Q2xpZmYgQmFpbGV5?= Microsoft Powerpoint 1 1st Mar 2005 11:07 AM
How do I remove downloaded design template from Slide Design? =?Utf-8?B?VEM=?= Microsoft Powerpoint 1 25th Jan 2005 01:02 PM
Re: Enter Excel Design Mode and Exit Design Mode Bill Lunney Microsoft Excel Programming 0 4th Aug 2003 07:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:27 PM.