Click here to Skip to main content
15,886,724 members
Home / Discussions / C#
   

C#

 
GeneralRe: Ignore Error Pin
OriginalGriff12-Jun-16 21:05
mveOriginalGriff12-Jun-16 21:05 
QuestionSystem.NullReferenceException Pin
sunsher8-Jun-16 12:48
sunsher8-Jun-16 12:48 
AnswerRe: System.NullReferenceException Pin
Matt T Heffron8-Jun-16 13:56
professionalMatt T Heffron8-Jun-16 13:56 
AnswerRe: System.NullReferenceException Pin
BillWoodruff8-Jun-16 13:58
professionalBillWoodruff8-Jun-16 13:58 
GeneralRe: System.NullReferenceException Pin
sunsher8-Jun-16 18:57
sunsher8-Jun-16 18:57 
GeneralRe: System.NullReferenceException Pin
sunsher8-Jun-16 19:10
sunsher8-Jun-16 19:10 
GeneralRe: System.NullReferenceException Pin
Pete O'Hanlon8-Jun-16 21:15
mvePete O'Hanlon8-Jun-16 21:15 
GeneralRe: System.NullReferenceException Pin
BillWoodruff8-Jun-16 21:54
professionalBillWoodruff8-Jun-16 21:54 
As Pete commented, the code you show here won't compile.

A helpful metaphor could be: an Interface is like the blue-print for a house; the house is not a blue-print; and, the blue-print is not a house ... it makes no sense to try and combine a blue-print with a house. Yet, we can say there is a relationship between a blue-print and the house whose construction implemented the design specified in that blue-print.

But, to really describe what is happening in C#, we need to add one more ingredient to the metaphor, the Class (or Struct) as "factory:" the Interface is a blue-print for the Class, and the Class becomes a "factory" when you use it to create new instances of the Class. The house embodies, in a way, the blue-print, but it is neither blue-print, nor the workshop and construction tools used to build it.

Interfaces are useful for many things:

1. they are a contract which Classes or Structs that inherit from the Interface must "fulfill," or implement.

1.a. they assist you in organizing your code

2. they contribute to encapsulation and separation of concerns by allowing you to "hide" certain views of object instances of the Interface by casting them to the Interface. When cast to the Interface, the only "visible" parts of the instances are those which are specified in the Interface and implemented in the Class.

That allows you to pass the Class instance object to other Classes for processing with the guarantee that you cannot change/mess-with the other (now "hidden") Fields, Properties, Methods, etc., of the Class.

So, Interfaces are a way to control what a Class exposes.

3. Interfaces, combined with generic arguments allow you to create 'Factory objects which can produce instances of Classes with varying Types (this is an advanced topic).

Keep in mind that your design of Classes, Structs, Interfaces, Enums, etc. are all ways of expressing your intent. Hopefully, you intent directly expresses your goal: how you want your program to function; how you want your user-interface to handle asynchronous events, etc.

Here's a "guess" on what I think you may be intending:

C#
public interface ISettings
{
    string Color { get; set; }
}
 
public interface IDimensions
{
    int Height { set; get; }
}

public class Settings : ISettings
{
   string Color { get; set; }
}
At this point you have several choices; you could have your 'Dimensions Class inherit from both ISettings and IDimensions:

public class Dimensions : ISettings, IDimensions

Or, you could have it inherit from the 'Settings Class and IDimensions:

public class Dimensions : Settings, IDimensions

In the first case, 'Dimensions would now have to implement a 'Color Property as well as a 'Height Property; in the second case, 'Dimensions would automatically use the 'Height Property defined in the 'Settings Class.

Using the first example: you would then have two options to Cast an instance of the Dimension Class to an Interface "view," or "type:"

// assume you have a 'ctor that takes two parameters
Dimension myDimension = new Dimension(100, Color.AliceBlue);

ISettings myDimensionAsISettings = myDimension as ISettings;

IDimension myDimensionAsIDimension = myDimension as IDimension;

You now effectively have two "restricted views" of the Class instance 'myDimension; you can pass the 'ISettings view to some other Class to use, perhaps modify, and be assured that whatever happens cannot affect the value of the 'Height Property.

Think about your intent, your goal, and keep experimenting and studying; in my experience (as a teacher), it does take time and repeated multiple passes over what 'Interfaces are, and how they work, for it to "sink in" Smile | :)
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

Questionpassword policy using c# Pin
forest4ever7-Jun-16 20:33
forest4ever7-Jun-16 20:33 
AnswerRe: password policy using c# Pin
Garth J Lancaster7-Jun-16 20:44
professionalGarth J Lancaster7-Jun-16 20:44 
AnswerRe: password policy using c# Pin
Mycroft Holmes7-Jun-16 22:19
professionalMycroft Holmes7-Jun-16 22:19 
AnswerRe: password policy using c# Pin
OriginalGriff7-Jun-16 22:28
mveOriginalGriff7-Jun-16 22:28 
QuestionSet window on top from ShellExecute? Pin
kenw2327-Jun-16 11:25
kenw2327-Jun-16 11:25 
AnswerRe: Set window on top from ShellExecute? Pin
Eddy Vluggen7-Jun-16 12:10
professionalEddy Vluggen7-Jun-16 12:10 
AnswerRe: Set window on top from ShellExecute? Pin
BillWoodruff8-Jun-16 7:26
professionalBillWoodruff8-Jun-16 7:26 
QuestionHow can I format a text selection in bold AND italic? Pin
Member 119167356-Jun-16 23:23
Member 119167356-Jun-16 23:23 
AnswerRe: How can I format a text selection in bold AND italic? Pin
OriginalGriff6-Jun-16 23:39
mveOriginalGriff6-Jun-16 23:39 
GeneralRe: How can I format a text selection in bold AND italic? Pin
Member 119167357-Jun-16 0:02
Member 119167357-Jun-16 0:02 
GeneralRe: How can I format a text selection in bold AND italic? Pin
OriginalGriff7-Jun-16 0:20
mveOriginalGriff7-Jun-16 0:20 
GeneralRe: How can I format a text selection in bold AND italic? Pin
Member 119167357-Jun-16 0:23
Member 119167357-Jun-16 0:23 
GeneralRe: How can I format a text selection in bold AND italic? Pin
OriginalGriff7-Jun-16 0:45
mveOriginalGriff7-Jun-16 0:45 
Questionsend an acknowledgement in serial port listner Pin
Member 125557476-Jun-16 21:00
Member 125557476-Jun-16 21:00 
AnswerRe: send an acknowledgement in serial port listner Pin
Richard MacCutchan6-Jun-16 21:16
mveRichard MacCutchan6-Jun-16 21:16 
GeneralRe: send an acknowledgement in serial port listner Pin
OriginalGriff6-Jun-16 21:56
mveOriginalGriff6-Jun-16 21:56 
GeneralRe: send an acknowledgement in serial port listner Pin
Richard MacCutchan6-Jun-16 22:11
mveRichard MacCutchan6-Jun-16 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.