Am I missing any concept about inheritance? I am trying to call a method that is in a child class but it doesn't work.
This is what I have, so simple:
Vuelo.cs
public class Vuelo
{
private Aeronave _aeronave { get; set; }
public Vuelo(string numero, Aeronave aeronave)
{
ValidarNumero(numero); // validates numero
_aeronave = aeronave;
}
public string modelo_aeronave()
{
return _aeronave.model(); // This is where the error goes, .model()
}
public string RegistroAvion()
{
return _aeronave.Registration(); // This worked perfectly
}
}
Aeronave.cs
public class Aeronave
{
private string _registration { get; set; }
public Aeronave(string registration)
{
_registration = registration;
}
public string Registration()
{
return _registration;
}
}
Airbus319.cs (the child class):
public class AirbusA319 : Aeronave
{
private string _model { get; set; }
public AirbusA319(string model, string registro) : base(registro)
{
_model = model;
}
public string model()
{
return _model;
}
}
I want to show up the model of the Airbus that is in model() like this:
Vuelo vuelo = new Vuelo("AB345", new AirbusA319("G-EUPT", "GG235"));
Console.WriteLine(vuelo.modelo_aeronave());
I can't find solutions in the internet, even in microsoft docs about inheritance.
You would need to modify your classes as shown below. Aeronave should contain model (virtual or abstract) to be overridden in Airbus.
public abstract class Aeronave
{
private string _registration { get; set; }
public Aeronave(string registration)
{
_registration = registration;
}
public string Registration()
{
return _registration;
}
public abstract string model();
}
public class AirbusA319 : Aeronave
{
private string _model { get; set; }
public AirbusA319(string model, string registro) : base(registro)
{
_model = model;
}
public override string model()
{
return _model;
}
}
Related
I have base class and some inherited classes, like this:
public abstract class Base
{
public long ID { get; set; }
public string Name { get; set; }
public virtual string Description { get { return "Base"; } }
}
public class A : Base
{
public override string Description { get { return "A"; } }
}
public class B : Base
{
private string extraInfo;
public override string Description { get { return "B"; } }
public string ExtraInfo
{
get { return extraInfo; }
set { extraInfo = value; }
}
}
I have collection of these objects and I set this collection to DataSource of DataGridView. Then, columns of DataGridView will be created by Base class's properties (ID, Name. Description). Is there any way to reflect also concrete implementation properties?
List<Base> items = new List<Base>();
DataGridView dgv = new DataGridView();
dgv.DataSource = new BindingList<Base>(items);
In this case, I want also column for ExtraInfo property of class B.
Inherit class B too:
public class Base
{
public long ID { get; set; }
public string Name { get; set; }
public virtual string Description { get { return "Base"; } }
}
public class A : Base
{
public override string Description { get { return "A"; } }
}
public class B : Base
{
private string extraInfo;
public override string Description { get { return "B"; } }
public string ExtraInfo
{
get { return extraInfo; }
set { extraInfo = value; }
}
}
I have an object derived from interface. I want to use display template and editor template.Display template works very well. But editor template does not work very well.It does not understand it says "can not create instance of an interface".
I have a custom model binder. But it is really dummy.
protected override object CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext, Type modelType)
{
if (modelType.Equals(typeof(IExample)))
{
Type instantiationType = typeof(ExampleType1);
var obj = Activator.CreateInstance(instantiationType);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, instantiationType);
bindingContext.ModelMetadata.Model = obj;
return obj;
}
return base.CreateModel(controllerContext, bindingContext, modelType);
}
How can I do it for every class derived from IExample? Any ideas?
[HttpGet]
public ActionResult Index()
{
MyModel model = new MyModel();
model.inter = new ExampleType1();
model.inter.number = 50;
return View(model);
}
[HttpPost]
public ActionResult Index(MyModel model)
{
//*-*-* I want to get it here.
return View();
}
public class MyModel
{
public IExample inter { get; set; }
}
public interface IExample
{
int number { get; set; }
}
public class ExampleType1 : IExample
{
public int number { get; set; }
public string tip1 { get; set; }
}
public class ExampleType2 : IExample
{
public int number { get; set; }
public string tip2 { get; set; }
}
Without dwelling on reason why you need this ( i think it's a bad design, to have interface in as a parameter for controller methods). I think the simplest solution would be to extend the IExample interface with string property ImplementedType.
public interface IExample
{
string type {get;}
int number { get; set; }
}
Implementation:
public class ExampleType1 : IExample
{
public string type
{ get { return "ExampleType1"; } }
public int number { get; set; }
public string tip1 { get; set; }
}
And model binder:
var type = (string)bindingContext.ValueProvider.GetValue("type");
if (type == "ExampleType1")
{
//create new instance of exampletype1.
}
I build a rest service which output are json. I using Newtonsoft.Json.
This is my class.
public class DownloadPDA
{
public List<FRUTE> lsRute { get; set; }
public List<FCUSTMST> lsCustomer { get; set; }
public List<FMASTER> lsMaster { get; set; }
public List<FNOTEC> lsNotec { get; set; }
public List<FINFO> lsInfo { get; set; }
public List<FBRAND> lsBrand { get; set; }
public List<FKPL> lsKpl { get; set; }
}
but when I test my rest service my result are:
{"downloadDataResult":"{"lsBrand":[{}],"lsCustomer":[{},{},{}],"lsInfo":[],"lsKpl":null,"lsMaster":[{},{},{},{},{}],"lsNotec":[],"lsRute":[{},{},{}]}"}
it not show the data in list. I know something is wrong. Can anybody help?
This one of my collection class
public class FRUTE
{
private String norute;
private String custno;
private String flag;
private String st_visit;
private float amount;
private int jmlvisit;
public FRUTE() { }
public void getData(DCTRTDTO dto) {
this.norute = dto.NOROUT;
this.custno = dto.NOCUST;
this.flag = dto.FLAG;
this.st_visit = "not yet";
this.amount = 10;
this.jmlvisit = 1;
}
public static List<FRUTE> getList(List<DCTRTDTO> lsRute)
{
List<FRUTE> ls = new List<FRUTE>();
FRUTE info = new FRUTE();
foreach (DCTRTDTO dto in lsRute)
{
info.getData(dto);
ls.Add(info);
}
return ls;
}
}
Your FRUTE class doesn't have public properties that are required for Json serialization.
Encapsulate you private fields and all will work as expected.
public class FRUTE
{
private String norute;
private String custno;
public string Norute
{
get { return norute; }
set { norute = value; }
}
public string Custno
{
get { return custno; }
set { custno = value; }
}
//...
}
I have the below code in my Application.
public class GeneralInfo
{
private string _id;
private string _name;
public string id
{
set
{
_id = value;
}
get
{
return _id;
}
}
public string name
{
set
{
_name = value;
}
get
{
return _name;
}
}
}
public class SecureInfo
{
private string _password;
public string password
{
set
{
_password = value;
}
get
{
return _password;
}
}
}
public class User
{
}
I need to apply multiple inheritance in the above code ie. the classes GeneralInfo,SecureInfo properties should be accessible in the user class.
I know using interface Multiple inheritance can be achieved. But i need to define the properties in the base class which is restricted in Interface.
How I can achieve this?
C# does not support multiple inheritance. However you can achieve this via multiple interfaces.
public interface ISecureInfo
{
}
public interface IGeneralInfo
{
}
public class UserClass : ISecureInfo, IGeneralInfo {
}
You probably better off encapsulating the data in the class rather than trying to use something to do multiple inheritance here. See this question for some arguments for this.
You can achieve this through interface based inheritance:
public interface IGeneralInfo
{
String Id { get; set; }
String Name { get; set; }
}
public interface ISecureInfo
String Password { get; set; }
}
public class User : IGeneralInfo, ISecureInfo
{
// Implementation of IGeneralInfo
public String Id { get; set; }
public String Name { get; set; }
// Implementation of ISecureInfo
public String Password { get; set; }
}
Or, going one step further, through composition:
public interface IGeneralInfo
{
String Id { get; set; }
String Name { get; set; }
}
public class GeneralInfo : IGeneralInfo
{
public String Id { get; set; }
public String Name { get; set; }
}
public interface ISecureInfo
String Password { get; set; }
}
public class SecureInfo : IGeneralInfo
{
public String Password { get; set; }
}
public class User : IGeneralInfo, ISecureInfo
{
private GeneralInfo generalInfo = new GeneralInfo();
private SecureInfo secureInfo = new SecureInfo();
public String Id {
get { return generalInfo.Id; }
set { generalInfo.Id = value; }
}
public String Name {
get { return generalInfo.Name; }
set { generalInfo.Name = value; }
}
public String Password {
get { return secureInfo.Password; }
set { secureInfo.Password = value; }
}
}
From your sample description, encapsulation might be what you might want to use:
public class Info{
GeneralInfo general;
SecureInfo secure;
...
}
You cannot do multiple inheritance in C# because it is not supported like C++. In C# you can use interfaces for it and implement method and properties. For sample, you could have a base class
public abstract class Entity
{
public string Name { get; set; }
}
You also could have some interfaces:
public interface IPrint
{
void Print();
}
public interface IGenerate
{
void Generate();
}
And use it like multiples inheritance (but it is not, it is just a single inheritance and interfaces)
public class User : Entity, IPrint, IGenerate
{
public void Print()
{
// some code
// here you could access Name property, because it is on base class Entity
}
public void Generate()
{
// some code
}
}
And you could instance it using the abstractions:
Entity e = new User();
IPrint p = new User();
IGenerate g = new User();
User u = new User();
If you need implementations, you could do a hiearachy inherits, for sample:
User inherit from Person that inherit from Entity.
public class Entity
{
public int Id { get; set; }
public void Method()
{
// some code
}
}
public class Person : Entity
{
public string Name { get; set; }
public void AnotherMethod()
{
// some code
}
}
public class User : Person
{
public string Password { get; set; }
public bool CheckUser(string name, string passworkd)
{
// some code
}
}
I think the best would be to seperate the implementation of the interfaces and the real class you have at the end.
What I mean is something like the Bridge Pattern.
Your class (that will implement several interfaces) will just deleagte the method calls to the real implementation, that you can have in a seperate place and only once.
You could also use an approach like this. You would get to the same point than if you would be using multiple inheritance. That way, you could inherit only Entity if you don't need the SecureInfo stuff (i.e. for books and other stuff). Still, I think composition would do better in this case as others say...
class User : SecuredEntity { }
abstract class SecuredEntity : Entity, ISecureInfo
{
public string Password { get; set; }
}
abstract class Entity : IGeneralInfo
{
public string ID { get; set; }
public string Name { get; set; }
}
interface IGeneralInfo
{
string ID { get; set; }
string Name { get; set; }
}
interface ISecureInfo
{
string Password { get; set; }
}
I'm trying to implement my own RequiredAttribute, in which I call a custom resource handler:
public class LocalizedValidationAttributes
{
public class LocalizedRequiredAttribute : RequiredAttribute
{
private String _resourceString = String.Empty;
public new String ErrorMessage
{
get { return _resourceString; }
set { _resourceString = GetMessageFromResource(value); }
}
}
private static String GetMessageFromResource(String resourceTag)
{
return ResourceManager.Current.GetResourceString(resourceTag);
}
}
I call this the following way:
[LocalizedValidationAttributes.LocalizedRequiredAttribute(ErrorMessage = "test")]
public String Text { get; set; }
But the getter of ErrorMessage is never called.
Any hints? Thanks!
Try like this:
public class LocalizedRequiredAttribute : RequiredAttribute
{
public override string FormatErrorMessage(string name)
{
return ResourceManager.Current.GetResourceString(name);
}
}
or like this:
public class LocalizedRequiredAttribute : RequiredAttribute
{
public LocalizedRequiredAttribute(string resourceTag)
{
ErrorMessage = GetMessageFromResource(resourceTag);
}
private static String GetMessageFromResource(String resourceTag)
{
return ResourceManager.Current.GetResourceString(resourceTag);
}
}
and then:
[LocalizedValidationAttributes.LocalizedRequiredAttribute("test")]
public String Text { get; set; }