-2

in VBA, it support us to add/edit code programatically in a module through VBE.

Do we have any similar method in VB.NET or any other language ?

if we're able to let the program write function by itself then it'll be very interesting to see.

For example:

  1. I have module A, it have function test() inside now I have code-string of function Hello() I want to add to module A I read some post those are on-fly complier.

  2. I have Class School, which have method AddStudent() Now I saw a usefull method AddTeacher() somewhere in the internet. I don't wanna open class School then copy/paste or type those code of method AddTeacher().

    Is there anyway I can add that method into Class School like this:

    Dim Harvard as new School
    Harvard.addMethod "string code of method AddTeacher()"
    Harvard.addTeacher "Sarah", "Math"
    

More detail:
I have class School

Public Class School
  Public Function AddMethod(strCode$)
  'magic method
  End Function

  Public Function AddStudent(ClassName$, Number%)
  End Function
End Class

after I run the 2nd example (I don't know exactly way)
class School mutate into this:

Public Class School
  Public Function AddMethod(strCode$)
  'magic method
  End Function

  Public Function AddStudent(ClassName$, Number%)
  End Function

  Public Function AddTeacher(Name$, subject$)
  End Function
End Class    
10
  • Question is unclear, but you probably should look at this question stackoverflow.com/questions/14709263/… Commented Apr 30, 2018 at 20:10
  • I've read that, it similar to on fly complie right ? btw, that may be a work around, do we have any direct method ? I mean for example I have module A with function Test() inside it, now I want to add function Helloworld() into module A without typing it line by line in Visual studio, how to do that ? :D Commented Apr 30, 2018 at 20:33
  • Check out Extension methods. Commented Apr 30, 2018 at 21:08
  • @Mary: "Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type". Extension is good method, it look like a temporary interface for instances. but we have to type or copy/paste code for that new extension... Commented Apr 30, 2018 at 21:27
  • @Mary That alone won't help, extension methods are just syntactic sugar for pretending a method belongs to a different class. Commented Apr 30, 2018 at 21:46

1 Answer 1

1

There are several ways to do this:

There are probably others. However, you won't find anything as simple as the Eval() method you may have seen in some other platforms, and this omission is intentional. .Net is a full-featured platform, intended to be used in a wide variety of places. An Eval()-like mechanism has the ability to do anything you could possibly write a .Net program to do... including some things that aren't very nice.

Sign up to request clarification or add additional context in comments.

1 Comment

so you mean python or other language can do what I ask for ? I start to research on AI, I have to ask before build a custom platform for this. it just a hobby btw ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.