Forum  General Visual ...  Let's Chat Abou...  Global Variable question/Clarification
Previous Previous
 
Next Next
New Post 5/13/2009 10:37 PM
  rdhatch
635 posts
1st Level Poster




Re: Global Variable question/Clarification 
Modified By rdhatch  on 5/14/2009 12:55:43 AM)

Hi Derek,

I try to keep all my user-specific stuff in my Main Form (ie, Form1... or whatever).  One MainForm is created per user session.  Even if the user opens up multiple browser tabs to the application - only one Main Form instance will be created for them, which is good since that user's session data will be shared across multiple browser tabs.

I try to stay away from storing data in the Session/VWGContext if I can help it.  I prefer to work with strongly-typed objects instead of strings... I like compile-time checking.

So any variables/objects you store in your Main Form will only be accessible from within that user session.  Be sure to declare your variables in your Main Form as Public/Friend/Private... but NOT as Shared.  A Shared variable in the Main Form would give all users access to the same piece of information, and they could overwrite eachother's data, too.  So be sure to not use Shared variables in the Main Form.

Here's a good example for you.  Create a simple class as a container & expose your variables from it (like Palli's class... excellent example, Palli!).  Something like this:

Public Class SessionVariables

Public Username As String

Public IsAdmin as Boolean

End Class

Then in your MainForm, simply put this at the top (outside of any methods):

Public mySessionVariables As New SessionVariables

Now, I'd create a Shared class as a helper class, which I always do in my projects.  For Instance:

Public Class Util

Public Shared Function GetSessionVariables(ByVal myControl as Control) As SessionVariables

'Gets the MainForm belonging to the current user

Dim myForm As MainForm = CType(myControl.Context.MainForm, MainForm)

Return myForm.mySessionVariables

End Function

Private Sub New     'Prevent instantiation

End Sub

End Class

Now, from anywhere in your application - You can instantly access that user's SessionVariable object.  Simply import/include Util and do this:

GetSessionVariables(Me).Username

GetSessionVariables(Me).IsAdmin

By passing Me, which is the current control you're in - you automatically pass the user Context as well.  Hope this helps!

Ryan

PS. Database connection strings belong in your web.config, because they are application-wide, not user-specific.  Use the ConfigurationManager to read from your web.config.


Ryan D. Hatch, VWG MVP
GeniusCode.net | VWG Community Frameworks | VWG Wiki
 
New Post 5/14/2009 5:54 PM
  derekmhart@yahoo.com
685 posts
1st Level Poster


Re: Global Variable question/Clarification 

Can you please tell me if all the code should be in the main form, or if any should be in a separate file. I understand the GetSessionVariables can be used anywhere.

 
New Post 5/14/2009 6:18 PM
  derekmhart@yahoo.com
685 posts
1st Level Poster


Re: Global Variable question/Clarification 

Actually, I am unclear how I should be doing a get/set on the variables, if I do this the same way as in winforms - can you be more specific as to exactly how to set these variables (preferably using one line, or is that a problem because they are not shared variables).

 
New Post 5/14/2009 6:31 PM
  palli
11189 posts
1st Level Poster




Re: Global Variable question/Clarification 

Hi Derek,

Referencing Ryan's post before in this thread, which is an excellent description of what you need (Thanks Ryan), the only thing that needs to be in your MainForm is: Public mySessionVariables As New SessionVariables

The rest of the class definitions can be in seperate file.

What is missing from Ryans description is how you get information about the logged on user from your logon form to your mainform. I usually do that with Session/VWGContext variables in the same spot as I assign Context.Session.IsloggedOn = True.

In the load event of my mainform, I check if those variables are set and in there I fill the mySessionVariables class instance on the mainform.

Palli

 

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
New Post 5/14/2009 9:13 PM
  derekmhart@yahoo.com
685 posts
1st Level Poster


Re: Global Variable question/Clarification 
Modified By derekmhart@yahoo.com  on 5/14/2009 11:21:36 PM)

When I use this code I cannot call GetSessionVariables from another class or a module. How can I use this function in a way where I do not have to pass a control, but I can still pass something that represents ME. And I was trying to avoid using the context variables but simply a class in the main form. But I am not sure how to get/set mySessionVariables from elsewhere. Do I have to use VWGContext.Current("UserName") or can I just use the class with strongly typed variables?

Public Shared Function GetSessionVariables(ByVal myControl As Control) As SessionVariables

Dim myForm As Main = CType(myControl.Context.MainForm, Main)Return myForm.mySessionVariables

End Function

 

 

 

 

 

 
Previous Previous
 
Next Next
  Forum  General Visual ...  Let's Chat Abou...  Global Variable question/Clarification
CompanionKit Bottom
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS