I'm new to classes, why isn't this working?

D

David Lozzi

Hey,

Trying to build a simple class to pull data from the web.config file. Here's
what I have

in common.vb
Imports System.Configuration

Public Class CommonFuncts
Public Property ConnectionString() As String
Get
Return ConfigurationSettings.AppSettings("ConnectionString")
End Get
Set(ByVal Value As String)
End Set
End Property
End Class

In my page header.ascx.vb
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim conn As String = ConnectionString
lblTest.Text = conn

End Sub

I am getting a blue underling on ConnectionString in header.ascx.vb. The
message is "Reference to a non-shared member requires an object reference."

How do I fix this?

Thanks a million!

David Lozzi
 
S

Steve C. Orr [MVP, MCSD]

Try this:
Dim conn As String = new CommonFuncts.ConnectionString

That specifies the class name and instantiates it.
You could make this into a shared method so you wouldn't have to instantiate
the class.
Or you could put it into a code module so you wouldn't have to refer to any
class at all, but that's not very object oriented.
 
J

Joel Zinn

Use the Shared keyword as in Public Shared property or Public Shared
function, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,354
Latest member
OrenKrause

Latest Threads

Top