diff --git a/Ndapi/Block.cs b/Ndapi/Block.cs index dac4b8d..93cd6d8 100644 --- a/Ndapi/Block.cs +++ b/Ndapi/Block.cs @@ -387,7 +387,7 @@ public string QueryDataSourceName public QueryDataSourceType QueryDataSourceType { get => GetNumberProperty(NdapiConstants.D2FP_QRY_DAT_SRC_TYP); - set => SetNumberProperty(NdapiConstants.D2FP_QRY_DAT_SRC_TYP, value); + set => SetNumberProperty(NdapiConstants.D2FP_QRY_DAT_SRC_TYP, value); } /// diff --git a/Ndapi/Core/PropertyAttribute.cs b/Ndapi/Core/PropertyAttribute.cs index 906fbbb..0060d6c 100644 --- a/Ndapi/Core/PropertyAttribute.cs +++ b/Ndapi/Core/PropertyAttribute.cs @@ -2,7 +2,7 @@ namespace Ndapi.Core { - [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + [AttributeUsage(AttributeTargets.Property)] internal sealed class PropertyAttribute : Attribute { internal int PropertyId { get; } diff --git a/Ndapi/FormModule.cs b/Ndapi/FormModule.cs index db75e57..0127781 100644 --- a/Ndapi/FormModule.cs +++ b/Ndapi/FormModule.cs @@ -386,7 +386,7 @@ public new static FormModule Open(string filename) #if FORMS_6 var status = NativeMethods.d2ffmdld_Load(NdapiContext.GetContext(), out var form, filename, false); #else - var status = NativeMethods.d2ffmdld_Load(NdapiContext.GetContext(), out form, filename); + var status = NativeMethods.d2ffmdld_Load(NdapiContext.GetContext(), out var form, filename); #endif Ensure.Success(status); @@ -443,23 +443,13 @@ public override void CompileObjects() Ensure.Success(status); } - /// - /// Gets the version of the last Form Builder that loaded the module. - /// - /// Form module location (.fmb file) - /// The Form Builder version - public static int GetFileVersion(string file) - { - return GetFileVersion(file, false); - } - /// /// Gets the version of the last Form Builder that loaded the module. /// /// Form module location (.fmb file) /// Module should be loaded from database. /// The Form Builder version - public static int GetFileVersion(string file, bool loadFromDb) + public static int GetFileVersion(string file, bool loadFromDb = false) { #if FORMS_6 var status = NativeMethods.d2ffmdfv_FileVersion(NdapiContext.GetContext(), file, loadFromDb, out var version); diff --git a/Ndapi/LibraryModule.cs b/Ndapi/LibraryModule.cs index 6295483..e123f84 100644 --- a/Ndapi/LibraryModule.cs +++ b/Ndapi/LibraryModule.cs @@ -91,7 +91,7 @@ public override void Save(string path) /// /// Program unit libraries does not support saving. /// - public override void Save(string path = null, bool saveInDatabase = false) + public override void Save(string path, bool saveInDatabase) { throw new NotSupportedException("Library module does not support saving"); } diff --git a/Ndapi/MenuModule.cs b/Ndapi/MenuModule.cs index f1c1fc8..20d3345 100644 --- a/Ndapi/MenuModule.cs +++ b/Ndapi/MenuModule.cs @@ -232,23 +232,13 @@ public override void CompileObjects() Ensure.Success(status); } - /// - /// Gets the version of the last Form Builder that loaded the module. - /// - /// Menu module location (.mmb file) - /// The Form Builder version - public static int GetFileVersion(string file) - { - return GetFileVersion(file, false); - } - /// /// Gets the version of the last Form Builder that loaded the module. /// /// Menu module location (.mmb file) /// Module should be loaded from database. /// The Form Builder version - public static int GetFileVersion(string file, bool loadFromDb) + public static int GetFileVersion(string file, bool loadFromDb = false) { #if FORMS_6 var status = NativeMethods.d2fmmdfv_FileVersion(NdapiContext.GetContext(), file, loadFromDb, out var version); diff --git a/Ndapi/Metadata/NdapiMetaProperty.cs b/Ndapi/Metadata/NdapiMetaProperty.cs index 75c7f73..11d871a 100644 --- a/Ndapi/Metadata/NdapiMetaProperty.cs +++ b/Ndapi/Metadata/NdapiMetaProperty.cs @@ -96,12 +96,9 @@ internal static NdapiMetaProperty GetOrCreate(int propertyId, string name, bool private Dictionary LoadAllowedValues() { - if (AcceptConstants) - { - return Enum.GetValues(RawPropertyType).Cast().ToDictionary(e => e, e => Enum.GetName(RawPropertyType, e)); - } - - return new Dictionary(); + return AcceptConstants ? + Enum.GetValues(RawPropertyType).Cast().ToDictionary(e => e, e => Enum.GetName(RawPropertyType, e)) : + new Dictionary(); } /// diff --git a/Ndapi/NdapiContext.cs b/Ndapi/NdapiContext.cs index 7f13595..ab675cb 100644 --- a/Ndapi/NdapiContext.cs +++ b/Ndapi/NdapiContext.cs @@ -43,25 +43,30 @@ public sealed class NdapiContext : IDisposable internal static ContextSafeHandle GetContext() { - if (_context == null) + if (_context != null) { - var context_attributes = new D2fContextAttributes(); - context_attributes.mask_d2fctxa = D2FCTXAMCALLS; - context_attributes.d2fmalc_d2fctxa = allocateMemory; - context_attributes.d2fmrlc_d2fctxa = reallocateMemory; - context_attributes.d2fmfre_d2fctxa = freeMemory; - D2fErrorCode status; - try - { - status = NativeMethods.d2fctxcr_Create(out _context, ref context_attributes); - } - catch (DllNotFoundException) - { - throw new NdapiException($"Could not found the {NativeMethods.formsLib} from Oracle Forms installation. " + - "Please check if this version of Oracle Forms is installed."); - } - Ensure.Success(status); + return _context; + } + + var contextAttributes = new D2fContextAttributes + { + mask_d2fctxa = D2FCTXAMCALLS, + d2fmalc_d2fctxa = allocateMemory, + d2fmrlc_d2fctxa = reallocateMemory, + d2fmfre_d2fctxa = freeMemory + }; + + D2fErrorCode status; + try + { + status = NativeMethods.d2fctxcr_Create(out _context, ref contextAttributes); } + catch (DllNotFoundException) + { + throw new NdapiException($"Could not found the {NativeMethods.formsLib} from Oracle Forms installation. " + + "Please check if this version of Oracle Forms is installed."); + } + Ensure.Success(status); return _context; } @@ -72,12 +77,7 @@ private static IntPtr AllocateMemory(ref IntPtr context, IntPtr size) private static IntPtr ReallocateMemory(ref IntPtr context, IntPtr ptr, IntPtr newsize) { - if (ptr == IntPtr.Zero) - { - return AllocateMemory(ref context, newsize); - } - - return Marshal.ReAllocHGlobal(ptr, newsize); + return ptr == IntPtr.Zero ? AllocateMemory(ref context, newsize) : Marshal.ReAllocHGlobal(ptr, newsize); } private static void FreeMemory(ref IntPtr context, IntPtr ptr) diff --git a/Ndapi/NdapiException.cs b/Ndapi/NdapiException.cs index 5596ff9..a814418 100644 --- a/Ndapi/NdapiException.cs +++ b/Ndapi/NdapiException.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.Serialization; using Ndapi.Enums; @@ -9,7 +8,7 @@ namespace Ndapi /// The exception that is thrown when an error occurs during application execution. /// [Serializable] - public class NdapiException : Exception + public sealed class NdapiException : Exception { /// /// Gets the internal Forms API error code. @@ -40,15 +39,6 @@ public NdapiException(string message, Exception inner) : base(message, inner) { } - /// - /// Initializes a new instance of the class with a serialized data. - /// - /// The that holds the serialized object data about the exception being thrown. - /// The that contains contextual information about the source or destination. - protected NdapiException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } - internal NdapiException(string message, D2fErrorCode code) : this(message) { ErrorCode = code; diff --git a/Ndapi/NdapiMenuItemRoleList.cs b/Ndapi/NdapiMenuItemRoleList.cs index 8cbb983..b7c29ef 100644 --- a/Ndapi/NdapiMenuItemRoleList.cs +++ b/Ndapi/NdapiMenuItemRoleList.cs @@ -6,15 +6,14 @@ namespace Ndapi public class NdapiMenuItemRoleList : IEnumerable { private readonly MenuItem _menuItem; - private readonly int _count; internal NdapiMenuItemRoleList(MenuItem menuitem) { _menuItem = menuitem; - _count = menuitem.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT); + Count = menuitem.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT); } - public int Count => _count; + public int Count { get; } public IEnumerator GetEnumerator() => new Enumerator(this); @@ -24,7 +23,6 @@ public sealed class Enumerator : IEnumerator { private readonly NdapiMenuItemRoleList _ndapiMenuItemRoleList; private int _position; - private string _current; internal Enumerator(NdapiMenuItemRoleList ndapiMenuItemRoleList) { @@ -39,14 +37,14 @@ public bool MoveNext() return false; } - _current = _ndapiMenuItemRoleList._menuItem.GetRoleAt(_position); + Current = _ndapiMenuItemRoleList._menuItem.GetRoleAt(_position); _position++; return true; } public void Reset() => _position = 1; - public string Current => _current; + public string Current { get; private set; } object IEnumerator.Current => Current; diff --git a/Ndapi/NdapiObject.cs b/Ndapi/NdapiObject.cs index d47ad15..0e39f89 100644 --- a/Ndapi/NdapiObject.cs +++ b/Ndapi/NdapiObject.cs @@ -99,7 +99,7 @@ public int ParentModuleType public ModuleStorageType ParentModuleStorage { get => GetNumberProperty(NdapiConstants.D2FP_PAR_MODSTR); - set => SetNumberProperty(NdapiConstants.D2FP_PAR_MODSTR, value); + set => SetNumberProperty(NdapiConstants.D2FP_PAR_MODSTR, value); } #endif @@ -257,12 +257,7 @@ public void SetBooleanProperty(int property, bool value) var status = NativeMethods.d2fobgo_GetObjProp(NdapiContext.GetContext(), _handle, property, out var handle); Ensure.Success(status); - if (handle.IsInvalid) - { - return null; - } - - return Create(handle); + return handle.IsInvalid ? null : Create(handle); } /// @@ -406,7 +401,7 @@ public bool HasInheritedProperty(int property) /// The property state. public PropertyState GetPropertyState(int property) { - var state = PropertyState.Unknown; + PropertyState state; if (HasInheritedProperty(property)) { state = PropertyState.Inherited; @@ -436,16 +431,6 @@ public void Reattach() Ensure.Success(status); } - /// - /// Change the subclassing parent of an object to the parent object. - /// This will cause the property values to be overriden for all properties which are defined on the parent object. - /// - /// Object to subclass. - public void Subclass(NdapiObject parent) - { - Subclass(parent, false); - } - /// /// Change the subclassing parent of an object to the parent object. /// This will cause the property values to be overriden for all properties which are defined on the parent object. @@ -453,7 +438,7 @@ public void Subclass(NdapiObject parent) /// Object to subclass. /// Indicates whether the system should refer to the parent object's module by filename or by path+filename. /// The recommended choice is false in most cases. - public void Subclass(NdapiObject parent, bool keepPath) + public void Subclass(NdapiObject parent, bool keepPath = false) { var status = NativeMethods.d2fobsc_SubClass(NdapiContext.GetContext(), _handle, parent._handle, keepPath); Ensure.Success(status); @@ -487,7 +472,7 @@ public virtual void Destroy() var instance = Activator.CreateInstance(objectType, BindingFlags.NonPublic | BindingFlags.Instance, null, - new[] { handle }, + new object[] { handle }, null); return (T)instance; } @@ -499,19 +484,21 @@ public virtual void Destroy() public override string ToString() => Name; #region IDisposable Support - private bool disposedValue = false; // To detect redundant calls + private bool disposedValue; // To detect redundant calls protected virtual void Dispose(bool disposing) { - if (!disposedValue) + if (disposedValue) { - if (disposing) - { - _handle?.Dispose(); - } + return; + } - disposedValue = true; + if (disposing) + { + _handle?.Dispose(); } + + disposedValue = true; } public void Dispose() @@ -526,7 +513,7 @@ public void Dispose() /// public abstract class NdapiObject : NdapiObject where T : NdapiObject { - internal NdapiObject() : base() { } + internal NdapiObject() { } internal NdapiObject(ObjectType type) : base(type) { } internal NdapiObject(string name, ObjectType type, NdapiObject parent = null) : base(name, type, parent) { } internal NdapiObject(ObjectSafeHandle handle) : base(handle) { } @@ -548,18 +535,7 @@ public abstract class NdapiObject : NdapiObject where T : NdapiObject /// Name of the new object. public T Clone(string newName) { - return Clone(newName, null, true); - } - - /// - /// Creates a new object with the given name and owner. - /// The new object is an exact copy of the original object, with all the same property values. - /// - /// Name of the new object. - /// /// New owner of the object. If null, the object will be owned by the same parent of the current object. - public T Clone(string newName, NdapiObject newOwner) - { - return Clone(newName, newOwner, true); + return Clone(newName, null); } /// @@ -570,7 +546,7 @@ public T Clone(string newName, NdapiObject newOwner) /// New owner of the object. If null, the object will be owned by the same parent of the current object. /// If false, the sublassing info is discarded and the inherited properties are flattened into local values in the new object. /// The new object. - public T Clone(string newName, NdapiObject newOwner, bool keepSubclassingInfo) + public T Clone(string newName, NdapiObject newOwner, bool keepSubclassingInfo = true) { var parentHandle = newOwner?._handle ?? Owner._handle; diff --git a/Ndapi/NdapiObjectLibraryObjectList.cs b/Ndapi/NdapiObjectLibraryObjectList.cs index 068d986..709c76a 100644 --- a/Ndapi/NdapiObjectLibraryObjectList.cs +++ b/Ndapi/NdapiObjectLibraryObjectList.cs @@ -6,19 +6,18 @@ namespace Ndapi public class NdapiObjectLibraryObjectList : IEnumerable { private readonly ObjectLibrary _objectLibrary; - private readonly int _count; public NdapiObject this[int index] => _objectLibrary.GetObjectByPosition(index); internal NdapiObjectLibraryObjectList(ObjectLibrary objectLibrary) { _objectLibrary = objectLibrary; - _count = _objectLibrary.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT); + Count = _objectLibrary.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT); } - public int Count => _count; + public int Count { get; } - public bool Any() => _count > 1; + public bool Any() => Count > 1; public IEnumerator GetEnumerator() => new Enumerator(this); @@ -28,7 +27,6 @@ public sealed class Enumerator : IEnumerator { private readonly NdapiObjectLibraryObjectList _ndapiLibraryObjectList; private int _position; - private NdapiObject _current; internal Enumerator(NdapiObjectLibraryObjectList ndapiLibraryObjectList) { @@ -43,14 +41,14 @@ public bool MoveNext() return false; } - _current = _ndapiLibraryObjectList._objectLibrary.GetObjectByPosition(_position); + Current = _ndapiLibraryObjectList._objectLibrary.GetObjectByPosition(_position); _position++; return true; } public void Reset() => _position = 1; - public NdapiObject Current => _current; + public NdapiObject Current { get; private set; } object IEnumerator.Current => Current; diff --git a/Ndapi/NdapiObjectLibraryTabObjectsList.cs b/Ndapi/NdapiObjectLibraryTabObjectsList.cs index f1e8927..b68be74 100644 --- a/Ndapi/NdapiObjectLibraryTabObjectsList.cs +++ b/Ndapi/NdapiObjectLibraryTabObjectsList.cs @@ -6,19 +6,18 @@ namespace Ndapi public class NdapiObjectLibraryTabObjectsList : IEnumerable { private readonly ObjectLibraryTab _objectLibraryTab; - private readonly int _count; public NdapiObject this[int index] => _objectLibraryTab.GetObjectByPosition(index); internal NdapiObjectLibraryTabObjectsList(ObjectLibraryTab objectLibraryTab) { _objectLibraryTab = objectLibraryTab; - _count = _objectLibraryTab.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT); + Count = _objectLibraryTab.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT); } - public int Count => _count; + public int Count { get; } - public bool Any() => _count > 1; + public bool Any() => Count > 1; public IEnumerator GetEnumerator() => new Enumerator(this); @@ -28,7 +27,6 @@ public sealed class Enumerator : IEnumerator { private readonly NdapiObjectLibraryTabObjectsList _list; private int _position; - private NdapiObject _current; internal Enumerator(NdapiObjectLibraryTabObjectsList list) { @@ -43,14 +41,14 @@ public bool MoveNext() return false; } - _current = _list._objectLibraryTab.GetObjectByPosition(_position); + Current = _list._objectLibraryTab.GetObjectByPosition(_position); _position++; return true; } public void Reset() => _position = 1; - public NdapiObject Current => _current; + public NdapiObject Current { get; private set; } object IEnumerator.Current => Current; diff --git a/Ndapi/NdapiObjectList.cs b/Ndapi/NdapiObjectList.cs index 0351c95..2db3f1a 100644 --- a/Ndapi/NdapiObjectList.cs +++ b/Ndapi/NdapiObjectList.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using Ndapi.Core; -using Ndapi.Core.Handles; using Ndapi.Enums; using Ndapi.Metadata; @@ -18,16 +17,18 @@ public class NdapiObjectList : IEnumerable where T : NdapiObject { get { - var enumerator = GetEnumerator(); - for (var i = 0; i <= index; i++) + using (var enumerator = GetEnumerator()) { - if (!enumerator.MoveNext()) + for (var i = 0; i <= index; i++) { - throw new ArgumentOutOfRangeException(nameof(index)); + if (!enumerator.MoveNext()) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } } - } - return enumerator.Current; + return enumerator.Current; + } } } @@ -43,15 +44,16 @@ internal NdapiObjectList(NdapiObject ndapiObject, int property) public void RemoveAll() { - var enumerator = GetEnumerator(); - - var value = enumerator.Current; - while (value != null) + using (var enumerator = GetEnumerator()) { - enumerator.MoveNext(); - var nextValue = enumerator.Current; - value.Destroy(); - value = nextValue; + var value = enumerator.Current; + while (value != null) + { + enumerator.MoveNext(); + var nextValue = enumerator.Current; + value.Destroy(); + value = nextValue; + } } } @@ -86,14 +88,9 @@ internal Enumerator(NdapiObjectList ndapiObjectList) public bool MoveNext() { - if (_current == null) - { - _current = _objectList._ndapiObject.GetObjectProperty(_objectList._property); - } - else - { - _current = _current.GetNext(); - } + _current = _current == null ? + _objectList._ndapiObject.GetObjectProperty(_objectList._property) : + _current.GetNext(); return _current != null; } diff --git a/Ndapi/ObjectLibrary.cs b/Ndapi/ObjectLibrary.cs index b6cc413..18bacd6 100644 --- a/Ndapi/ObjectLibrary.cs +++ b/Ndapi/ObjectLibrary.cs @@ -44,12 +44,7 @@ public NdapiObject GetObjectByPosition(int position) var status = NativeMethods.d2folbf2_Findobjbypos(NdapiContext.GetContext(), _handle, position, out var obj); Ensure.Success(status); - if (obj.IsInvalid) - { - return null; - } - - return Create(obj); + return obj.IsInvalid ? null : Create(obj); } /// @@ -117,23 +112,13 @@ public override void CompileObjects() throw new NotSupportedException("Object library module does not support compilation"); } - /// - /// Gets the version of the last Form Builder that loaded the library. - /// - /// Object library location (.olb file) - /// The Form Builder version - public static int GetFileVersion(string file) - { - return GetFileVersion(file, false); - } - /// /// Gets the version of the last Form Builder that loaded the library. /// /// Object library location (.olb file) /// Library should be loaded from database. /// The Form Builder version - public static int GetFileVersion(string file, bool loadFromDb) + public static int GetFileVersion(string file, bool loadFromDb = false) { #if FORMS_6 var status = NativeMethods.d2folbfv_FileVersion(NdapiContext.GetContext(), file, loadFromDb, out var version); diff --git a/Ndapi/ObjectLibraryTab.cs b/Ndapi/ObjectLibraryTab.cs index 857afaa..6c1ed78 100644 --- a/Ndapi/ObjectLibraryTab.cs +++ b/Ndapi/ObjectLibraryTab.cs @@ -63,23 +63,7 @@ public NdapiObject GetObjectByPosition(int position) var status = NativeMethods.d2foltf2_Findobjbypos(NdapiContext.GetContext(), _handle, position, out var obj); Ensure.Success(status); - if (obj.IsInvalid) - { - return null; - } - - return Create(obj); - } - - /// - /// Add an object to the object library tab. When adding an object, a copy is made and it is this copy that is added to the library. - /// - /// Object type. - /// Object to add to library tab. - /// A copy of the original object. - public T AddObject(T obj) where T : NdapiObject - { - return AddObject(obj, false); + return obj.IsInvalid ? null : Create(obj); } /// @@ -89,7 +73,7 @@ public NdapiObject GetObjectByPosition(int position) /// Object to add to library tab. /// Should replace the existing object. /// A copy of the original object. - public T AddObject(T obj, bool replace) where T : NdapiObject + public T AddObject(T obj, bool replace = false) where T : NdapiObject { var status = NativeMethods.d2folbao_AddObj(NdapiContext.GetContext(), ObjectLibrary._handle, _handle, obj._handle, out var handle, replace); Ensure.Success(status); diff --git a/Ndapi/RecordGroupColumn.cs b/Ndapi/RecordGroupColumn.cs index d1e8d4e..c2b6f11 100644 --- a/Ndapi/RecordGroupColumn.cs +++ b/Ndapi/RecordGroupColumn.cs @@ -64,23 +64,13 @@ public IEnumerable Values } } - /// - /// Inserts a value in the specified index. - /// - /// The one-based index at which value should be inserted. - /// Value to insert. - public void AddValueAt(int index, string value) - { - AddValueAt(index, value, null); - } - /// /// Inserts a value in the specified index. /// /// The one-based index at which value should be inserted. /// Value to insert. /// Format mask to format the data. - public void AddValueAt(int index, string value, string formatMask) + public void AddValueAt(int index, string value, string formatMask = null) { var status = NativeMethods.d2frcsir_InsertRow_Int(NdapiContext.GetContext(), _handle, index, value, formatMask); Ensure.Success(status);