Weste Posted April 17, 2006 Posted April 17, 2006 I am attempting to learn OOP. The book I am using has the examples in C++. I am attempting to modify the code in C#. .Net doesn't like Person:Display(); in the Student class below. Can someone tell me how this should be modified with C#. Thanks. using System; namespace LevelInheritance { /// <summary> /// Summary description for Person. /// </summary> public class Person { protected int m_ID; protected string m_First; protected string m_Last; public Person() { m_ID = 0; m_First = "\0"; m_Last = "\0"; } public virtual void Display() { Console.WriteLine("ID: " + m_ID + "\rFirst: " + m_First + "\rLast: " + m_Last); } public void Write(int ID, string First, string Last) { m_ID = ID; m_First = First; m_Last = Last; } } class Student: Person { protected int m_Graduation; public new virtual void Display() { Person:Display(); Console.WriteLine("Graduation: " + m_Graduation); } public void Write(int ID, string First, string Last, int Graduation) { Person:Write(ID, First, Last); m_Graduation = Graduation; } public Student() { m_Graduation = 0; } } Quote
Administrators PlausiblyDamp Posted April 17, 2006 Administrators Posted April 17, 2006 (edited) Try public override void Display() { Person:Display(); Console.WriteLine("Graduation: " + m_Graduation); } Edited April 18, 2006 by Nerseus Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted April 18, 2006 Posted April 18, 2006 It looks like something wigged out in your code block, PlausiblyDamp. Were your recomending Person.Display()? Wouldn't you need to do base.Display() becuase Display is a non-static method? public override void Display() { base.Display(); Console.WriteLine("Graduation: " + m_Graduation); } The only colon format I know of is used for labeling goto's so as far as I know, Person:Display() will not work. Quote
Administrators PlausiblyDamp Posted April 18, 2006 Administrators Posted April 18, 2006 My bad - I just copied, pasted and tweaked the code without checking it... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted April 18, 2006 *Experts* Posted April 18, 2006 About mskeel's comment ("...wigged out..."), I edited PM's message to turn off smilies. The line "Person:Display();" was interpreted as ":D" and, I guess in the code brackets, turned it into a big mess! -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.