tags:

views:

120

answers:

4

I have this line in the declarations section:

Private filePath As String

And somthing like this below:

Public Sub Print(filePath As String)
...
End Sub

In the ... part, I want to assign the parameter filePath to the module-level filePath. But how can I get access to the latter one? Thanks a lot.

+5  A: 

How about changing the code, to avoid confusion?

Private mFilePath As String

Change either the module level variable or the argument to the print function.

shahkalpesh
I often do this purely because of the public/private distinction with the Me variable. "Me" is no "this" unfortunately.
Joel Goodwin
The name of the parameter isn't defined by me. I can't touch it. If there's really no way, I could change the name of the class variable. However, the naming convention applied in this project isn't for it.
+1  A: 

Phoenie I don't think there's anyway to look at the private class variable in VB6/VBA once you have something in local scope that hides it (I had a good old Google search, but nothing came up). If you want to retain the naming convention and not change the existing interface, the only workaround is to develop a private function to access the variable for you. e.g.

Public Sub Print(filePath As String)

    SetMyFilePath filePath

End Sub

Private Sub SetMyFilePath(ByVal passedFilePath as String)

    filePath = passedFilePath

End Sub
Joel Goodwin
So although VB6 fails me, a little sub will do the trick. You really understand my situation. Thanks!
This seems like one nasty hack to me. Is there a reason you don't have data of module scope prefixed (a la mFilePath) in the first place? This is more common practice and it arose for just this reason. http://msdn.microsoft.com/en-us/library/aa262310(VS.60).aspx is just one topic in the VB6 documentation that advises this approach to writing Class modules.
Bob Riemersma
Bob, I think phoenie had red tape to cut through regarding making the correct change. I wouldn't implement this workaround myself and I upvoted shahkalpesh's answer - but I doubt phoenie is alone in making choices of expediency over red tape.
Joel Goodwin
A: 

If you can't change the name of the parameter in your function you can always define another function:

Public Sub SetfilePath(m As String)
  filePath = m
End Sub

and call SetfilePath in your ... code.

If this were my problem to solve, I'd go figure out why you aren't allowed to change the name of the parameter in your subroutine, and get it changed. Clearly, you're allow to modify your subroutine; why is this particular change disallowed?

Ira Baxter
Dear Ira, thanks for your sub. I'll do it this way. In fact, I can propose changing the name of the parameter, and it may be accepted after discussion with senior coworkers, modifying the class design documents, and confirmation for this process. I just don't want the trouble, you know.
+1  A: 

So you're allowed to modify the Sub in such a way that would (if possible) allow you to set the Private variable to the local variable...but you're not allowed to rename either parameter? If you can't do Ira Baxter's suggestion either...then your task is impossible in VB6. The restrictions imposed on you sound unreasonable.

CodeByMoonlight