Silverfrost Forums

Welcome to our forums

Elements of DotNet Collection

24 Dec 2008 12:46 #4135

How can I access the elements of a dotnet collection (e.g. a DataTable in the System.Data.DataTablesCollection)?

It seems to be immpossible with the Index, because you cannot declare a collection as an array. If you try, you get

Error 1 Conversion from System.Data.DataTableCollection to System.Data.DataTableCollection[] not possible.

Also the collection ist not an Array of DataTables like this: 'System.Data.DataTable[]'

If you declase the collection as a scalar and try to access its elements with the index position you get

Error 1 Array TABS appears in this expression as rank 1, but was declared as a scalar

There seems to be no other accessor method that could be declared via ASSEMLY_EXTERNAL.

What's the trick here? :?:

27 Dec 2008 4:12 #4139

If it is really impossible to to answer this question (I can't believe that or I'm missing a very obvious thing) then a I have related question:

Has anybody a working example for reading/writing a MS-Access Database, SQL-Server Database or Excel-Sheet via OleDb, ODBC, DAO, ADO.NET or some other .Net-way?

27 Dec 2008 6:43 #4140

I do not know if I can help with this but I recommend that you set your question in the context of some sample code to illustrate clearly what you aim to do and what fails to happen.

27 Dec 2008 7:32 #4141

Here is the program:

program ReadMDB

library ('System') library ('System.Data') implicit none

object('System.Data.OleDb.OleDbCommand') :: myCommand object('System.Data.OleDb.OleDbDataAdapter') :: myDataAdapter object('System.Data.OleDb.OleDbConnection') :: myConn object('System.Data.DataTableCollection') :: tabs !trying to define as array produces error 1a and 2a below !object('System.Data.DataTableCollection[]') :: tabs object('System.Data.DataSet') :: myDataSet object('System.Data.DataTable') :: myTab

string :: strConn string :: strSelect integer :: res

assembly_external(name='System.Data.OleDb.OleDbConnection.Open') dc_open assembly_external(name='System.Data.OleDb.OleDbConnection.Close') dc_close assembly_external(name='System.Data.OleDb.OleDbDataAdapter.Fill') da_fill

strConn = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Test.mdb' strSelect = 'SELECT * FROM tblTest'

myConn = new@('System.Data.OleDb.OleDbConnection',strConn) myCommand = new@('System.Data.OleDb.OleDbCommand',strSelect,myConn) myDataAdapter = new@('System.Data.OleDb.OleDbDataAdapter',myCommand) myDataSet = new@('System.Data.DataSet')

res=da_fill(myDataAdapter,myDataSet,'Test') tabs=MyDataset%Tables

print*, res !result ok. Numer of selected records print*, tabs%count !ok. 1 table

!HOW DO I ACCESS THE ELEMENTS OF THE DATATABLECOLLECTION tabs (or any other collection?) mytab=tabs(0) !Error 1 Array TABS appears in this expression as rank 1, but was declared as a scalar D:\Visual Studio 2008\Projects\F95Projects\ReadFromAccess\ReadFromAccess\ReadMdb.F95 41 !Error 2 Conversion from System.Data.DataTableCollection to System.Data.DataTable not possible D:\Visual Studio 2008\Projects\F95Projects\ReadFromAccess\ReadFromAccess\ReadMdb.F95 41 !if tabs is defined as DataTableCollection[] !Error 1a Conversion from System.Data.DataTableCollection to System.Data.DataTableCollection[] not possible D:\Visual Studio 2008\Projects\F95Projects\ReadFromAccess\ReadFromAccess\ReadMdb.F95 39 !Error 2a Member count does not exist (or is not publicly accessible) in OBJECT('System.Data.DataTableCollection[]') D:\Visual Studio 2008\Projects\F95Projects\ReadFromAccess\ReadFromAccess\ReadMdb.F95 44

!trying to access default attribute: !mytab=tabs%item(0) ! produces !Error 1 Internal compiler error D:\Visual Studio 2008\Projects\F95Projects\ReadFromAccess\ReadFromAccess\ReadMdb.F95 54

read * call dc_close(myConn)

end program

29 Dec 2008 9:53 #4144

I have had a look at your code and can confirm that you cannot access an item in a .NET collection via its index. If I wanted to do this I would consider writing a C# function to extract the item. This function would be included in a C# DLL which I would then access from my Fortran code.

29 Dec 2008 1:08 #4145

Thank you Paul, that works. It is usable as a workaround.

But given the central role of collections and other higher order datatypes (e.g. dictionaries) that are variants of linked lists and hashtables you should really implement some form of direct support for them. Without that the Dotnet-support is really 'basic'. You can use only a very resticted part of the framework without the help of other dotnet languages.

Please login to reply.