Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Please help!

  • 12-02-2010 10:34am
    #1
    Closed Accounts Posts: 324 ✭✭


    Does anyone know in VB, can you override a method "on the fly" like you can in java?

    Like in java, you can do:

    MyClass blah = new MyClass() {

    Protected void methodToOverRide() {
    // actually do something different in here to what is actually in the method in MyClass
    }

    };

    Where MyClass is a class you have already, but you're creating a new instance of it on the fly and overriding one of its methods that you need to change.

    How the fup do you do this in vb :D?

    Thanks!


Comments

  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    I don't know much about Java, but I gather that's an anonymous class system of some sort that it supports, AFAIK .NET (I'm assuming you mean VB.NET) doesn't support anonymous classes like that. There may be some arcane way of achieving that using the System.Reflection namespace, but I wouldn't care to speculate :)
    There is a system for "extension methods" in C# (VB too I assume), but you can only use that to stick stuff on to existing classes, not override things. Another point to note is that if you do subclass the existing version, unlike (AFAIK) Java, methods aren't virtual by default in .NET.

    Edit; I gather those kind of anonymous classes are usually used for event handling behaviours in Java - if you want to deal with that kind of thing in .NET, "delegates" would be the mechanism to use, Google will turn up lots on that.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Would Lambda Expressions be of any use?


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    Hi thanks for the suggestions, in the end I just coded it as an inner class. Probably just as good because even if I did figure out a way to do this there's no guarantee another person will be able to read it easily
    NeverSayDie: it's a thing you can do in java to define an anonymous class on the fly. It's very handy but it's definitely a specific java trick, it would be different in other languages...


  • Registered Users, Registered Users 2 Posts: 3,766 ✭✭✭Reku


    Does anyone know in VB, can you override a method "on the fly" like you can in java?

    Like in java, you can do:

    MyClass blah = new MyClass() {

    Protected void methodToOverRide() {
    // actually do something different in here to what is actually in the method in MyClass
    }

    };

    Where MyClass is a class you have already, but you're creating a new instance of it on the fly and overriding one of its methods that you need to change.

    How the fup do you do this in vb :D?

    Thanks!

    What you're describing there is not overriding. To override a method you need to create an alternative version of the method in a subclass of the class with the original method in it.
    e.g.

    Class blah extends MyClass
    {

    Protected void methodToOverRide()
    {
    // actually do something different in here to what is actually in the
    method in MyClass
    }
    }

    With what you've given above in order to call the method in MyClass you need to call blah.methodToOverRide(); from the class, which to the system is completely different to just methodToOverRide();

    The following page seems to show how to do the same in visual basic:
    http://www.startvbdotnet.com/oop/class.aspx

    Altered to match your example I think that would be (I don't know visual basic:( hopefully someone who does can fix any errors)

    Dim blah As New MyClass()
    'creating a object blah for MyClass class

    Sub methodToOverRide()
    'actually do something different in here to what is actually in the method in
    'MyClass
    End Sub


Advertisement