locked
collection on c# or similar RRS feed

  • Question

  • User103585383 posted

     hello friends I have a function in vb that need it in c # (do not send me to http://www.developerfusion.com/tools/convert/vb-to-csharp/, because I did), it translates well, but VStudio not compiled


    Compiler Error Message: CS0246: Can not find the type or namespace name 'Collection' (are you missing a using directive or an assembly reference?)

    no alternative to collections, what I can do?



    Public Shared Function GetQuestionIDsForSurvey(ByVal id As Integer) As Collection
            Try

                'call the GetQuestionsForSurvey function which returns a dataset...

               Dim dsQuestions As DataSet = GetQuestionsForSurvey(id)
                'get the first table in the dataset
                Dim dtQuestions As DataTable = dsQuestions.Tables(0)
                'obtain a reference to a collection object
                Dim mQuestionCollection As New Collection
                'iterate through the rows of Question ID's adding them to a collection
                For Each dr As DataRow In dtQuestions.Rows
                    mQuestionCollection.Add(dr.Item("ID").ToString())
                Next
                'return the collection of question ID's
                Return mQuestionCollection

            Catch ex As Exception

                Throw
            End Try
        End Function

    ----------------------c#-----------------------

    public static Collection GetQuestionIDsForSurvey(int id)

           {

                 try {

                        //call the GetQuestionsForSurvey function which returns a dataset...

                        DataSet dsQuestions = GetQuestionsForSurvey(id);

                        //get the first table in the dataset

                        DataTable dtQuestions = dsQuestions.Tables[0];

                        //obtain a reference to a collection object

                        Collection mQuestionCollection = new Collection();

                        //iterate through the rows of Question ID's adding them to a collection

                        foreach (DataRow dr in dtQuestions.Rows) {

                               mQuestionCollection.Add(dr["ID"].ToString());

                        }

                        //return the collection of question ID's

                        return mQuestionCollection;

     

                 } catch (Exception ex) {

                        throw;       } 

                }

    Wednesday, December 1, 2010 12:22 AM

Answers

  • User-1181492241 posted

    You can use ArrayList or Hashtable in C# as alternative.

    Add the namespace also.

    using System.Collections; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 1, 2010 6:37 AM

All replies

  • User-1181492241 posted

    You can use ArrayList or Hashtable in C# as alternative.

    Add the namespace also.

    using System.Collections; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 1, 2010 6:37 AM
  • User397347636 posted

    The best solution is to use a real .NET collection, as N. Rajesh mentioned.

    If you really need to use the original Collection type, just reference the Microsoft.VisualBasic assembly and use it.

    Wednesday, December 1, 2010 10:46 AM
  • User103585383 posted

    thanks for the suggestions and views. apologizes for the delay I am new to this issue (c #). How to implement?, as I do this "just reference the Microsoft.VisualBasic assembly and use it."

    Thursday, December 2, 2010 11:59 AM
  • User-1181492241 posted

    Hi,

    If you want to use ArrayList, just replace Collection with ArrayList in your code. I think ArrayList serves your purpose in this context. 

    Friday, December 3, 2010 7:20 AM