Testing for variables #19

Open
opened 2021-07-09 11:50:38 +00:00 by miloush · 4 comments
miloush commented 2021-07-09 11:50:38 +00:00 (Migrated from github.com)

There doesn't seem to be an easy way to check if a variable exists in a given file, and unfortunately the IMatFile indexer throws an exception when a variable does not exist. It would be useful to have a more tolerant way to access variables, such as one of the following:

  • have IMatFile[string] return null if variable does not exist
  • add IMatFile.TryGetVariable(string, out IVariable) method
  • expose the underlying dictionary as IReadOnlyDictionary<string, IVariable>
There doesn't seem to be an easy way to check if a variable exists in a given file, and unfortunately the `IMatFile` indexer throws an exception when a variable does not exist. It would be useful to have a more tolerant way to access variables, such as one of the following: * have `IMatFile[string]` return `null` if variable does not exist * add `IMatFile.TryGetVariable(string, out IVariable)` method * expose the underlying dictionary as `IReadOnlyDictionary<string, IVariable>`
mahalex commented 2021-07-10 07:58:36 +00:00 (Migrated from github.com)

Thanks for creating the issue!
The current way to check if a variable exists is go through the IMatFile.Variables array and test the Name property of the elements, which is quite ugly.
I will go with the second proposed solution. Another approach would be to inherit IMatFile from IReadOnlyDictionary<string, IVariable> or IDictionary<string, IVariable>, but there are some problems associated with that: I feel like it should be a read-only dictionary, but there is already a setter on the indexer for some reason. I guess we can do it when (if) we think about the next major version with breaking changes to the API.

Thanks for creating the issue! The current way to check if a variable exists is go through the `IMatFile.Variables` array and test the `Name` property of the elements, which is quite ugly. I will go with the second proposed solution. Another approach would be to inherit `IMatFile` from `IReadOnlyDictionary<string, IVariable>` or `IDictionary<string, IVariable>`, but there are some problems associated with that: I feel like it should be a read-only dictionary, but there is already a setter on the indexer for some reason. I guess we can do it when (if) we think about the next major version with breaking changes to the API.
mahalex commented 2021-07-10 10:28:07 +00:00 (Migrated from github.com)

Version 1.4.0-beta1 published on nuget.org contains this change. Feel free to test it.

Version 1.4.0-beta1 published on nuget.org contains this change. Feel free to test it.
miloush commented 2021-07-15 20:11:52 +00:00 (Migrated from github.com)

Great, working for me!

Great, working for me!
cbergemann commented 2022-04-24 12:54:47 +00:00 (Migrated from github.com)

It would be very usefull to have the same interface for IStructureArray - currently you need to iterate over the FieldNames property. Exposing the TryGet method of the underlying Dictionary would help.

It would be very usefull to have the same interface for IStructureArray - currently you need to iterate over the FieldNames property. Exposing the TryGet method of the underlying Dictionary would help.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mahalex/MatFileHandler#19
No description provided.