Pass an argument as an object?
Well, I made a game, and I have two MMControls on it, one to play background music, and one to play sound effects. Since so many parts of the program need to access these controls, I decided the entire music control into one public sub on a module, as shown here:
VB Code:
Public Sub SoundEffect(strSound As String, mmController As MMControl)
frmGameScreen.mmController.Command = "close"
frmGameScreen.mmController.Command = "Stop"
frmGameScreen.mmController.FileName = App.Path & "\sounds\" + Trim(strSound)
frmGameScreen.mmController.Command = "open"
frmGameScreen.mmController.Command = "play"
End Sub
The problem is when I try to call the procedure with a line like this:
VB Code:
Call SoundEffect("EXPLODE.WAV", mmMusicControl)
I get an error saying "Method or data member not found", is it not possible to do what I want to do without copying the code for both control twice? I want to avoid as much extra code as possible. Can somebody help me?
Re: Pass an argument as an object?
The problem is that you have specified a form in the sub - so the code is looking for a control called mmController on frmGameScreen.
To use the parameter instead, simply remove "frmGameScreen.", eg:
VB Code:
mmController.Command = "open"
Re: Pass an argument as an object?
Or add another parameter, the Form:
VB Code:
Public Sub SoundEffect(strSound As String, [B]frm As Form[/B], mmController As MMControl)
[B]frm[/B].mmController.Command = "close"
'...
End Sub
... and call it like this:
VB Code:
SoundEffect "EXPLODE.WAV", [B]frmName[/B], mmMusicControl
'or simply...
SoundEffect "EXPLODE.WAV", [B]Me[/B], mmMusicControl
Re: Pass an argument as an object?
That won't work.. as it is still looking for a control called mmController.
There is absolutely no need to use the Form anyway - as the control is an object in itself. If necessary the form can be specified when calling the sub, eg:
VB Code:
Call SoundEffect("EXPLODE.WAV", [u]Form1.[/u]mmMusicControl)
..note that this is only required if the control is on a different form to this line of code.
Re: Pass an argument as an object?
Both of the media controllers are on frmGameScreen, so the program is supposed to find them only on that form.
Re: Pass an argument as an object?
And that is how it will work.. they only exist on the form they are on. You do not need to specify the form within the SoundEffect sub itself, as it only works with the controls, not the form (the controls themselves hold a reference to the form, but it isn't needed). Your mmController parameter refers to the control which was specified, not the name of the control.
If you have the "Call" line in the code for frmGameScreen, use your original Call.
If the Call line is on a different form, simply use this:
VB Code:
Call SoundEffect("EXPLODE.WAV", frmGameScreen.mmMusicControl)
You only need to use the form name to differentiate the object when it is on a different form (within frmGameScreen's code, "frmGameScreen.mmMusicControl" is exactly the same as "mmMusicControl").