Diesel Posted October 2, 2003 Posted October 2, 2003 Does .NET have anything like a BASE CLASS POINTER? Quote
aewarnick Posted October 2, 2003 Posted October 2, 2003 You mean like when you override a function like OnPaint and then calls it's base? base. Quote C#
Diesel Posted October 2, 2003 Author Posted October 2, 2003 No. Say I have three Classes. Employee, Hourly Employee and Salaried Employee. Hourly and Salary are both inheritied from Employee. In C/C++ you can create a pointer to Employee: Employee * tmpEmp; and use it to reference objects of both Hourly and Salaried: tmpEmp = tmpHourlyEmp; tmpEmp = tmpSalariedEmp; I checked out the MSDN library, but the closest I came to were delegates. So, any ideas? Quote
*Gurus* divil Posted October 2, 2003 *Gurus* Posted October 2, 2003 Delegates have nothing to do with that. You can declare a variable of the type of your base class and assign an instance of any derived class to it, but calling the methods on it will still result in the derived methods being called. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Diesel Posted October 2, 2003 Author Posted October 2, 2003 Actually, you can't. If that would have been the case I wouldn't have posted. When I tried to assign an Hourly Employee instance to an Employee instance, the employee instance did not change. I could not access the data or methods of the Hourly Employee instance. Quote
*Gurus* divil Posted October 2, 2003 *Gurus* Posted October 2, 2003 Dim emp As Employee = myHourlyEmployee Do you have some problem with that? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted October 2, 2003 *Experts* Posted October 2, 2003 I'm not sure what you want, Diesel... If you declare a variable as Employee and set it equal to an instance of HourlyEmployee, of course you can't access the HourlyEmployee's members (if they're specific to HourlyEmployee). On the other hand, if HourlyEmployee overrides a method in Employee and you try and call the method on your Employee variable, it will STILL call the HourlyEmployee's version (even though the variable is of type Employee). That's not really what you asked, but I was clarifying what divil said. I think your original question was how do you assign a variable to the base class's type, which you've already done as soon as you did: HourlyEmployee hEmployee = new HourlyEmployee(); Employee emp = hEmployee; The second line is what you asked for, no? -Nerseus 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
Diesel Posted October 2, 2003 Author Posted October 2, 2003 I know what you two are saying...I tried that approach at first, and it doesn't work. As in the previous example, say if you had a variable HourlyPayRate in the HourlyEmployee class. Even after you set the Employee emp instance equal to the hEmployee instance, you still cant access that data member. I don't think you can assign derived classes to base classes in .NET. Thank you for your help though. Quote
Leaders Squirm Posted October 2, 2003 Leaders Posted October 2, 2003 (edited) Once more, since you don't seem to understand: HourlyEmployee hourEmp = new HourlyEmployee(); Employee emp = hourEmp; //Ok! emp.SomeMethodWhichIsPartOfEmployeeClass(); //Ok! emp.SomeMethodWhichIsPartOfHourlyEmployeeClass(); //Bad! ((HourlyEmployee) emp).SomeMethodWhichIsPartOfHourlyEmployeeClass(); //Ok! I hope you can see the difference here. By casting an HourlyEmployee to type Employee you can only call those methods which were inherited from Employee, you cannot call the 'new' HourlyEmployee methods without first casting back to HourlyEmployee. This is a design paradigm of OOP and is reflected in all languages. Can you show an example of when this isn't the case? To use a different example, imagine the 'thoughts' (for lack of better term) of the compiler when it hits this line: emp.SomeMethodWhichIsPartOfHourlyEmployeeClass(); //Compiler thinks: //hey! emp is an Employee, but there's no such method for Employee! //It doesnt KNOW that this Employee is actually an extended //class with that method, all it knows is that it is dealing with an //Employee and Employees cant do that! If you're still not convinced, please post the code which compiles in C++ but which cannot be reproduced in .Net. :) Edited October 3, 2003 by Squirm Quote Search the forums | Still IRCing | Be nice
Diesel Posted October 8, 2003 Author Posted October 8, 2003 Haven't checked the boards in a few days...but, what Im talking about is Polymorphism.... #include <iostream> using namespace std; class item { public: float getprice() {return price; } int getquantity() { return quantity; } virtual int getounces(); virtual void setounces(int); private: float price; int quantity; }; class pepsi: public item { public: int getounces() { return ounces; } void setounces(int oz) { ounces=oz;} private: int ounces; }; void main() { item *someitem; someitem = new pepsi; someitem->setounces(20); cout << someitem->getounces; } Quote
Administrators PlausiblyDamp Posted October 8, 2003 Administrators Posted October 8, 2003 (edited) Really not sure what your problem is with this I think VolteFace and Squirm explained it clearly but here you go: using System; namespace ConsoleApplication1 { abstract class item { public float getprice() {return price; } public int getquantity() { return quantity; } public abstract int getounces(); public abstract void setounces(int oz); private float price; private int quantity; }; class pepsi: item { public override int getounces() { return ounces; } public override void setounces(int oz) { ounces=oz;} private int ounces; }; /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [sTAThread] static void Main(string[] args) { item someitem; someitem = new pepsi(); someitem.setounces(20); Console.WriteLine(someitem.getounces()); } } } Edited October 8, 2003 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Camaro Z28 Posted October 8, 2003 Posted October 8, 2003 Imports System Namespace ConsoleApplication1 MustInherit Class item Public Function getprice() As single Return price End Function Public Function getquantity() As Integer Return quantity End Function Public abstract Integer getounces() Public abstract void setounces(Integer oz) Private price As single Private quantity As Integer End Class Class pepsi Implements item Public Overrides Function getounces() As Integer Return ounces End Function Public Overrides Sub setounces(ByVal oz As Integer) ounces=oz End Sub Private ounces As Integer End Class '/ <summary> '/ Summary description for Class1. '/ </summary> Class Class1 '/ <summary> '/ The main entry point for the application. '/ </summary> <STAThread> _ Shared Sub Main(ByVal args() As String) Dim someitem As item someitem = New pepsi() someitem.setounces(20) Console.WriteLine(someitem.getounces()) End Sub End Class End Namespace '---------------------------------------------------------------- ' Converted from C# to VB .NET using CSharpToVBConverter(1.2). ' Developed by: Kamal Patel (http://www.KamalPatel.net) '---------------------------------------------------------------- This may not be a perfect convert to VB.Net, but it should get you started. BTW, There are a ton of c# to vb.net converters online. Quote Visual Basic Code Source FREE Visual Basic Source Code
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.