Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB.NET scope of property values

Posted on 2007-03-27
11
902 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
ID: 18800483
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
ID: 18800521
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
ID: 18800855
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 43

Expert Comment

by:TimCottee
ID: 18806371
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
ID: 18814374
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
 

Author Comment

by:rss2
ID: 18814408
Sorry that "Cann" should read "Cannot"
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 18814439
   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
ID: 18814480
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
ID: 18814508
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
ID: 18814510
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
ID: 18814541
Awesome. You truly are a GENIUS.

thank you! :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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