Link to home
Start Free TrialLog in
Avatar of rss2
rss2

asked on

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

Avatar of dctuck
dctuck
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
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.
Avatar of rss2
rss2

ASKER

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
?
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.
Avatar of rss2

ASKER

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
Avatar of rss2

ASKER

Sorry that "Cann" should read "Cannot"
   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.
Avatar of rss2

ASKER

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

?
Avatar of rss2

ASKER

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."

??
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rss2

ASKER

Awesome. You truly are a GENIUS.

thank you! :)