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
Related
Disclaimer: I'm very new to RSpec and TDD in general, so I may be going about this the wrong way entirely.
I want to write a command-line interface to my program which takes a command and generates a class to handle it. The way the program is intended to work is similar to tools like git and svn; i.e. you could pass "srs init" to initialise the program, "srs add" to add something to it, and so forth.
So I have a class which takes ARGV and passes it off to a specific handler, which looks like this:
class CLI
def run!(*arguments)
command = arguments.shift
case command
when "init"
CLI::Init.new.run!(*arguments)
end
end
end
My Init handler would then look like this:
class CLI
class Init
def initialize()
end
def run!(*arguments)
end
end
end
I am trying to write a test suite for the CLI class's routing functionality. RSpec fails if I use the following:
describe CLI do
it "should launch the Init handler if we pass init" do
CLI::Init.any_instance.should_receive(:run!)
CLI::run!(*["init"])
end
end
However it passes if I replace the call to CLI::run! with a direct call to the Init handler's run; i.e.:-
describe CLI do
it "should launch the Init handler if we pass init" do
CLI::Init.any_instance.should_receive(:run!)
CLI::Init.new.run!(*[])
end
end
It looks as if any_instance only works on instances defined/constructed within the describe block, but I'm not really sure. If anyone can offer me any guidance either on how I can check that a class method has been called on an instance constructed inside my run! function, or on a better way to test this functionality in the first place, I'd be most appreciative.
Sometimes explaining the problem reveals the answer. In actual fact, the name of the handler, "Init" was being passed as a parameter to the describe block, more like this:
%w{Init}.each do |cmd|
describe CLI do
it "should launch the #{cmd} handler if we pass #{cmd}" do
CLI.const_get(cmd).any_instance.should_receive(:run!)
CLI::run!(*[cmd])
end
end
end
In describing the problem I took out the loop to simplify the question, but in doing so made a crucial change -- the name of the class, Init, began with a capital "I", while the name of the command passed to the command line, init, begins with a small "i".
So it turned out the test failed correctly, because I tried to pass the command "Init" when I should have been passing the command "init".
TL;DR -- the original code does actually work! Sorry for the bother.
In a Visual Basic 5 file (.vbd), how do I call userDocument terminate from userDocument Hide?
Option Explicit
Private Sub Command1_Click()
MsgBox "hello World"
End Sub
Private Sub UserDocument_Hide()
MsgBox "Before Termination"
<I want UserDocument termination here>
MsgBox "After Termination"
End Sub
While you can call the same code that occurs on termination, you can't actually terminate the object.
Object termination occurs when all references to it are released, and implicitly all code running in it exits.
For what you want to do, you have to get whatever created/is using your UserDocument to release it.
You may be able to do this by putting an event in your object and calling it from the Hide event that that caller listens for and releases it.
Obviously, if the caller is not your code, you can not do this.
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
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.
I have some modules in an array. All the modules define a method called "process", and I'd like to call each of these process methods in sequence. The code I have looks something like this(assume the modules are all defined inside the Mod class):
modules.each do |mod|
extend Mod.const_get(mod)
process(data)
end
This works fine for the first time, but the method doesn't get overwritten after the first pass of the loop. I've tried adding undef process as the last line inside the each block but that didn't work.
Is there any way I can do this?
modules can only be included into an inheritance chain once.
Also, what are you doing is really weird, you should think about redesigning your system.
is making the 'process' method into a module-method an option (by defining it def self.process)?
If so, sending the method 'process' directly to the returned constant would work:
modules.each do |mod|
Mod.const_get(mod).send(:process, data)
end
EDIT
Come to think of it, why not call the method directly?
Mod.const_get(mod).process(data)