-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    
Sum
  • 53
  • 5
  • Question is unclear, but you probably should look at this question https://stackoverflow.com/questions/14709263/import-code-from-text-vb-net/14711110#14711110 – Steve Apr 30 '18 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 – Sum Apr 30 '18 at 20:33
  • Check out Extension methods. – Mary Apr 30 '18 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... – Sum Apr 30 '18 at 21:27
  • @Mary That alone won't help, extension methods are just syntactic sugar for pretending a method belongs to a different class. – jrh Apr 30 '18 at 21:46
  • If you don't want to open a class to add a method, then use extension methods. – Mary Apr 30 '18 at 21:48
  • @Mary: nah, well, don't open a class and don't type anything too. you can see my detail above :D this look like a challenge in codefight.com haha – Sum Apr 30 '18 at 21:51
  • @Mary the way I read it, I don't think that's what the OP is looking for, sounds like they are looking to (at runtime) compile a new method and add it to the class. "if we're able to let the program write function by itself then it'll be very interesting to see.", "I want to add to module A I read some post those are on-fly complier." – jrh Apr 30 '18 at 21:51
  • Fine, it was just a one line comment! – Mary Apr 30 '18 at 21:52
  • @Mary: you did well, need more active member like you <3 – Sum Apr 30 '18 at 21:53

1 Answers1

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 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 ^^ – Sum Apr 30 '18 at 21:42