I have been given this interface to start with. There are a number of functions I must implement.
using System;
using System.Windows.Forms;
public interface IInfoCard
{
string Name { get; set; }
string Category { get; }
string GetDataAsString();
void DisplayData(Panel displayPanel);
void CloseDisplay();
bool EditData();
}
How would I implement the following function into the interface.
class Class2 : IInfoCard
{
public string Name
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
You should use then Abstract classes, not interfaces, then.
public abstract class IInfoCard
{
string Name { get; set; }
string Category { get; }
string GetDataAsString() { return null; }
void DisplayData(Panel displayPanel) {}
void CloseDisplay() {}
bool EditData() { return true;}
}
You are trying to implement a property. You can basically encapsulate a field.
An easy way to do that is using auto-property:
public string Name { get; set; }
Try this
public class Class2 : IInfoCard
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
You can use Auto-Implemented Properties
public class Class2 : IInfoCard
{
public string Name { get; set;}
}
See More
You have two options.
Option 1:
Make them Auto properties, in which case compiler creates a private, anonymous backing field.
public string Name {get;set};
Option 2
Defining an explicit backing field, a private field.
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
Related
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; }
}
class Section
{
public string Name;
public List<Option> Options;
public class Option
{
public string Name;
public string Value;
public string Path
{
get { return "SectionName.OptionName=Value"; }
}
}
}
In other words, I would like to have access to members of Section from Option, which will always be used as a list.
What is an elegant way to implement this?
An Option needs a reference to its Section:
class Section
{
public string Name;
public List<Option> Options;
public class Option
{
public Section MySection { get; private set; }
public string Name;
public string Value;
public string Path
{
get { return string.Format("{0}.{1}={2}", this.MySection.Name, this.Name, this.Value); }
}
public Option(Section mySection)
{
this.MySection = mySection;
}
}
}
I have 2 web refs which I can't change:
They are almost identical but when referenced one only accepts ProperCase and the other Uppercamelcase.
Example
Not only props is the thing but entire classes with its props and methods
#EDIT: Sorry, I've realized it's more complicated than initially stated:
Not only props is the thing but entire classes with its props and methods and inner classes. Although only used as structures, inner classes have the same issue.
public class Foobar
{
public string Logmsgno;
public string Revno;
public string Reqsox;
public void Dosomething();
public Barbaz Mybarbaz;
public List<quux> Myquuxlist;
}
And the other has names like
public class FooBar
{
public string LogMsgNo;
public string RevNo;
public string ReqSox;
public void DoSomething();
public BarBaz MyBarBaz;
public List<Quux> MyQuuxList;
}
Is there an easy way to make an interface for both?
TIA!
Without a proper re-factoring to update everything and changing names, yes, you COULD with a little bit of smoke and mirrors. Create an interface based on the NEW values you WANT them to be, then change them to respectively use getter/setter to retain original and not break it.
To expand from your expanded question. You would have to adjust each of those levels too.. Define an interface for the "Barbaz" and "BarBaz" class so your outer class can have an object of
public interface IYourBarBazInterface
{
string BarBazProp1 { get; set; }
string AnotherProp { get; set; }
}
public interface IQuux
{
int QuuxProp { get; set; }
string AnotherQuuxProp { get; set; }
}
public interface IYourCommonInterface
{
string LogMsgNo { get; set; };
string RevNo { get; set; };
string ReqSox { get; set; };
// Similar principle of declarations, but interface typed objects
IYourBarBazInterface MyBarBaz { get; set; }
List<IQuux> MyQuuxList;
void DoSomething();
}
public class Foobar : IYourCommonInterface
{
public string Logmsgno;
public string Revno;
public string Reqsox;
public void Dosomething();
// your existing old versions keep same name context
// but showing each of their respective common "interfaces"
public IYourBarBazInterface mybarbaz;
public List<IQuux> myQuuxlist = new List<IQuux>();
// these are the implementations of the interface...
public string LogMsgNo
{ get { return Logmsgno; }
set { Logmsgno = value; }
}
public string RevNo
{ get { return Revno; }
set { Revno = value; }
}
public string ReqSox
{ get { return Reqsox; }
set { Reqsox = value; }
}
public void DoSomething()
{ Dosomething(); }
// Now, the publicly common Interface of the "IYourCommonInterface"
// that identify the common elements by common naming constructs.
// similar in your second class.
public IYourBarBazInterface MyBarBaz
{ get { return mybarbaz; }
set { mybarbaz = value; }
}
public List<IQuux> MyQuuxList
{ get { return myQuuxlist; }
set { myQuuxlist = value; }
}
}
public class FooBar : IYourCommonInterface
{
// since THIS version has the proper naming constructs you want,
// change the original properties to lower case start character
// so the interface required getter/setter will be properly qualified
public string logMsgNo;
public string revNo;
public string reqSox;
public IYourBarBazInterface MyBarbaz;
public List<IQuux> Myquuxlist;
// these are the implementations of the interface...
public string LogMsgNo
{ get { return logMsgMo; }
set { logMsgNo = value; }
}
public string RevNo
{ get { return revNo; }
set { revNo = value; }
}
public string ReqSox
{ get { return reqSox; }
set { reqSox = value; }
}
// Since your "DoSomething()" method was already proper case-sensitive
// format, you can just leave THIS version alone
public void DoSomething()
{ .. do whatever .. }
public IYourBarBazInterface MyBarBaz
{ get { return MyBarbaz; }
set { MyBarbaz = value; }
}
public List<IQuux> MyQuuxList
{ get { return myquuxlist; }
set { myquuxlist = value; }
}
}
Unfortunately, no. There's not. C# is case sensitive (including interfaces). To have them both conform to a single interface, the name case would have to match. If you did that, the classes would be the same anyway.
Your only option would be to create an interface that used one of the casing methods, implement it on both classes, and then add code to one class (with the naming convention you didn't chose) to pass through the calls:
public interface IFooBar
{
string LogMsgNo { get; set; }
string RevNo { get; set; }
string ReqSox { get; set; }
void DoSomething();
}
public class Foobar : IFooBar
{
public string Logmsgno;
public string Revno;
public string Reqsox;
public void Dosomething();
public string LogMsgNo
{
get { return Logmsgno; }
set { Logmsgno = value; }
}
// And so on
}
UPDATE
After seeing your edit, things become much more complex. You'll have to do the same thing to all of the inner classes and then have your interfaces reference the lower level interfaces. Same concept, just more work.
If I had to handle this, I would likely write an extension method to convert from one type to another. Some reflection would do most of the work. new Foobar().ToFooBar().ToFoobar() Or write a class I would always interact with and at the last point you need to access the right implementation, call the ToFoobar().
I have two functions that do the basically same thing on two different classes.... each class has different properties.
For example:
public class ClassA
{
public int ColorID {get;set;}
public string ColorDescription {get;set;}
}
public class ClassB
{
public int TypeID {get;set;}
public string TypeDescription {get;set;}
}
public void ExFunctionSaveA(ClassA aClass)
{
aClass.ColorID=1;
aClass.ColorDescription="My Color";
Save();
}
public void ExFunctionSaveB(ClassB bClass)
{
bClass.TypeID=2;
bClass.TypeDescription="My Type";
Save();
}
As you can see the classes and the functions have the same type structure, just the property names are different... but I feel like I am repeating code doing this
Is there a way to make ExFunctionA and ExFunctionB into one function, so that I could use this for all classes that have similar structure
I know I could do some sort of generic thing like
public void ExFunctionSave<T>() // T is either ClassA or ClassB
{
.
.
.
.
Save();
}
but how would I handle the properties of each
Rather than using a generic, why not use inheritance to solve this?
public class theBase
{
string ID;
string Description;
}
public class theColor : theBase
{
}
public class theType : theBase
{
}
public void ExFunctionSaveA(theBase base)
{
base.ID=1;
base.Description="My Color";
Save();
}
If you can alter the definitions of your classes, then the best approach would be to make them implement a common interface that contains the properties you want to access:
public interface IDescribable
{
int ID { get; set; }
string Description { get; set; }
}
public class ClassA
{
public int ID { get; set; }
public string Description { get; set; }
public int ColorID
{
get { return ID; }
set { ID = value; }
}
public string ColorDescription
{
get { return Description; }
set { Description = value; }
}
}
public class ClassB
{
public int ID { get; set; }
public string Description { get; set; }
public int TypeID
{
get { return ID; }
set { ID = value; }
}
public string TypeDescription
{
get { return Description; }
set { Description = value; }
}
}
public void ExFunctionSave(IDescribable d, int id, string desc)
{
d.ID = id;
d.Description = desc;
Save();
}
Nothing more you can do unless the the 2 classes implement the same interface which has the function. In your case, even the function signatures are different.
You could define an Interface with attributes id and description.
The clases that has this structure could implement that interface.
And your method receive as parameter the interface and execute the moethods ...
Take a look at Reflection.
Reflection will let your code receive a ClassA, and discover that it has a ColourID and a ColorDescription. Likewise, when you receive a ClassB, you can discover its TypeID and TypeDescription. It's cool.
I would probably recommend a common interface, at least for your example, but if you're trying to something more complex and more generic, Reflection is the way to go.
Assuming I have:
public abstract class A {
public abstract string Name { get; }
}
I want to use the "shortcut" for creating properties in the child class:
public string Name { get; set; }
Instead of:
private string _Name;
public string Name {
get{ return _Name; }
set{ _Name=Name; }
}
But this cause compilation error since I have set; there
public class B {
public override Name{get; set;}
}
How can I override only get;?
Just implement the getter:
public override string Name
{
get
{
return this.name;
}
}
private string name;
The shorthand for a simple get + set property is great if you want a simple get + set property, but if you want something more complicated (such as a property with just a getter) you need to implement it yourself.
Short answer: there is no shorthand way
public class B {
private string iName;
public override string Name{get{ return iName;}}
}
on a side note:
You could make your base class an interface and then override in the implementing class:
public interface A {
string Name { get; }
}
public class B : A {
public string Name {get;set;}
}