In vb6, i can do :
set object=new class
where object is a Object and Class is a class defined in the code.
Now, i want to do the same dynamically, i want to do something like:
set object=createobject("class")
but it fail because createobject is apparently for activex registered class and not class modules.
If you put the class in question in a separate VB6 OCX, you will be able to use createObject to create them on-the-fly.
I hope the reason you want to do this is to mimic some sort of interface-like functionality, otherwise it's probably not an ideal solution.
Anyway, you could create a method that gives back a different class depending on the string you provide.
function myClassCreatingFunction(className)
select className
case: "Class1"
set myClassCreatingFunction = new Class1
exit function
...
end select
end function
Related
consider the following example
module DataMapper
class Property
class CustomType < DataMapper::Property::Text
def load(value)
# do stuff and return formatted value
end
end
end
end
Class A
property :name, String
property :value, CustomType
end
now when I do A.first or A.first.value the load method gets executed, but the calculations that I need to do inside load is dependent on that instance's name property. So how do I get the context of this instance/resource(as referred inside source code) inside the load method ?
Please let me know if the question is not clear yet !
You are attempting to break encapsulation. The name and value are different properties, and thus each should be in ignorance of the other's existence, let alone value.
The correct solution is to move the "stuff" to an object which has visibility over both properties. The two options are:
the A class (as suggested by user1376019); or
a complex data type, e.g. NameAndValue < DataMapper::Property::Object, which encapsulates both properties.
If you need to perform aggregate functions over the individual properties, the second option would not work, unless you can somehow override the complex property to have multiple fields.
In either case, value cannot refer to name without a reference to it.
I've googled for articles on VB6's support for polymorphism, but all of the articles that I read merely mentioned that you can make a derived class inherit properties and methods from a derived class. None of the three or four articles that I read mentioned whether or not VB6's polymorphism allowed you to pass the derived classes as parameters of the base class type.
If you were to have a class named Bunny, which was derived from a class named Animal, could you pass a variable of type Bunny to the following function?
Public Sub Chase(thePerson as Person, theAnimal as Animal)
Do While thePerson.position <> theAnimal.position
...
Loop
End Sub
If your Bunny class Inherits from Animal then yes you should be able to do what you've stated in your sample code.
I know about controller_name returning a string containing the controller's name but how can I retrieve the controller class (or object) from within a helper?
EDIT: The solution should also work when the controller is namespaced (eg. Admin::PostsController)
You can use the constantize method, like:
controller_name.constantize
Though I'm not sure how it will behave if you have a namespaced controller.
Update:
That one won't work for all controller names and/or namespaces. Though one can use the #controller method in combination with #class:
controller.class
A view probably shouldn't need to do this. Ideally whatever you're trying to do in the view that expects this, you would instead do in the controller.
Trying to think about why you'd want to do this, the best answer I can think of is that you want to invoke a helper method you've defined in the controller. There already exists a construct to do this, use helper_method.
For pretty much anything else, the controller should provide that data to the view. Not the view pulling it out of the controller. (e.g. even though you shouldn't need the class, the controller could provide it with #controller_class = self.class, which would then be available to the view)
In pure Ruby, because class names are constants, you can do this to get the class from a string:
classname = 'Posts'
p Kernel.const_get(classname).methods
There is a nice shortcut in Rails, constantize for just this:
p 'Posts'.constantize.methods
If the classname is eg 'editable_file', first call the camelize method:
p 'editable_file'.camelize.constantize # EditableFile
p 'extensions/editable_file'.camelize.constantize # Extensions::EditableFile
EDIT: If you really want to get the controller name un-demodulized, then this code in config/initializers/controller_name.rb should ensure it:
class ActionController::Metal
def self.controller_name
# #controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
#controller_name ||= self.name.sub(/Controller$/, '').underscore
end
end
I'd like to create an object... say a "Movie" object. The object should have a method name "Stop", so I can have this code below
dim Mov as new Movie
Mov.Stop ' To execute the Stop method.
In my Movie class, I should have something like this.
Sub Stop()
'code here needed for the Stop subroutine
End Sub
However, I can't use "Stop" as name as this is a reserved word. I see a VB code that has "Stop" as one of the method. Unfortunately, the code is protected so I can't view it.
How can I name a subroutine as "Stop"?
It might work if you created a typelib with Stop and your other methods and properties and then use Implements in your class. I haven't tested this though.
Update:
See Tutorial: Using Reserved Words as VB6 Class Member Names
Stop is a statement used to break the app when debugging (same as setting a breakpoint) so choose another name.
Stop is a keyword in Vb6.
You will have to rename your method to something different.
Say MovieStop
try this:
Sub [Stop]()
' code
end sub
Is it possible to create an instance of a class in VB 5 CCE from a string containing the class's definition?
Um... I'm actually using VB 5 CCE because I'm too cheap to get a version that costs money. Can someone tell me how to implement it that way?
TYVM to whoever answers this!!!!!
NVM... I'm downloading VS 2008 Express...
Dim obj As Object
Set obj = CreateObject("ClassName")
If you mean something like given a string with the following value: "Public Class Foo:Public Sub Bar():End Sub:End Class" and you want something like Dim the class = Eval(theString) and do something with your new class, see this.
They do it by extending it in VBScript.