shanvidhya
asked on
VB Collection code equivalent in C#
Can some expert provide me equivalet C# code from the below VB code. Thanks for your help in advance.
I have the following data in a table tempdata:
EmpNo EmpName DeptNo
1 Roger 1
2 Mike 1
3 Wayne 2
4 Whitney 3
5 Anne 3
Main.frm has following code
Private Sub Command1_Click()
Dim oRS As ADODB.Recordset
Dim oCn As ADODB.Connection
Dim mDataBaseName As String
Dim oEmp As Class1
Dim cEmp As Collection
Dim iLoop As Integer
Set oRS = New ADODB.Recordset
Set oCn = New ADODB.Connection
Set cEmp = New Collection
mDataBaseName = App.Path & "\test.mdb"
oCn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & mDataBaseName & ";Jet OLEDB:Database Password=" & PWD & ";")
oRS.CursorLocation = adUseClient
oRS.Open "select * from tempdata", oCn
Do While Not oRS.EOF
Set oEmp = New Class1
With oEmp
.EmpNo = oRS.Fields("EmpNo").Value
.EmpName = oRS.Fields("EmpName").Value
.DeptNo = oRS.Fields("DeptNo").Value
End With
cEmp.Add oEmp
Loop
For iLoop = 1 To cEmp.Count
With cEmp.Item(iLoop)
Debug.Print .EmpNo, .EmpName, .DeptNo
End With
Next iLoop
If oRS.State Then oRS.Close
If oCn.State Then oCn.Close
Set cEmp = Nothing
Set oRS = Nothing
Set oCn = Nothing
End Sub
class1.cls has following code:
Private mEmpNo As Integer
Private mEmpName As String
Private mDeptNo As Integer
Public Property Let EmpNo(vEmpNo As Integer)
mEmpNo = vEmpNo
End Property
Public Property Get EmpNo() As Integer
EmpNo = mEmpNo
End Property
Public Property Let EmpName(vEmpName As String)
mEmpName = vEmpName
End Property
Public Property Get EmpName() As String
EmpName = mEmpName
End Property
Public Property Let DeptNo(vDeptNo As Integer)
mDeptNo = vDeptNo
End Property
Public Property Let DeptNo(vDeptNo As Integer)
DeptNo = mDeptNo
End Property
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use the following link to do conversions
http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://www.carlosag.net/Tools/CodeTranslator/
http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://www.carlosag.net/Tools/CodeTranslator/
ASKER
philipjonathan,
I get an error in oRs part saying "The Name oRS do not exist in the current context" for
(int) oRS["EmpNo"];
(string) oRS["EmpName"];
(int) oRS["DeptNo"];
from the below part. Please advice.
while (oRs.Read())
{
Class1 oEmp = new Class1();
oEmp.EmpNo = (int) oRS["EmpNo"];
oEmp.EmpName = (string) oRS["EmpName"];
oEmp.DeptNo = (int) oRS["DeptNo"];
cEmp.Add(oEmp);
}
Thanks,
Shan
I get an error in oRs part saying "The Name oRS do not exist in the current context" for
(int) oRS["EmpNo"];
(string) oRS["EmpName"];
(int) oRS["DeptNo"];
from the below part. Please advice.
while (oRs.Read())
{
Class1 oEmp = new Class1();
oEmp.EmpNo = (int) oRS["EmpNo"];
oEmp.EmpName = (string) oRS["EmpName"];
oEmp.DeptNo = (int) oRS["DeptNo"];
cEmp.Add(oEmp);
}
Thanks,
Shan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Philipjonathan and Harry for providing me right solution. I am a VB developer moving to C#. Jonathan's last comment (upper and lower case typo problems) will help me to avoid more mistakes in future. You guys are great.
Open in new window