Solved

VB Collection code equivalent in C#

Posted on 2009-04-07
6
424 Views
Last Modified: 2013-12-25
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

Open in new window

0
Comment
Question by:shanvidhya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24093633
Something like this:
private void Command1_Click()
{
  List<Class1> cEmp = new List<Class1>();
 
  string mDataBaseName = App.Path + "\\test.mdb";
  string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + mDataBaseName + ";Jet OLEDB:Database Password=" + PWD + ";";
  OleDbConnection oCn = new OleDbConnection(connectionString);
  oCn.Open();
 
  string cmdText = "select * from tempdata";
  OleDbCommand cmd = new OleDbCommand(cmdText, oCn);
  OleDbDataReader oRs = cmd.ExecuteReader();
 
  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);
  }
 
  for (int iLoop = 0; iLoop < cEmp.Count; i++)
  {
    Class1 oEmp = cEmp[iLoop];
    System.Diagnostics.Debug.WriteLine(string.Format("{0} {1} {2}", oEmp.EmpNo, oEmp.EmpName, oEmp.DeptNo));
  }
 
  oRs.Close();
  oCn.Close();
}

Open in new window

0
 
LVL 6

Assisted Solution

by:HarryNS
HarryNS earned 25 total points
ID: 24094834
C# code for Class file:

using System;
 
public class class1
{
 
    int mEmpNo;
    string mEmpName;
    int mDeptNo;
 
    public int EmpNo
    {
        get { return mEmpNo; }
        set { mEmpNo = value; }
    }    
    public string EmpName
    {
        get { return mEmpName; }
        set { mEmpName = value; }
    }
    public int DeptNo
    {
        get { return mDeptNo; }
        set { mDeptNo = value; }
    }
 
}

Open in new window

0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24096853
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shanvidhya
ID: 24097400
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
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 100 total points
ID: 24097634
Sorry, typo-error, should be oRs, instead of oRS (note the small letter s).


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);
  }
0
 

Author Closing Comment

by:shanvidhya
ID: 31567837
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question