Visual Basic programming question

Status
Not open for further replies.

sohummisra

Broken In
I'm using VB6 to make this program and I have this minor problem that is very difficult to search for on the net (because the solution is so simple--I just don't know it or it is eluding me). Basically I have a public procedure which contains a parameter. Let's just call it:

Code:
Public Sub X(Byval username As String)
     blah blah
End Sub

Now I want to use the same parameter in a user-generated click event on a command-button. I was wondering if there is anyway to refer to that variable (in the the Sub X) from within the other procedure and how to do this. I was trying things like X.username but it didn't work. :S[/code]
 

tuxfan

Technomancer
sohummisra said:
Now I want to use the same parameter in a user-generated click event on a command-button. I was wondering if there is anyway to refer to that variable (in the the Sub X) from within the other procedure and how to do this. I was trying things like X.username but it didn't work. :S

Could you just clarify a little more. What exactly do you want to do?

x.username would surely not work because s is not an object, its just a procedure (sub-routine). What you can do is have a global variable having the same value as username and access that on the click event.
 

daj123

Journeyman
lol, the answer is quite simple. You either set a global variable when that 'sub X' is called.

Code:
Option Explicit
Public myusername as STRING

Public Sub X(Byval username As String)
     myusername = username
End Sub 

Public Sub AnotherX
     command1.text = myusername
End Sub

Hope you get the idea ;)
 
OP
S

sohummisra

Broken In
I know that the problem can be solved using a global variable but I was wondering if there was any way to program it without 'wasting' space. That is, programming it efficiently yet confusingly. A global variable would solve all problems because then variables would not have to be passed.
 

tuxfan

Technomancer
Actually, there could be a better way then making a global variable. Just tell us exactly what you are trying to do and what is the purpose of accessing that variable. We need more details.
 
OP
S

sohummisra

Broken In
Okay basically I am making a sort of organizer program for my IB Computer Science class. Since the user requires to login with the username and password, and the username is used to access all of the user's files, it is a variable that is required by more or less all the procedures and functions. This was no problem in pseudocode because user-generated procedures were not differentiated.

Now, I have made an Add Activity form which opens and shows. Unfortunately, this requires me to use a Command_Click() function which does not have the username parameter because the latter is not a global variable. But the AddActivity procedure in the add activity form needs the username parameter. Any more questions? Please do ask.
 

tuxfan

Technomancer
I think there is some fault in your designing. In Add if you need a username, create a variable.

If the same variable is earlier entered somewhere, it must be stored in a database table. Retrieve it from there!! If it is not there in the database, you will want your user to enter it. So get it in a textbox and take it from there!! What's the problem?

Frankly speaking, I am really not too sure what you are trying to do. Your design or logic seems to have some problem.
 
OP
S

sohummisra

Broken In
I figured I don't really need it because I just needed it to open the files. There was no design logic problem--there was just the problem of transferring the username from a computer-process to a user-generated event.
 

tuxfan

Technomancer
Good to know that there was no designing problem :) That could've increased a lot of problems for you. I felt there was a problem from your questions. :D
 
Status
Not open for further replies.
Top Bottom