VB Collection code equivalent in C#

Posted on 2009-04-07
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


    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

Question by:shanvidhya
LVL 18

Expert Comment

Comment Utility
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);


  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"];



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





Open in new window


Assisted Solution

HarryNS earned 25 total points
Comment Utility
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

LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.


Author Comment

Comment Utility

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"];

LVL 18

Accepted Solution

philipjonathan earned 100 total points
Comment Utility
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"];

Author Closing Comment

Comment Utility
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.

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now