Solved

VB.NET scope of property values

Posted on 2007-03-27
11
898 Views
Last Modified: 2008-01-09
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
Comment
Question by:rss2
  • 6
  • 4
11 Comments
 
LVL 7

Expert Comment

by:dctuck
Comment Utility
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
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
 

Author Comment

by:rss2
Comment Utility
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
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
 

Author Comment

by:rss2
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rss2
Comment Utility
Sorry that "Cann" should read "Cannot"
0
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
   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
 

Author Comment

by:rss2
Comment Utility
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
 

Author Comment

by:rss2
Comment Utility
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
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
Comment Utility
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
 

Author Comment

by:rss2
Comment Utility
Awesome. You truly are a GENIUS.

thank you! :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now