Click here to Skip to main content
15,889,403 members
Home / Discussions / C#
   

C#

 
AnswerRe: playing media from server storage help.. in C# Pin
Richard MacCutchan11-Jul-17 21:42
mveRichard MacCutchan11-Jul-17 21:42 
QuestionRfid Reader Tag number Extraction Pin
Member 132923109-Jul-17 4:18
Member 132923109-Jul-17 4:18 
AnswerRe: Rfid Reader Tag number Extraction Pin
Michael_Davies9-Jul-17 4:40
Michael_Davies9-Jul-17 4:40 
QuestionWhy can't I access a variable declared in derived class with the object of base class Pin
Member 132999538-Jul-17 7:02
Member 132999538-Jul-17 7:02 
AnswerRe: Why can't I access a variable declared in derived class with the object of base class Pin
PIEBALDconsult8-Jul-17 7:10
mvePIEBALDconsult8-Jul-17 7:10 
AnswerRe: Why can't I access a variable declared in derived class with the object of base class Pin
Dave Kreskowiak8-Jul-17 7:27
mveDave Kreskowiak8-Jul-17 7:27 
AnswerRe: Why can't I access a variable declared in derived class with the object of base class Pin
OriginalGriff8-Jul-17 19:07
mveOriginalGriff8-Jul-17 19:07 
AnswerRe: Why can't I access a variable declared in derived class with the object of base class Pin
BillWoodruff9-Jul-17 23:02
professionalBillWoodruff9-Jul-17 23:02 
C# provides flexible ways to constrain a Class to being a provider of data, and methods, to other Classes that inherit from it. These include virtual Methods, abstract Classes, and Interfaces. The inheriting instances of a Class have special semantics for declaring their members that implement the inherit-from Class' members: the use of 'override, and 'new.

If you are confused by this wealth of possibilities, do not feel alone Smile | :) It takes most folks time and effort to master them, and choose which techniques are most useful in a given circumstance/problrm/scenario.

OriginalGriff's example uses a virtual method in a Public Class; in that example you could create an instance of 'Shape using 'new.

A further constraint can be achieved using an abstract Class: you cannot create an instance of an abstract Class. In your case, I think 'Shape is, indeed, an abstraction, and, creating an instance of it would have no purpose.

An example of using an abstract Class:
public abstract class Shape
{
    public virtual void Draw(string txt = "Shape")  // Note 0
    {
        Console.WriteLine(txt);
    }
}

public class Rect : Shape
{
    public string Name = "Rect";

    public int Ratio { get; set; }

    public void Draw(String txt = "Rect")
    {
        base.Draw(txt);
    }
}

public class Circle : Shape
{
    public string Name = "Circle";

    public int Radius { get; set; }

    public void Draw(String txt = "Circle")
    {
        base.Draw(txt);
    }
}
Let's test this:
// error: cannot create an instance of an abstract class
// Shape newShape = new Shape();

Rect newRectangle = new Rect();
Circle newCircle = new Circle();

newRectangle.Draw();
newCircle.Draw();

Shape shapeAsRectangle = newRectangle;  // Note 1
Shape shapeAsCircle = newCircle;

shapeAsRectangle.Draw();
shapeAsCircle.Draw();

Rect rectBackFromShape = shapeAsRectangle as Rect;  // Note 2
Circle rectBackFromCircle = shapeAsCircle as Circle;

rectBackFromShape.Draw();
rectBackFromCircle.Draw();

Console.WriteLine(newRectangle == shapeAsRectangle);   // true
Console.WriteLine(newCircle == shapeAsCircle);         // true
Console.WriteLine(newRectangle == rectBackFromShape);  // true
Console.WriteLine(newCircle == rectBackFromCircle);    // true
Notes:

0. to be used by inheriting classes this method in the abstract class must be 'public, which means it can be called on an instance of an inheriting class down-cast to the abstract parent's Type. imho, this is less than ideal.

1. even though we can't create an instance of an abstract class, we can "change the view" we have by casting it to its abstract base Type ... when cast, the data, or methods, specific to the inherited class are not "lost" !

2. here you see we can re-hydrate an instance cast to its abstract base Type back to its "native" Type, and, now we have access again to its data, and methods.
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

AnswerRe: Why can't I access a variable declared in derived class with the object of base class Pin
Gerry Schmitz11-Jul-17 6:48
mveGerry Schmitz11-Jul-17 6:48 
SuggestionRe: Why can't I access a variable declared in derived class with the object of base class Pin
Richard Deeming11-Jul-17 7:01
mveRichard Deeming11-Jul-17 7:01 
Questionprinter default font. Pin
rahul19957-Jul-17 1:53
rahul19957-Jul-17 1:53 
AnswerRe: printer default font. Pin
Dave Kreskowiak7-Jul-17 2:25
mveDave Kreskowiak7-Jul-17 2:25 
AnswerRe: printer default font. Pin
Pete O'Hanlon7-Jul-17 2:53
mvePete O'Hanlon7-Jul-17 2:53 
GeneralRe: printer default font. Pin
OriginalGriff7-Jul-17 4:11
mveOriginalGriff7-Jul-17 4:11 
GeneralRe: printer default font. Pin
Richard MacCutchan7-Jul-17 4:22
mveRichard MacCutchan7-Jul-17 4:22 
GeneralRe: printer default font. Pin
OriginalGriff7-Jul-17 4:30
mveOriginalGriff7-Jul-17 4:30 
GeneralRe: printer default font. Pin
Richard MacCutchan7-Jul-17 5:19
mveRichard MacCutchan7-Jul-17 5:19 
AnswerRe: printer default font. Pin
Richard Andrew x647-Jul-17 7:41
professionalRichard Andrew x647-Jul-17 7:41 
GeneralRe: printer default font. Pin
rahul19959-Jul-17 18:48
rahul19959-Jul-17 18:48 
AnswerRe: printer default font. Pin
jschell10-Jul-17 4:39
jschell10-Jul-17 4:39 
GeneralRe: printer default font. Pin
rahul199510-Jul-17 19:02
rahul199510-Jul-17 19:02 
GeneralRe: printer default font. Pin
Dave Kreskowiak11-Jul-17 3:30
mveDave Kreskowiak11-Jul-17 3:30 
Questionusing Form does not release memory Pin
Montgomery-Burns6-Jul-17 22:12
Montgomery-Burns6-Jul-17 22:12 
AnswerRe: using Form does not release memory Pin
OriginalGriff6-Jul-17 23:59
mveOriginalGriff6-Jul-17 23:59 
GeneralRe: using Form does not release memory Pin
Montgomery-Burns7-Jul-17 0:15
Montgomery-Burns7-Jul-17 0:15 

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.