Re: Variable help

Posted 16 May 2008 - 11:54 AM

Hi, Ive created a login page, the page then takes u in to the application which is like a calculator, i want to save the calculation made, answer, username and date in a database. Ive got the calc and the answer saved but i'm struggling on the other to.

With the username ive declared the username in the login form, is there anyway i can make the varable availabe for the whole app so i can refer to it on the calc page??

And for the date i dont really know where to start, ive had a good hunt but nothing, any help waould be awsome

Re: Variable help

Posted 16 May 2008 - 01:30 PM

the easiest way to do that is on the login form declare a variable that is Public Shared and use that variable to hold the username. its not the ideal way but its very quick and easy. so the variable would look like

Public Shared sUsername as String
'on login set the variable
sUsername = txtUserName.text

the date, you can just use the Today() function in vb to pass todays date to database. or if ur using stored procedures to write everything to the database, use you would a sql function called GetDate()

Re: Variable help

Posted 16 May 2008 - 01:58 PM

bflosabre91, on 16 May, 2008 - 01:30 PM, said:

the easiest way to do that is on the login form declare a variable that is Public Shared and use that variable to hold the username. its not the ideal way but its very quick and easy. so the variable would look like

Public Shared sUsername as String
'on login set the variable
sUsername = txtUserName.text

the date, you can just use the Today() function in vb to pass todays date to database. or if ur using stored procedures to write everything to the database, use you would a sql function called GetDate()

Hi thanks for the response, but i've done that yet on the other form it still says username is undefined??

Re: Variable help

Well if you are instantiating the 'login' form from your 'home' form, then create a Property on the 'login'. From within the 'login' form, assign the username to this property on successful login.

Back on your 'home' form, after you instantiate the 'login' form, then get the username from the Property that you created.

Now you can use it on your 'home' form.

Example:

Public Class Login
Private _userName As String
Public Property UserName() As String
Get
Return _userName
End Get
Set (ByVal value As String)
_userName = value
End Set
End Property
'the rest of your code that does validation and such
'after user has been validated then assign the username to the property
UserName = txtUserName.Text
End Class

Public Class Home
Private _userName As String
' the following code should be inside your Load event
Dim login As New Login()
login.ShowDialog()
'now get the username and store it in a global variable
'so that it is accessable whenever you need it.
_userName = login.UserName
''the rest of your code
End Class

This is just to give you an idea of how to do it. Give it a try and let us know how it goes.