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