can't get string value from inheritance - c#

Can't get string value even tho in my Parent class it works properly and now that I'm getting it . It won't get its value
this my parent class
public class GameManagerRevamped : MonoBehaviour
{
public string finalAnswer;
public string TryInheritance;
public virtual void firstButtonClicked()
{
Debug.Log(TryInheritance);
}
public virtual void GetValuebtn()
{
TryInheritance = finalAnswer;
Debug.Log("this is working" +TryInheritance +finalAnswer);
}
}
Child Class
public class FableScript : GameManagerRevamped
{
public override void firstButtonClicked()
{
finalAnswer += "a";
base.firstButtonClicked();
}
}

GetValuebtn() method is never called therefore TryInheritance is always "".
I think you want to do the following:
public virtual void firstButtonClicked()
{
GetValuebtn();
Debug.Log(TryInheritance);
}

Related

how to pass value to abstract class for changing name property

I had an interview today and it had the following code with 2 questions. Could someone please help me answer the two questions below on the below code snippet. (Some minor syntax error if seen please ignore as I tried to remember it from mind)....
Question 1 - Change the name to "NameChange".
Question 2 - Override the abstract method in derived class.
public abstract Class A
{
public string Name { get { return GetName(); } };
public virtual string GetName()
{
return this.Name.ToString();
}
protected abstract void SomeMethod();
}
public class B : A
{
//Change name to "NameChange"
//override the abstract method here
}
I'm not sure whether there's a hidden question or those are as simple as this. For the first question, there're two answers (method 1 and 2):
public class B : A
{
//Method 1
public new string Name { get { return "NameChange"; } }
//Method 2
public override string GetName()
{
return "NameChange"; // return whatever you want
}
protected override void SomeMethod() // override abstract method
{
// do something
}
}
In Method 1, you are effectively hiding the implementation of Name of the base class. In Method 2, you are overriding the implementation of GetName.
This should answer both questions:
public class B : A
{
//override Method
public override string GetName()
{
return "NameChange";
}
// override abstract Method
protected override void SomeMethod()
{
//code here...
}
}
I'm not sure if i get it right, but is that what you need?
public class B : A
{
public string NameChange => base.GetName();
protected override void SomeMethod()
{
throw new NotImplementedException();
}
}
Or maybe this one?
public class B : A
{
public override string GetName()
{
return "NameChange";
}
protected override void SomeMethod()
{
throw new NotImplementedException();
}
}
I am not sure what is the point here but if they insist on changing the value of property name, new constraint can be the trick here.
You can find the implementation following;
public abstract class A
{
public string Name { get { return GetName(); } }
public virtual string GetName()
{
return this.Name.ToString();
}
protected abstract void SomeMethod();
}
public class B : A
{
public B() : base()
{
SomeMethod();
}
public new string Name { get; set; }
public override string GetName()
{
return Name;
}
protected override void SomeMethod()
{
this.Name = "Ayberk";
}
}
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
B b = new B();
Console.WriteLine(b.GetName());
}
}
You can try it here

Inheritance with packages/namespaces [C#]

I want to redefine the classes of a package in a sub-package while keeping the hierarchy.
I would have wanted to do something like this :
namespace RPG {
public class Actor {}
public class Character : Actor {}
}
public class Actor : RPG.Actor {}
//kind of a diamond problem
public class Character : Actor, RPG.Character {}
I know this is incorrect ; C# doesn't support multiple inheritance, but I don't know the best way to resolve this kind of redefinition.
Interfaces doesn't solve my problem. I could do something like that using composition :
public class Character : Actor {
private RPG.Character basis;
// and do this for each attribute and method
public int atk { get { return basis.atk } set { basis.atk = value; } }
}
but it doesn't look good. Moreover, I ran into another problem with this way
namespace RPG {
public class Actor {
public List<Stat> stats { get; set; }
}
public class Stat {}
}
public class Actor {
private RPG.Actor basis;
//I can't do that because Stat does not inherit from RPG.Stat
public override List<RPG.Stat> stats { get; set; }
}
public class Stat {
private RPG.Stat basis;
}
What is the best way to resolve my problem ?
Use composition instead of inheritance.
It's tough to draw a direct example because you didn't provide any actions for your RPG actor/characters but the concept is that you inject the behaviors in the constructor of your class.
In this case "move" is the behavior:
class Fighter
{
public int Health = 100;
public string Name;
private Move _move;
public string MoveName
{
get
{
return _move.Name;
}
}
public int MoveDamage
{
get
{
return _move.Damage;
}
}
public Fighter(string name, Move defaultMove)
{
Name = name;
_move = defaultMove;
}
public void Attack(Fighter defendant)
{
if (_move != null)
_move.Attack(defendant);
}
public void SetMove(Move move)
{
if (move != null)
_move = move;
}
}
abstract class Move
{
public int Damage { get; set; }
public string Name { get; set; }
protected Move(int damage,string name)
{
Damage = damage;
Name = name;
}
public void Attack(Fighter defendant)
{
defendant.Health -= Damage;
}
}
class PunchMove:Move
{
public PunchMove() : base(5, "Punch")
{
}
}
class KickMove:Move
{
public KickMove() : base(7, "Kick")
{
}
}
try this.
namespace RPG {
public class Actor {
public void d(){
System.Console.Write("fhdf");
}
}
public class Character : Actor {}
}
////////////////
public class Character : RPG.Character {}
class Program
{
static void Main()
{
Character c = new Character();
c.d();
Console.WriteLine("Hello, World!");
}
}

How to have the base class be aware of derived class properties?

I'm trying to create a base class that will be used in storing data in key/value manner. The base class will generate the keys and is responsible for storing the data. When the base class is inherited, the derived class can specify it's own key but the the base class has to be aware of the derived classes' key. For example:
public class ParentClass
{
private string key = "Parent";
public void GenerateKey()
{
Console.WriteLine(key);
}
}
public class FeatureClass : ParentClass
{
public string key = "Feature";
}
public class SubFeatureClass : FeatureClass
{
public string key = "SubFeature";
}
FeatureClass feature = new FeatureClass();
feature.GenerateKey(); //I would like this to produce "Parent_Feature"
SubFeatureClass subFeature = new SubFeatureClass();
subFeatureClass.GenerateKey(); //I would like this to generate "Parent_Feature_SubFeature"
How can I make the base class be aware of its children's keys regardless of how many levels of inheritance has occurred.
You can do this by making the base method virtual and referencing it in the override:
public class ParentClass
{
public virtual string key {get {return "Parent";}}
public void GenerateKey()
{
Console.WriteLine(key);
}
}
public class FeatureClass : ParentClass
{
public override string key {get{return base.key + "_Feature";}}
}
public class SubFeatureClass : FeatureClass
{
public override string key {get{return base.key + "_SubFeature";}}
}
You could do this, using virtual properties,
public class ParentClass
{
public virtual string Key
{
get
{
return "Parent";
}
}
}
public class FeatureClass : ParentClass
{
public override string Key
{
get
{
return base.Key + "_" + "Feature";
}
}
}
public class SubFeatureClass : FeatureClass
{
public override string Key
{
get
{
return base.Key + "_" + "SubFeature";
}
}
}
Then, you could use it this way,
FeatureClass feature = new FeatureClass();
Console.WriteLine(feature.Key); // <-- "Parent_Feature"
SubFeatureClass subFeature = new SubFeatureClass();
Console.WriteLine(subFeature.Key); // <-- "Parent_Feature_SubFeature"
Use a virtual property instead of a field:
public class ParentClass
{
public ParentClass(){Key = "Parent";}
private virtual string Key {get;set;}
public void GenerateKey()
{
Console.WriteLine(Key);
}
}
public class FeatureClass : ParentClass
{
public FeatureClass(){Key = base.Key + "_" + "Feature";}
override string Key {get;set;}
}
public class ParentClass
{
protected List<string> keys = new List<string>();
public ParentClass()
{
keys.Add("Parent");
}
public void GenerateKey()
{
Console.WriteLine(string.Join("_", keys));
}
}
public class FeatureClass : ParentClass
{
public FeatureClass()
{
keys.Add("Feature");
}
}
public class SubFeatureClass : FeatureClass
{
public SubFeatureClass()
{
keys.Add("SubFeature");
}
}
Abstract or virtual functions can be used to achieve this (which one depends on whether it makes sense for ParentClass to be the actual type or whether it always needs a derived class.
In any case, you would have a virtual (or abstract) function which you override in the derived class to return the special key. In fact, in C# you can have virtual/abstract properties:
public abstract class ParentClass
{
private string key = "Parent";
protected abstract string derivedKey
{
get;
}
public void GenerateKey()
{
Console.WriteLine(key + derivedKey);
}
}
public class FeatureClass : ParentClass
{
public override string derivedKey
{
get
{
return "Feature";
}
}
}
Using constructor overloading and constructor chaining in my version:
public class ParentClass
{
private string key;
protected ParentClass(string childKeys)
{
key = !string.IsNullOrEmpty(childKeys) ? key + "Parent_" + childKeys : key;
}
public void GenerateKey()
{
// Get keys from subclasses
Console.WriteLine(key);
}
}
public class FeatureClass : ParentClass
{
public FeatureClass() : base("Feature") { }
protected FeatureClass(string key) : base("Feature_" + key) { }
}
public class SubFeatureClass : FeatureClass
{
public SubFeatureClass() : base("SubFeature") { }
protected SubFeatureClass(string key) : base("SubFeature_" + key) { }
}
public class ReallySubFeatureClass : SubFeatureClass
{
public ReallySubFeatureClass() : base("ReallySubFeature") { }
}
Test code looks just like yours but I added one more level for example:
class Program
{
static void Main(string[] args)
{
FeatureClass feature = new FeatureClass();
feature.GenerateKey(); //I would like this to produce "Parent_Feature"
SubFeatureClass subFeature = new SubFeatureClass();
subFeature.GenerateKey(); //I would like this to generate "Parent_Feature_SubFeature"
ReallySubFeatureClass reallySubFeature = new ReallySubFeatureClass();
reallySubFeature.GenerateKey();
Console.ReadKey();
}
}
Result:

How to access different property value in derived class

I have ViewModelBase with a derived DerivedViewModel
ViewModelBase has DoSomething() which accesses AProperty
DerivedViewModel also uses DoSomething() but it needs to access a different object.
The reason behind this is that the ViewModel is used on a screen, as well as in a Dialog. When it is in a screen, it needs to access a particular entity, but when in a dialog, it needs to access a different entity.
Here it is, simplified, in code. If you run it, they both return A, instead of A, then B. So the question is, how to return A, then B?
class Program
{
static void Main(string[] args)
{
ViewModelBase bc = new ViewModelBase();
bc.DoSomething(); Prints A
DerivedViewModel dr = new DerivedViewModel();
dr.DoSomething(); Prints A, would like it to print B.
}
}
public class ViewModelBase {
private string _aProperty = "A";
public string AProperty {
get {
return _aProperty;
}
}
public void DoSomething() {
Console.WriteLine(AProperty);
}
}
public class DerivedViewModel : ViewModelBase {
private string _bProperty = "B";
public string AProperty {
get { return _bProperty; }
}
Override the property in derived class
public class ViewModelBase
{
private string _aProperty = "A";
public virtual string AProperty
{
get { return _aProperty; }
}
public void DoSomething()
{
Console.WriteLine(AProperty);
}
}
public class DerivedViewModel : ViewModelBase
{
private string _bProperty = "B";
public override string AProperty
{
get { return _bProperty; }
}
}
DerivedViewModel dr = new DerivedViewModel();
dr.DoSomething();//Prints B
Further take a look at Msdn Polymorphism

Override only Get accessor

I got an abstract class :
abstract class ClassBase
{
public abstract string Test { get; }
}
I want to derive it and by the way add a set accesor
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
set { _s = value; }
}
}
I can't do that because i may not override set
class ClassDerive2 : ClassBase
{
string _s;
public string Test
{
override get { return _s; }
set { _s = value; }
}
}
Syntax error
class ClassDerive3 : ClassBase
{
string _s;
public override string ClassBase.Test
{
get { return _s; }
}
public string Test
{
set { _s = value; }
}
}
Syntax error
Any Idea ???
thx
You cannot do exactly what you want to do but here is a workaround:
abstract class ClassBase
{
public abstract String Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
}
public void SetTest(String test)
{
this._s = test;
}
}
This will make Test only settable in ClassDerived via the public SetTest method. I know this is not as clean as using the property's setter but it is about as good as it's going to get.
If at first you have defined a read-only property in a type, you can't later change it to a read/write property in a derived class. That's simply how .NET works, and can't be changed.
If, on the other hand, you define an interface with a read-only property, you can later implement that interface in a class with a writable property.
If you'd like to share what you are trying to achieve, perhaps we can come up with a design that works and can compile :)
Another way:
abstract class ClassBase
{
public abstract string Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
protected void setTest(string s)
{
_s = s;
}
public override string Test
{
get { return _s; }
}
}
class ClassDerive2 : ClassDerive
{
public new string Test
{
get { return base.Test; }
set { setTest(value); }
}
}
class Program
{
static void Main(string[] args)
{
var cd2 = new ClassDerive2();
cd2.Test = "asdf";
Console.WriteLine(cd2.Test);
}
}
My first thought was also to implement it as an interface. If this fits in with your design, the following code will work:
public interface TestInterface
{
string TestProperty { get; }
}
public class TestClass : TestInterface
{
public string TestProperty
{
get { return "test"; }
set { string t = value; }
}
}
No you cant, sorry. It is by design, so it's the law.

Categories