[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

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

0
rss2
Asked:
rss2
  • 6
  • 4
1 Solution
 
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
 
TimCotteeCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimCotteeCommented:
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
 
TimCotteeCommented:
   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
 
TimCotteeCommented:
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
 
rss2Author Commented:
Awesome. You truly are a GENIUS.

thank you! :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now