KlausWiesel

Hello

I have this code, which adds files in a listbox

Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim f As Scripting.Folder
Set f = fso.GetFolder("c:\windows\system32")

Dim sa As mscorlib.SortedList
Set sa = New SortedList

Dim fi As Scripting.File
For Each fi In f.Files
sa.Add fi.Name, fi.Size
Next

Dim i As Integer
For i = 0 To sa.Count - 1
List1.AddItem (sa.GetKey(i))
Next

Looping the sortedlist I have access to the key part using GetKey. How can I read the value part
GetValue is not available

The objectbrowser does not show any methods for sortedlist, is there a way to display the available interface details, as the ".net" operations like sa.key and sa.value does not work either


Regards
Klaus


Re: Visual Basic Interop and Upgrade Using SortedList from VB6

Bruno Yu - MSFT

KlausWiesel,

Based on your post, you are using FileSystemObject in VB 6 development for adding files in ListBox. I would like to provide you the suggestions in .NET development on the issue because .NET Framework provides you the better solutions on this kind of question. Furthermore, VB 6 questioins are not suppported any more in MSDN forums.

1. You can still use the FileSystemObject in .NET. Because the FileSystemObject is a Component Object Model (COM) component, .NET requires that access to the object be through the Interop layer. .NET generates a wrapper for the component for you if you want to use it. However, the File, FileInfo, Directory, DirectoryInfo classes, and other related classes in the .NET Framework, offer functionality that is not available with the FSO, without the overhead of the Interop layer.

2. I would like to provide you the following article that shows you how to do six basic file input/output (I/O) operations in Microsoft Visual Basic 2005 or in Microsoft Visual Basic .NET. If you are new to .NET, you will find that the object model for file operations in .NET is similar to the FileSystemObject (FSO) that is popular with many Visual Studio 6.0 developers.

How to do basic file I/O in Visual Basic 2005 or in Visual Basic .NET

3. You can also take a look at this thread on adding file to the ListBox: How do i add a file into a listbox

4. Visual Basic Interop and Upgrade forum discuss how to do upgrades of VB6 code to VB.NET. Covers questions on interop features, Interop Forms Toolkit, Upgrade wizard, but NOT for questions about VB6. If you have any questions only in VB 6 development, please post the question in the following list of forums, newsgroups, resources and sites, to get an answer about your question :

Hope that can help you and thanks again for your question.






Re: Visual Basic Interop and Upgrade Using SortedList from VB6

KlausWiesel

Bruno

I think I have to clarify:

We have tons of vb6 code to bring to .NET and YES, therefore I think this is the right forum.

The core of my question is, how I can consume a SortedList most easily from VB6 (assume that I rewrite some functionality in .NET and return a SortedList as a function result for a assembly, which could be consumed from .NET and VB6 simultaneously.

I was wondering who I can (in general) retrieve the possible functions I can use on the vb6 side, as F12 (vb6 object browser) does not show the available interface

Klaus





Re: Visual Basic Interop and Upgrade Using SortedList from VB6

Bruno Yu - MSFT

Klaus Wielsel,

In .NET development, I would like to suggst you use System.Collections.SortedList class. The following link on SortedList class in MSDN shows you the specific information in .NET Framework Class Library:

http://msdn2.microsoft.com/en-us/library/system.collections.sortedlist(vs.71).aspx

Since you would like to consume the SortedList class in both .NEt and VB 6 simultaneously, I would like to suggest you to try to call the .NET SortedList class in VB 6 or the opposite way. If there is any questions on calling managed assembly from the unmanaged project, or use VB 6 component in .NET application, I would like to suggest you the following articles in MSDN and hope that can help you on this kind of problems:

1. Calling a .NET Component from a COM Component
http://msdn2.microsoft.com/en-us/library/ms973802.aspx

2. Calling COM Components from .NET Clients
http://msdn2.microsoft.com/en-us/library/ms973800.aspx

3. How to call a Visual Basic .NET or Visual Basic 2005 assembly from Visual Basic 6.0
http://support.microsoft.com/kb/817248

Hope that can help you/




Re: Visual Basic Interop and Upgrade Using SortedList from VB6

KlausWiesel

Bruno

my problem is that I already use mscorlib form .NET in my VB6 program

Here is the code

Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim f As Scripting.Folder
Set f = fso.GetFolder("c:\windows\system32")

Dim sa As mscorlib.SortedList
Set sa = New SortedList

Dim fi As Scripting.File
For Each fi In f.Files
sa.Add fi.Name, fi.Size
Next

Dim i As Integer
For i = 0 To sa.Count - 1
List1.AddItem (sa.GetKey(i))
Next



mscorlib (NET 2.0 framework) is referenced using the .tlb file

This code works, but if you look closely, additional to the file name (which goes to the key part of the sortedlist) there is the file size stored in the sortedlist as well.

To retrieve the key, there is a function .GetKey
But what I miss is something like .GetValue

My questions are:
1) for this specific scenario: how can I retrieve the value part of the sortedlist
2) in general: is there a chance to lookup the available methods/properties from vb6 side, as for sortedlist, the vb6 object browser does not show any members

Thanks for your support
Klaus





Re: Visual Basic Interop and Upgrade Using SortedList from VB6

Bruno Yu - MSFT

KlausWiesel,

I found an article Using the .NET Framework Class Library from Visual Basic 6 in MSDN with the same code snippet you provided. I am guessing your question in from this article.

As is shown in the article, the .NET Framework includes classes that make it easy to work with the file system. I still recommend you to use the SortedList class in .NET development. You can use SortedList.ContainsKey method in System.Collections.Generic namespace to determines whether the SortedList contains a specific key. For further information and the example, please take a look at the following link provided in MSDN:

http://msdn2.microsoft.com/en-us/library/ms132332.aspx

Hope that can provide you some idea.






Re: Visual Basic Interop and Upgrade Using SortedList from VB6

KlausWiesel

Bruno

your right, the question arose by using this code.

What I am thinking about is using .NET assemblies from VB6 with the lowest possible effort.

Therefore, if an assemly has a sortedlist as return value or parameter, it would be nice to be able to consume it from within VB6. I am aware that I can easily write a GetValue function in the assembly, which returns the value when I call this from vb6, but I am searching for a more generic approach.

Though the code example you mentioned is also about file system, it shows that there is a access function named "GetKey" (in VB6 for mscorlib). All I'm asking is, if there is a "GetValue" (for vb6 in mscorlib), too. I know that the function "GetValue" is not available, maybe it has an other name. And if there is one, how can I find it (object browser, web site, ...

Thanks again

Klaus





Re: Visual Basic Interop and Upgrade Using SortedList from VB6

Toddap_MS

(1) This code does what you want...

Private Sub Form_Load()
Dim x As New mscorlib.SortedList
x.Add "HI", "THER2"
x.Add "Hi2", "There2"

Dim i As Integer
For i = 0 To x.Count - 1
Debug.Print x.GetKey(i)
Debug.Print x.GetValueList.Item(0)
Next i

End Sub

2) It's unfortunate that VB6 doesn't show you all properties/methods for each object, but It's probably just a side effect of the typelib that's generated from this file. The easiest workaround is to just play with the object from VB.NET and figure out what to call.

Good luck!


Toddap_MS





Re: Visual Basic Interop and Upgrade Using SortedList from VB6

KlausWiesel

Toddap_MS,

thanks, question 1 is answered

But it's a pity that there is no kind of list for the interface available in VB6

Regards

Klaus