Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi my name Vishal. For past 3 days i have been breaking my head on finding c# equivalent of vb6's Collection(data type) and use it in c# windows forms.
Given below is my vb6 code which i am trying to convert to c#:
VB
Option Explicit
Public pCollConn As New Collection
Public Function getConnectionIndex(IPString As String) As Long
    On Error GoTo errorH
    Dim i As Long
    i = Val(Trim(pCollConn(IPString)))
    getConnectionIndex = i + 1
    Exit Function
errorH:
    getConnectionIndex = 0
End Function


The above code executes well with no problem at all(be it compile time/run-time errors).
where getConnectionIndex is my method of data type Long with IPString as it's parameter which is of data type String in vb6.

Given below is my c# code interpretation of above vb6 code:
C#
 public ArrayList pCollConn = new ArrayList();
public int getConnectionIndex(String IPString)
        {
            int i;   
            i=Convert.ToInt32(pCollConn.Add(IPString).ToString().Trim());
            return getConnectionIndex(IPString);
        }

I have included namespace using System.Collections.Generic in my c# code.

In my above c# code interpretation of vb6 code i am getting stuck in given below lines of converting/interpreting vb6 code to c# code:
VB
i = Val(Trim(pCollConn(IPString)))
    getConnectionIndex = i + 1


i know that this is a silly/stupid question of finding alternate/equivalent of Collection data type of vb6 to c# but in solving of this problem would help me finish my project that i am doing winsock control in c# windows forms(I am using Winsock control in c# windows forms because of my BOSS order's).

I should not use any convertor/any namespace of(using Microsoft.VisualBasic) since of my BOSS order's too.
Can anyone help me on what is equivalent of Collection data type of vb6 in c# windows forms? and can anyone help me/guide me to code correctly of vb6 code below in c# of which i am getting stuck on in converting/interpreting vb6 code to c#:
VB
i = Val(Trim(pCollConn(IPString)))
    getConnectionIndex = i + 1

Can anyone help me please?! Any help/guidance in solving of this problem would be greatly appreciated!
Posted
Comments
Pikoh 20-Jun-14 3:17am    
So,let me see if i understand what you wanna do. You want to be able to access a collection of integers by an index?
Member 10248768 20-Jun-14 3:22am    
Dear Pikoh thanks for replying to my question on such short notice. Yes i want to able to access a collection of integers by an index in c# windows forms!

Well..there are probably a lot of methods to acomplish what you're trying to do. As your index is a string if i'm not wrong,i suggest using a dictionary.

Let's see an example

C#
Dictionary<string,int> dictionary;//We define a dictionary with a string key and a int value

//Let's add values 
dictionary=new Dictionary<string,int>();
dictionary.Add("cat", 1);
dictionary.Add("dog", 2);
dictionary.Add("fish",3);

//We call a method to get the next value 
getNextValue("dog");


private int getNextValue(string key)
{
    int i=dictionary[key]; //We get the value for the key 
    i++; 
    return i;
}


It's just an example. Hope it gives you an idea of how to accoplish it.


[EDIT]

Now i'm thinking of it,to your requirements i think a better aproach is a OrderedDictionary. More on this here
 
Share this answer
 
v4
Comments
Member 10248768 20-Jun-14 3:53am    
Hi Pikoh
thanks for your solution on such short notice.
I just now tried your solution but i am getting compile error when declaring Dictionary.Given below is my line in c#:
Dictionary<string,> pCollConn;
here after "," in dictionary i am getting compile error message "Type expected" Tell me/guide me on how to resolve this problem? Reply please?
where pCollConn is name of my dictionary
Pikoh 20-Jun-14 3:56am    
see my improved solution. in the dictionary definition,after string, there should be int. In the frocess of copy/paste got lost
Hi.

In your VB6 sample code, you are retrieving items using a key, that is like a dictionary.
You did not include VB6 sample code that adds items, but having IPString as the parameter to fetch tems, the instructions you used were like
VB
pCollConn.Add item, IPString
.

There are different matching classes, depending of the way you add or retrieve items in VB6.

First of all, the VB6 Collection class is fully matched by Microsoft.VisualBasic.Collection, from the Microsoft.VisualBasic.dll assembly (see Collection class): the simplest conversion would be to use that class, but depending on each usage case, you may found other classes suit best.

Examples:
- If you add items passing only 1 parameter, that is the item, and retrieve the items only by looping, than the best options are ArrayList or List<T> (where T is the type of the item if it is always the same for that collection object).
- If you add items passing only 1 parameter, the item, and retrieve them by index, than the best options would be again ArrayList or List<T>.
- If you add items passing only 1 parameter, the item, and later checks for existance, than you should adopt HashSet<T> or Collection<T>.
- If you add passing 2 parameters, that are the item value and the item key, and retrieve values by key, then you should use HashTable or Dictionary<TKey,TValue> or SortedList<TKet,TValue>.

There are even more classes you can choose from, but the ones listed above are the most common.

Regards,
Daniele.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900