Reference to a non-shared member requires an object reference

lgdt

New member
Joined
Jun 27, 2005
Messages
2
Programming Experience
10+
Hi

I am new to VB - and this seems to work in C# - so can someone help me please because thisa is driving me mad.

I have created a function as a namespace within a VB class file in my .NET project (Content) as follows:


VB.NET:
Imports System
Imports System.Data.SqlClient
Namespace Connections
Public Class SQLCons
 
Public Function LogonManager(ByVal ProcName As String, ByVal DSTabName As String, ByVal man_uname As String, ByVal man_password As String) As DataSet
 
Dim con As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim SQLcom As SqlCommand = New SqlCommand(ProcName, con)
SQLcom.CommandType = CommandType.StoredProcedure
 
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQLcom)
 
SQLcom.Parameters.Add("@man_uname", SqlDbType.NVarChar, 50, "man_uname").Value = man_uname
SQLcom.Parameters.Add("@man_password", SqlDbType.NVarChar, 50, "man_password").Value = man_password
 
SQLcom.Connection.Open()
SQLcom.ExecuteNonQuery()
SQLcom.Connection.Close()
 
Dim DS As DataSet = New DataSet
DA.Fill(DS, DSTabName)
 
Return DS
 
End Function
 
 
 
End Class
End Namespace


I use the import statement in the main calling class:

VB.NET:
Imports System.Data.SqlClient
Imports System
Imports Content.Connections

However when I try to reference Class SQLCons in this class using these statements:

VB.NET:
Dim DS As DataSet = New DataSet
DS = SQLCons.LogonManager("LogonManagers", "manager", txbxUName.Text.ToString(), txbxPword.Text.ToString())
I get the following compilation error:

Reference to a non-shared member requires an object reference (this refers to the reference to SQLCons.LogonManager)

Can anyone tell me what I'm doing wrong? As I say - this works OK in C# which is where I have lifted it from and tried to convert.

Thanks in advance.
 
Last edited by a moderator:
try this

VB.NET:
[size=2][color=#0000ff]Imports[/color][/size][size=2] System.Data

[/size][size=2][color=#0000ff]Imports[/color][/size][size=2] System.Data.SqlClient

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Class[/color][/size][size=2] SQLCons

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Shared[/color][/size][size=2] [/size][size=2][color=#0000ff]Function[/color][/size][size=2] LogonManager([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] ProcName [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] DSTabName [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] man_uname [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] man_password [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataSet

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] con [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlConnection = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlConnection(ConfigurationSettings.AppSettings([/size][size=2][color=#800000]"ConnectionString"[/color][/size][size=2]))

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] SQLcom [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlCommand = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlCommand(ProcName, con)

SQLcom.CommandType = CommandType.StoredProcedure

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] DA [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlDataAdapter = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlDataAdapter(SQLcom)

SQLcom.Parameters.Add([/size][size=2][color=#800000]"@man_uname"[/color][/size][size=2], SqlDbType.NVarChar, 50, [/size][size=2][color=#800000]"man_uname"[/color][/size][size=2]).Value = man_uname

SQLcom.Parameters.Add([/size][size=2][color=#800000]"@man_password"[/color][/size][size=2], SqlDbType.NVarChar, 50, [/size][size=2][color=#800000]"man_password"[/color][/size][size=2]).Value = man_password

SQLcom.Connection.Open()

SQLcom.ExecuteNonQuery()

SQLcom.Connection.Close()

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] DS [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataSet = [/size][size=2][color=#0000ff]New[/color][/size][size=2] DataSet

DA.Fill(DS, DSTabName)

[/size][size=2][color=#0000ff]Return[/color][/size][size=2] DS

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Function

End[/color][/size][size=2] [/size][size=2][color=#0000ff]Class

[/color][/size]

And then

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] ds [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataSet = [/size][size=2][color=#0000ff]New[/color][/size][size=2] DataSet

ds = LogonManager([/size][size=2][color=#800000]""[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2], [/size][size=2][color=#800000]""[/color][/size][size=2])

[/size]
 
That code could not possibly work in C#. You have declared the LogonManager function as Public, so you must call it on an instance of the SQLCons class, e.g.
VB.NET:
'Visual Basic
Dim mySQLCons As New SQLCons

Dim myDataSet As DataSet = mySQLCons.LogonManager(...)

//C#
SQLCons mySQLCons = new SQLCons;

DataSet myDataSet = mySQLCons.LogonManager(...);
not using just by using the class name. To be able to call a function using just the name of a class, it must be declared Shared in VB.NET or static in C#.

Also, what conventions you use is up to you, but LogonManager would be a more appropriate name for a property because it is a noun. Function names a normally verb oriented, like GetSomething or DoSomething. LogonManager sounds more like a thing than an action.
 
Back
Top