user2367083 user2367083 - 9 days ago 5
C# Question

c# abstract field as a property and as a return from a method

I've tried and tried but I'm not sure how to even google this.

I've been playing with an abstract class this morning.
What I would like to do is get (only a

get
, forgetting a
set
for now) the
whenused
field of
Salutation
through the
GoodMorning
class ~ but to access it as a property instead of a method call.

The following code works just fine. But I'd use
morn.whenUsed
in the Main() instead of
morn.whenUsed()
.

If the same change is necessary in
GoodMorning
,
GoodAfternoon
, and
GoodNight
that is fine. But I'd like to think the three classes can implement whenUsed differently, but if not then that's fine.

Is this even possible using an abstract?

Thanks for you input.

namespace AbstractClas
{
class Program
{
static void Main(string[] args)
{
Salutation morn = new GoodMorning("beautiful day");
Salutation aftr = new GoodAfternoon("after lunch comfort food comma");
Salutation nigh = new GoodNight("light and brezzy night");

Console.WriteLine("morn is the saluataion used during the {0}.", morn.whenUsed());
Console.WriteLine("aftr is the saluataion used during the {0}.", aftr.whenUsed().ToUpper());
Console.WriteLine("nigh is the saluataion used during the {0}.", nigh.whenUsed());

if (morn is Salutation)
Console.WriteLine("morn is a Salutation");

if (morn is GoodMorning)
Console.WriteLine("morn is a GoodMorning");

if (morn is GoodAfternoon)
Console.WriteLine("morn is a GoodAfternoon");
else
Console.WriteLine("morn is NOT a GoodAfternoon");

if (morn is GoodNight)
Console.WriteLine("morn is a GoodNight");
else
Console.WriteLine("morn is NOT a GoodNight");

Console.ReadKey();
}
}


internal abstract class Salutation
{
protected string salutation = "";
protected string whenused = "";
internal abstract void salu(string str, string wUsed);
internal abstract string whenUsed();
}

internal class GoodMorning : Salutation
{
internal override void salu(string s, string w) { salutation = s; whenused = w; }
internal GoodMorning(string gm) { salu(gm, "morning".ToUpper()); }
internal override string whenUsed() { return whenused; }
}

internal class GoodAfternoon : Salutation
{
internal override void salu(string s, string w) { salutation = s; whenused = w; }
internal GoodAfternoon(string ga) {
char[] c = "afternoon".ToCharArray();
Array.Reverse(c);
salu(ga, new string(c));
}
internal override string whenUsed() { return whenused; }
}

internal class GoodNight : Salutation
{
internal override void salu(string s, string w) { salutation = s; whenused = w; }
internal GoodNight(string gn) { salu(gn, "night".ToUpper()); }
internal override string whenUsed() { return whenused.ToLower(); }
}

}

Answer

If you are looking to change whenUsed from a method to a read only property, you can define it as

 internal abstract string whenUsed { get; }

in Salutation, and then override it as

internal override string whenUsed { get { return /*whatever the specific class wants to return*/; } }

in your derived classes

Comments