VB.NET scope of property values

I am using properties in vb.net for an asp.net web site.

On the firstpage.aspx, I'm setting the username variable. When the submit button is clicked, I'm getting the value and passing it into a stored procedure.

I seem to lose that value that I just set, and I suspect it has to do with where I'm declaring a reference to the class where the properties are set incorrectly.


Here's my code: In LoadData() the value strGroup_Name is Nothing.

Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Data.SqlClient
Imports System.DirectoryServices

Partial Class Homepage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sv As New SetVars
        If Not IsPostBack Then
            sv.strGroup_Name = "UK-GLBL-xxxxxxx" ' Will get from Active Directory
            sv.strUser_Name = "MEADWAYP" 'Request.ServerVariables("AUTH_USER").ToString
            GetPermissions(sv.strGroup_Name)
            LoadData()
        End If
    End Sub

    Protected Sub LoadData()
        Dim conn As New SqlConnection
        Dim cmd As New SqlCommand
        Dim connstring As String
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        Dim sv As New SetVars

        connstring = Connector.ConnectStringBuild
        conn.ConnectionString = connstring
        cmd.Connection = conn
        cmd.CommandText = "ACT_GET_HOMEPAGE_DETAILS"
        cmd.CommandType = CommandType.StoredProcedure


        cmd.Parameters.Add("@GROUP_NAME", SqlDbType.VarChar).Value = sv.strGroup_Name

        conn.Open()
        da.Fill(dt)
        Me.Repeater1.DataSource = dt
        Me.Repeater1.DataBind()
        'cmd.ExecuteNonQuery()
        conn.Close()

        conn = Nothing
        cmd = Nothing
    End Sub

Why isn't it set?

Thanks,
rss2

rss2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dctuckCommented:
On every Page_Load, you're setting sv to be a brand new SetVars class. Perhaps moving this inside the Not IsPostback code would work?
0
TimCotteeHead of Software ServicesCommented:
It needs to be private to the class, not to the page_load event handler:

Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Data.SqlClient
Imports System.DirectoryServices

Partial Class Homepage
    Inherits System.Web.UI.Page

#Region "Declarations"
  Private sv As SetVars 'Declare the object here with scope for the whole class
# End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        sv = New SetVars 'Instantiate the object here
        If Not IsPostBack Then
            sv.strGroup_Name = "UK-GLBL-xxxxxxx" ' Will get from Active Directory
            sv.strUser_Name = "MEADWAYP" 'Request.ServerVariables("AUTH_USER").ToString
            GetPermissions(sv.strGroup_Name)
            LoadData()
        End If
    End Sub

And so on.
0
rss2Author Commented:
Ok, thank you.

And what about in the other aspx.vb modules?

How so I instantiate the SetVars class without overwriting the previously set values?

Do I:
Partial Class Collections
Inhereits System.Web.UI.Page
Private sv as New SetVars
?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

TimCotteeHead of Software ServicesCommented:
Does setVars class then need to be a public shared class amongst your application? If so then you would be better off perhaps creating it as a module and having Shared Property access to its properties and methods. This way you can have a single instance of the class but all your pages can access the same instance.
0
rss2Author Commented:
Yes, that is what I intend to do...to have the one class and reference it from all other classes in the application. Must it have "shared properties" to do that?

How do I reference the module SetVars throughout the application?
Dim sv as SetVars?
Dim sv as New Setvars?

Also when I make the properties within SetVars shared, I get blue squiggly lines everywhere.. The error message says: "Cann refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."


Here is the SetVars code below:

Imports Microsoft.VisualBasic
Imports System

Public Class SetVars
    Public strIndicator As String
    Public iService_ID As Integer
    Public strUser_Name As String
    Public strGroup_Name As String
    Public strUser_Greeting As String
    Public Shared Property UserGreeting() As String
        Get
            UserGreeting = strUser_Greeting
        End Get
        Set(ByVal value As String)
            strUser_Greeting = value
        End Set
    End Property

    Public Shared Property Indicator() As String
        Get
            Indicator = strIndicator
        End Get
        Set(ByVal value As String)
            strIndicator = value
        End Set
    End Property
    Public Shared Property Service_ID() As String
        Get
            Service_ID = iService_ID
        End Get
        Set(ByVal value As String)
            iService_ID = value
        End Set
    End Property
    Public Shared Property UserName() As String
        Get
            UserName = strUser_Name
        End Get
        Set(ByVal value As String)
            strUser_Name = value
        End Set
    End Property
    Public Shared Property GroupName() As String
        Get
            GroupName = strGroup_Name
        End Get
        Set(ByVal value As String)
            strGroup_Name = value
        End Set
    End Property
End Class
0
rss2Author Commented:
Sorry that "Cann" should read "Cannot"
0
TimCotteeHead of Software ServicesCommented:
   Private Shared strIndicator As String
    Private Shared iService_ID As Integer
    Private Shared strUser_Name As String
    Private Shared strGroup_Name As String
    Private Shared strUser_Greeting As String

These need to be declared as shared variables also.

They don't need to be public because you will only be accessing them through your properties so the actual value can be private to the class but must be declared as shared still so that the shared properties can access the variables.
0
rss2Author Commented:
Great. Thank you.

Within the aspx.vb classes, how to I instantiate the SetVars class to get to its properties?

For example, in my firstpage.aspx.vb I get blue squiggly lines underneath "sv.strUser_Greeting":
Imports System
Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Data.SqlClient
Imports System.DirectoryServices

Partial Class Collections
    Inherits System.Web.UI.Page
    Dim sv As SetVars
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Display the correct controls according to the group's permissions

        Me.lblUserGreeting.Text = sv.strUser_Greeting
        LoadData()
    End Sub

?
0
rss2Author Commented:
Sorry, I'm an idiot.

I changed the line to "sv.UserGreeting" and that removed the blue squiggly line. But now I get a green one. (think I'll just go through all the colours of the spectrum while I'm at it..)

Anyway, the error is "Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated."

??
0
TimCotteeHead of Software ServicesCommented:
You don't need to any more:

Imports System
Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Data.SqlClient
Imports System.DirectoryServices

Partial Class Collections
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Display the correct controls according to the group's permissions

        Me.lblUserGreeting.Text = setVars.strUser_Greeting
        LoadData()
    End Sub

You can use it as a global reference, you may need to include the namespace if you have one e.g., MyNamespace.setVars.UserGreeting()

As you have the properties you should use the property access as the underlying variables are now private so are not accessible from outside the shared class. This isolates the underlying variable so that only the properties are accessible. If you needed to format the result for example you could do it in the property.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rss2Author Commented:
Awesome. You truly are a GENIUS.

thank you! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.