uber_timmy Posted February 9, 2009 Posted February 9, 2009 Hi all I have been porting a program from java to vb.net. In Java you can create class containing an abstract method and assign a method on creation of the class. I was wondering if something like this can be done in VB? Example: public abstract class myclass { abstract public void execute(); } //Code from another class static myclass myclassvar1 = new myclass () { public void execute() { //Stuff here } }; static myclass myclassvar2 = new myclass () { public void execute() { //Different stuff here } }; Quote
Administrators PlausiblyDamp Posted February 9, 2009 Administrators Posted February 9, 2009 Public Must Inherit class myclass public must override sub execute end class is what you are after. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
uber_timmy Posted February 9, 2009 Author Posted February 9, 2009 Hi PlausiblyDamp To my knowledge the only way to use that is use an other class to Inherit that class. For example when i declare myclassvar1 and myclassvar2, they need to be declared as myclass not the class i use to Inherit myclass. Quote
Administrators PlausiblyDamp Posted February 9, 2009 Administrators Posted February 9, 2009 Java isn't my strong point - just to check... The method isn't part of the abstract class or the class that inherits from it but is assigned at runtime - that correct? If so delegates are probably the best way to go. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
uber_timmy Posted February 9, 2009 Author Posted February 9, 2009 I need it to work something like this(I know this is dogy..sorry): Thanks in advance Public class myclass Public MustOverride Sub execute() end class Public class MainClass public myvar1 as myclass public myvar2 as myclass public sub init() myvar1 =new myclass myvar2 =new myclass myvar1.execute=me.execute1 myvar2.execute=me.execute2 end sub public sub execute1() 'code here end sub public sub execute2() 'code here end sub end class Quote
uber_timmy Posted February 9, 2009 Author Posted February 9, 2009 Thanks PlausiblyDamp...You Rule Public Class myclass1 Public Delegate Sub BlankDelegate() Public Execute As BlankDelegate End Class Private myvar1 As myclass1 Private myvar2 As myclass1 Sub test() myvar1 = New myclass1 myvar2 = New myclass1 myvar1.Execute = AddressOf sub1 myvar2.Execute = AddressOf sub2 myvar1.Execute() myvar2.Execute() End Sub Sub sub1() MsgBox("Sub1") End Sub Sub sub2() MsgBox("Sub2") End Sub Quote
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.