Component problem, pls help!

C

CM

Hi there:
I created an ASP.Net project. I added and implemented a component
DataAcess.vb and there are several methods such as

Namespace ABC.Components
Public Class DBAccess
Public Function MakeInParam(...) As SqlParameter

...

End Function

Public Sub Open()


...

End Sub

End Class

End Namespace

In another component (Login.vb) in the same namespace, I have included the
namespace ABC.Components, and the "New" key word is followed with a dropdown
list that contains the class name 'DBAccess', But non of these methods such
as DBAccess.MakeInParam is shown in the other component such as Login.vb

Dim data As New DBAccess 'this line is okay

'this line causes a syntax error: type DBAccess.makeinparam is not defined:

Dim data As New DBAccess.makeinparam



What's wrong? How to fix this problem?

Thanks!

CM
 
A

Armin Zingler

CM said:
Hi there:
I created an ASP.Net project. I added and implemented a component
DataAcess.vb and there are several methods such as

Namespace ABC.Components
Public Class DBAccess
Public Function MakeInParam(...) As SqlParameter

...

End Function

Public Sub Open()


...

End Sub

End Class

End Namespace

In another component (Login.vb) in the same namespace, I have
included the namespace ABC.Components, and the "New" key word is
followed with a dropdown list that contains the class name
'DBAccess', But non of these methods such as DBAccess.MakeInParam is
shown in the other component such as Login.vb

Dim data As New DBAccess 'this line is okay

'this line causes a syntax error: type DBAccess.makeinparam is not
defined:

Dim data As New DBAccess.makeinparam



What's wrong? How to fix this problem?


The new keyword creates a new object. After "New", you write the type name.
DBAccess.makeinparam is not a type, it is a member of a type. You can create
instances of the DBAccess class and use the members of the created object.
See the OOP (object oriented programming) sections in the VB.NET docs.
 
T

Tom Spink

Hi CM,

What are you trying to do?

MakeInParam is a method, not a class, so you cannot create an instance of
it... Are you trying to call that method?

If so, all you need is:

data.MakeInParam(...)

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi CM,

Dim data As New DBAccess 'this line is okay

This line <is> ok because 'As' must be followed by a Type and DBAccess
<is> a Type.

Dim data As New DBAccess.makeinparam

But this line will cause a problem because DBAccess.makeinparam is <not> a
Type - it's a method of an instance of a Type.

However,
Dim data As New DBAccess
Dim SquealyPram As SqlParameter
SquealyPram = data.makeinparam (...)
will work, because data is an object, and makeinparam is an object
method.and they like each other just fine.

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

CM said:
I created an ASP.Net project. I added and implemented a component
DataAcess.vb and there are several methods such as

Namespace ABC.Components
Public Class DBAccess
Public Function MakeInParam(...) As SqlParameter

...

End Function

Public Sub Open()


...

End Sub

End Class

End Namespace

In another component (Login.vb) in the same namespace, I have included the
namespace ABC.Components, and the "New" key word is followed with a dropdown
list that contains the class name 'DBAccess', But non of these methods such
as DBAccess.MakeInParam is shown in the other component such as Login.vb

Dim data As New DBAccess 'this line is okay

'this line causes a syntax error: type DBAccess.makeinparam is not defined:

Dim data As New DBAccess.makeinparam

'MakeInParam' is a method, not a type. You can _call_ the method, but
you cannot instantiate it.

\\\
Dim c As New DBAccess()
Dim p As ... = c.MakeInParam(...)
///
 
C

CM

Thanks for reply.
I think you are all right but this line still not working:

Dim p As ... = c.MakeInParam(...)

Even I type
DBAccess. 'I expect the "open" method will popup in the list, but no.

Nothing follows the dot.

CM
 
H

Herfried K. Wagner [MVP]

CM said:
I think you are all right but this line still not working:

Dim p As ... = c.MakeInParam(...)

Even I type
DBAccess. 'I expect the "open" method will popup in the list, but no.

Nothing follows the dot.

Please post more code. Which error/exception do you get?
 
C

CM

Thanks for all everyone's help!

I've checked some error message:

Dim Data1 as new DBAccess 'this line is fine.

This line causes an error( I'll handle it later) : [the value of
'data.MakeInParam("@userID", SqlDbType.Char, 80, userID)' cannot be
converted to one demensional array of
ystem.data.sqlClient.SqlParameter() ]:
SqlParameter() = data.MakeInParam("@userID", SqlDbType.Char, 80, userID)

But for this line (begin at new line) there is nothing following the dot:

DBAccess. 'nothing following the dot

If I put this line:
DBAccess.MakeInParam() 'An error: Reference to a non-shard member requires
an object references.

It is seemed the problem is "non-shard", but sorry, I don't know what this
mean.
Please continue your help!

Thanks!

CM
 
F

Fergus Cooney

Hi CM,

SqlParameter() = data.MakeInParam (....)

It's confusing because () can be used with a method which takes no
arguments and with variables that are arrays. In this case the compiler thinks
that SqlParameter() is an array. As MakeInParam only returns a single
SqlParameter you don't need (). But you do need to have defined a variable.

Dim SqlParam As SqlParameter
SqlParam = data.MakeInParam (....)

Have another read of the replies that we've given you. I've already shown
you something very similar to this example.

===========================

DBAccess.

What are you expecting to appear ?? What do you want to do with DBAccess
at this point?

Regards,
Fergus
 
C

CM

Oh, Ya! Fergus, Sorry for my ignorance, It works, no syntax error:
Dim SqlParam As SqlParameter
SqlParam = data.MakeInParam (....)
Thank you!

But these remain unsolved:

Data. 'Nothing following the dot

Data.Open() 'Err: Reference to a non-shard member requires an object
references.

What I need is in the login component open database for login
authentication.
These code originally in C#, I used a C# to VB translator. I copied the
translated code (in text only editor) and pasted to the VB components.
Strange thing to me is the 'New' key word can follow a list that contain
DBAccess, but DBAccess' members are not shown after the dot 'DBAccess.' The
error message saying reference to a non-shard member, but the member is
public, what to do to share the member?

CM
 
T

Tom Spink

Hi CM, you need an instance of DBAccess to access the methods:

Dim X As New DBAccess

X.Open

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi CM,

In your first post you had:
Dim data As New DBAccess 'this line is okay

This will create an object, data, of type (class) DBAccess.

You're now getting an error
Data.Open()
'Err: Reference to a non-shard member
requires an object references.

This error occurs when you try to use a class to access methods which
belong to objects of the class.

Consider:
Class FooType
Public Shared Sub ClassAndObjMethod
End Sub

Public Sub ObjOnlyMethod
End Sub
End Class

Using the class to call a class method - This will work fine.
FooType.ClassAndObjMethod

Using the class to call an object method - This will fail.
FooType.ObjOnlyMethod
It will fail with the object-required message that you got - because
ObjOnlyMethod <hasn't> got Shared and therefore <must have> an object.

Using an object to call an object method - This will work
Dim oFoo As FooType
oFoo.ObjOnlyMethod

I suspect that somehow your Data has been declared as a Class rather than
as an object.

========================
DBAccess is a Class. What you get when you type the '.' after a class and
an object is different.

Let's look at the example above again.

If I type FooType and hit the '.', Intellisense will show me
ClassAndObjMethod. It's looked at all the methods defined within FooType and
picked out the ones that apply <only> to the class. These are the public ones
with the word Shared (and any constants, nested classes, etc)

If I type oFoo and the '.', Intellisense will show me ClassAndObjMethod
and ObjOnlyMethod. It's looked at all the methods defined within FooType and
picked out the public ones that apply <both> to the class <and> the object.

If I had no Shared methods or members in FooType, Intellisense would show
me nothing. I suspect that within DBAccess there is no sign of the word
Shared.

========================

You may like to post your C# and your VB so that we can give it a look (if
it's not reams!!).

Regards,
Fergus
..
 
A

Armin Zingler

CM said:
Oh, Ya! Fergus, Sorry for my ignorance, It works, no syntax
error: Dim SqlParam As SqlParameter
SqlParam = data.MakeInParam (....)
Thank you!

But these remain unsolved:

Data. 'Nothing following the dot

Data.Open() 'Err: Reference to a non-shard member requires an
object references.


<F1>
Visual Studio.NET
Visual Basic and Visual C#
Reference
Visual Basic language
Visual Basic Language Tour
Object oriented programming in Visual Basic
 
T

Tom Spink

Hi Armin,

I'm surprised you haven't broken your F1 key ;-) Do you keep spares?

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
C

CM

Hi, Fergus,
Thank you so much! I am really appreciated for your great help.

Yes, you are right, there is no "shared" within DBAccess, and I put "shared"
before the method, it is shown by intellisense.

However, because the original code are long, I tried to simplify them for
your read and do the translation and paste again, this time, the
intellisense shows all the members without "shared"!

Let me put it clear: I put the simplified code to DBAccess1.vb and Agent1.vb
respectively within the same project same folder.

In Agent1:

Dim data As New DBAccess1
data. 'it shows all members of DBAccess1 after the dot.

Strange thing is now in Agent :
Dim data As New DBAccess
data. 'it show all members of DBAccess after the dot no matter of shared
or not.

The strange thing is not happened immediately after paste the DBAccess1.vb
and Agent1.vb, but just after re-typed "Dim data As New DBAccess"

Following are the simplified code: Originals are DBAccess.cs and Agent.cs
and translated to DBAccess.vb and Agent.vb by
http://authors.aspalliance.com/aldotnet/examples/translate.aspx
The only syntax error in DBAccess.vb is the "Implenments IDisposable" which
ask me must implenment overridable Sub Dispose(), But I do have "Publid
Overridable Sub Dispose()", I don't know why this error message.

Again, Thank you so much!
CM
DBAccess.CS

using System;

using System.ComponentModel;

using System.Collections;

using System.Diagnostics;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;



namespace ETMarket.Components {

public class DBAccess : IDisposable

{

// connection to data source

private SqlConnection con;



public int RunProc(string procName, SqlParameter[] prams)

{

SqlCommand cmd = CreateCommand(procName, prams);

cmd.ExecuteNonQuery();

this.Close();

return (int)cmd.Parameters["ReturnValue"].Value;

}



private SqlCommand CreateCommand(string procName,
SqlParameter[] prams)

{



Open();



SqlCommand cmd = new SqlCommand(procName, con);

cmd.CommandType = CommandType.StoredProcedure;



if (prams != null)

{

foreach (SqlParameter parameter in prams)

cmd.Parameters.Add(parameter);

}



// return param

cmd.Parameters.Add(

new SqlParameter("ReturnValue", SqlDbType.Int,
4,

ParameterDirection.ReturnValue, false, 0, 0,

string.Empty, DataRowVersion.Default, null));



return cmd;

}



public void Open()

{

if (con == null)

{

con = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

con.Open();

}

}

public void Dispose()

{

// make sure connection is closed

if (con != null)

{

con.Dispose();

con = null;

}

}

public void Close()

{

if (con != null)

con.Close();

}

public SqlParameter MakeInParam(string ParamName, SqlDbType
DbType, int Size, object Value)

{

return MakeParam(ParamName, DbType, Size,
ParameterDirection.Input, Value);

}



public SqlParameter MakeParam(string ParamName, SqlDbType
DbType, Int32 Size, ParameterDirection Direction, object Value)

{

SqlParameter param;



if(Size > 0)

param = new SqlParameter(ParamName, DbType,
Size);

else

param = new SqlParameter(ParamName, DbType);



param.Direction = Direction;

if (!(Direction == ParameterDirection.Output && Value
== null))

param.Value = Value;



return param;

}

}

}



DBAccess.vb
Imports System

Imports System.ComponentModel

Imports System.Collections

Imports System.Diagnostics

Imports System.Data

Imports System.Data.SqlClient

Imports System.Configuration





Namespace ETMarket.Components

_

Public Class DBAccess

Implements IDisposable 'ToDo: Add Implements Clauses for
implementation methods of these interface(s)

' connection to data source

Private con As SqlConnection





Public Function RunProc(procName As String, prams() As SqlParameter)
As Integer

Dim cmd As SqlCommand = CreateCommand(procName, prams)

cmd.ExecuteNonQuery()

Me.Close()

Return CInt(cmd.Parameters("ReturnValue").Value)

End Function 'RunProc





Private Function CreateCommand(procName As String, prams() As
SqlParameter) As SqlCommand



Open()



Dim cmd As New SqlCommand(procName, con)

cmd.CommandType = CommandType.StoredProcedure



If Not (prams Is Nothing) Then

Dim parameter As SqlParameter

For Each parameter In prams

cmd.Parameters.Add(parameter)

Next parameter

End If

' return param

cmd.Parameters.Add(New SqlParameter("ReturnValue", SqlDbType.Int,
4, ParameterDirection.ReturnValue, False, 0, 0, String.Empty,
DataRowVersion.Default, Nothing))



Return cmd

End Function 'CreateCommand





Public Sub Open()

If con Is Nothing Then

con = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

con.Open()

End If

End Sub 'Open



Public Sub Dispose()

' make sure connection is closed

If Not (con Is Nothing) Then

con.Dispose()

con = Nothing

End If

End Sub 'Dispose



Public Sub Close()

If Not (con Is Nothing) Then

con.Close()

End If

End Sub 'Close



Public Function MakeInParam(ParamName As String, DbType As SqlDbType,
Size As Integer, Value As Object) As SqlParameter

Return MakeParam(ParamName, DbType, Size, ParameterDirection.Input,
Value)

End Function 'MakeInParam





Public Function MakeParam(ParamName As String, DbType As SqlDbType,
Size As Int32, Direction As ParameterDirection, Value As Object) As
SqlParameter

Dim param As SqlParameter



If Size > 0 Then

param = New SqlParameter(ParamName, DbType, Size)

Else

param = New SqlParameter(ParamName, DbType)

End If

param.Direction = Direction

If Not(Direction = ParameterDirection.Output And Value Is Nothing)
Then

param.Value = Value

End If

Return param

End Function 'MakeParam

End Class 'DBAccess

End Namespace 'ETMarket.Components

agent.cs
using System;

using System.Data;

using System.Data.SqlClient;



namespace ETMarket.Components

{



public class Agent

{



public string Login(string userName, string password)

{

string AgentID;



DBAccess data = new DBAccess();

SqlParameter[] prams = {


data.MakeInParam("@username", SqlDbType.VarChar, 25, userName),


data.MakeInParam("@Password", SqlDbType.VarChar, 25, password),

};



data.RunProc("upAccountLogin", prams);

AgentID = (string) prams[2].Value;



if (AgentID == string.Empty)

return null;

else

return AgentID;

}



}

}

Agent.vb
Imports System

Imports System.Data

Imports System.Data.SqlClient





Namespace ETMarket.Components

_



Public Class Agent





Public Function Login(userName As String, password As String) As
String

Dim AgentID As String



Dim data As New DBAccess()

Dim prams As SqlParameter() = {data.MakeInParam("@username",
SqlDbType.VarChar, 25, userName), data.MakeInParam("@Password",
SqlDbType.VarChar, 25, password)}



data.RunProc("upAccountLogin", prams)

AgentID = CStr(prams(2).Value)



If AgentID = String.Empty Then

Return Nothing

Else

Return AgentID

End If

End Function 'Login

End Class 'Agent

End Namespace 'PetShop.Components
 
F

Fergus Cooney

Hi Armin,

|| my keyboard is organic and keys grow by itself. ;)

LOL.

I'd like one of those - especially if it can lick itself clean every now
and then, and purr when I type on it. (Just so long as it leaves the mouse
alone).

Regards,
Fergus
 

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

Top