Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 29th, 2007, 04:48 PM
Friend of Wrox
 
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default access values of a class in other classes

Hi,
I am really confused!


I have different classes in my app_code folder
in one of my classes(below) i get the records of my config table
the table contains global necessary things like adminemail and application url
now how can i make it so that it can be accessible through other classes like this:

Dim config As New myNamspace.Config
exp:label1.Text = config.websitetitle

i am confused what to use function or sub or neither
please change the code
thankss

here is my config class

Namespace MyNamespace

    Public Class Config
        Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


            Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrin gs("Conn").ConnectionString)
            Dim Cmd As New SqlCommand()
            Dim dr As SqlDataReader
            With Cmd
                .Connection = Conn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "setting"
            End With
            Conn.Open()
            dr = Cmd.ExecuteReader()

            While dr.Read()

websiteTitle = dr("siteTitle").ToString
applicationURL = dr("appURL").ToString
adminEmail = dr("email").ToString
   End While

            dr.Close()
            Conn.Close()
        End Sub

  End Class

End Namespace



 
Old July 29th, 2007, 08:40 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Normally to access values on a class you use properties. For example:

    Public Property WebSiteTitle() As String
        Get
            Return _webSiteTitle
        End Get
        Set(ByVal value As String)
            _webSiteTitle = value
        End Set
    End Property

(_webSiteTitle is the internal variable in the class that contains the value you wish to get.)

Alternatively you could use a method to return the value.


Are you intending to use this Config class as a utility class? If so, you shouldn't derive it from System.Web.UI.Page because this class will never be instantiated as a web page. Thus the Page_Load method you have in it will never run. Instead, put all of that code into a class constructor so it gets run when you create the instance of it.

-Peter
 
Old July 30th, 2007, 01:45 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks
i did exactly what you say but i get this error:
Argument not specified for parameter 'WebsiteTitle' of 'Public Sub New(WebsiteTitle As String)'.
I have no idea how to use a function or sub

under Dim config As New myname.Config
config name i get that error
  Private _websiteTitle As String

        Public Sub New(ByVal WebsiteTitle As String)
            MyBase.New()
            _websiteTitle = WebsiteTitle

        End Sub

        Public Property WebsiteTitle() As String
            Get
                Return _websiteTitle
            End Get
            Set(ByVal Value As String)
                _websiteTitle = Value
            End Set
        End Property

 Public sub GetConfig()

Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrin gs("Conn").ConnectionString)
            Dim Cmd As New SqlCommand()
            Dim dr As SqlDataReader
            With Cmd
                .Connection = Conn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "cmmSP_setting"
            End With
            Conn.Open()
            dr = Cmd.ExecuteReader()

            While dr.Read()

               WebsiteTitle = dr("siteTitle").ToString

            End While

dr.Close()
            Conn.Close()
end sub

 
Old July 30th, 2007, 08:01 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You constructor is expecting a value for web site title. However, the whole point of this class is to get it from the database. Remove the argument from the constructor and move all the code you have in the Config() method into the constructor as I previously suggested. Then the database call will be made when you create the instance of this class without requiring a call to another method to load the data.

-Peter
 
Old July 31st, 2007, 11:27 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot planie it worked






Similar Threads
Thread Thread Starter Forum Replies Last Post
Property access from Class within Partial Class zoltac007 C# 2005 0 December 1st, 2006 01:01 AM
access app_code classes pooh2006 ASP.NET 2.0 Professional 1 April 5th, 2006 09:21 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
Get session values from a classes minhpx General .NET 3 August 9th, 2004 10:05 AM
classes :: base class is inaccessible .... Kaliste C# 3 July 16th, 2004 04:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.