Click here to Skip to main content
15,888,610 members
Home / Discussions / C#
   

C#

 
Questionplaying media from server storage help.. in C# Pin
AnthonyRaven11-Jul-17 17:10
AnthonyRaven11-Jul-17 17:10 
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 
So, let's assume you have this:
C#
public class Shape
    {
    public virtual void Draw()
        {
        Console.WriteLine("Drawing in Shape");
        }
    }
public class Rectangle : Shape
    {
    public int Ratio { get; set; }
    public override void Draw()
        {
        Console.WriteLine("Drawing in Rectangle");
        }
    }
public class Circle : Shape
    {
    public int Radius { get; set; }
    public override void Draw()
        {
        Console.WriteLine("Drawing in Circle");
        }
    }

When you write this:
C#
Shape s = new Rectangle();
s.Draw();
It works; this prints:
Drawing in Rectangle
Because the system "knows" that the object instance that s refers to is a Rectangle when it looks at runtime to find the most appropriate method.
Similarly, this:
C#
Shape s2 = new Circle();
s2.Draw();
will print:
Drawing in Circle
Becuase it looks at that for the Circle version of the method.
But ... s is a Shape, so it can refer to one of three classes: Shape, Rectangle, or Circle, so if you write
C#
Console.Writeline("{0}", s.Ratio);
Or
C#
Console.Writeline("{0}", s.Radius);
What can the system do? If s contains a reference to a Rectangle, the first code is fine, but the second fails, and if s contains a Circle then the first fails, and the second succeeds - if s has a Shape, then they both fail!

When you declare a variable, what you can do with it depends on the type of the variable, not what it contains. And because s is a Shape, you can only access Shape methods, fields, and properties via it. Declare your variable as the appropriate type, and you can do what you need:
C#
Rectangle r = new Rectangle();
Circle c = new Circle();
Shape s = r;
s.Draw();
s = c;
s.Draw();
Console.WriteLine("{0}", r.Ratio);
Console.WriteLine("{0}", c.Radius);

Make sense now?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

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 
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 

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.