Typesafe Interface

The typesafe interface is not as comfortable as the direct use of the square brackets Accessing entries via Brackets, but it gives you much more control.

Here is a list of all functions: Function Overview.

Get, Set, Add

Instead of using the square brackets you the Add, Set, and Get Methods. Let's have a look at each of them:

Add

public InISection InIReader.Add(string key)
public void InISection.Add(string key, string value)

Add will create a new Section or a new entry. If will throw an exception, if the key exists already, is empty or null. So check if the section does not exist and if you are not sure about your key call public static bool ValidateSection(string section) or `public static bool ValidateKey(string key). They return true if the key is a valid one.

If you want to add a section, to a existing one, you'll find more information here: Merge Files.

Set

public void InIReader.Set(string key, IEnumerable<KeyValuePair<string, string>> section, bool replace = true)
public void InISection.Set(string key, string value)

Setting a section or keym which does not exist will throw an exception. As you see, a section consists basically of a dictionary, which is why the function will take a KeyValuePair instead of a InISection. You can call the function with a section, without a explicit cast.

With the replace flag, you choose if the section shall be completly replaced or if just entries with the same name will be replaced. No matter what you choose, the not existing key's in the new section will be added.

Get

The Get-Method is defined as the example below shows. Called on the InIReader the Method will return the section, on a Section, it will return the entry's value.

public InISection InIReader.Get(string section)
public string InISection.Get(string key)

Calling the "Get" with a empty, null string or a key which does not exist, will cause an exception. To prevent that, check if the key does exist with "ContainsKey", or use the "TryGet".

TryGet

To make reading easier you got the 'TryGet method, which are a combination of ContainsKey and Get.

public bool InIReader.TryGet(string key, out InISection section)
public bool InISection.TryGet(string key, out string value)

Those methods, will try to get the value associated to the given key. If their return values are true the key exists and connected value was written into the with out marked parameter. Otherwise the parameter are set to their default values and the return value is false

Where is the typesafe thing?

For each method mentioned above, except for those who return a InISection, exist a variety of overloads. Here is a complete list, with the get method, but the same overloads exist also for the set, add and TryGet methods.

GetAsBoolean(string key):bool
GetAsByte(string key):byte
GetAsSByte(string key):sbyte
GetAsChar(string key):char
GetAsDecimal(string key):decimal
GetAsDouble(string key):double
GetAsSingle(string key):float
GetAsInt16(string key):short
GetAsUInt16(string key):ushort
GetAsInt32(string key):int
GetAsUInt32(string key):uint
GetAsInt64(string key):long
GetAsUInt64(string key):ulong
GetAsString(string key):string
GetAsEnum<T>(string key):T

// alternative names
GetAsFloat(string key):float
GetAsShort(string key):short
GetAsUShort(string key):ushort
GetAsInt(string key):int
GetAsUInt(string key):uint
GetAsLong(string key):long
GetAsULong(string key):ulong

Exceptions
This functions will throw the same exception as there base methods, plus the parse exceptions. The TryGet will not even throw the parse exceptions. Please note, that the GetAsEnum will only work with enums and it will thank you with an exception for trying any other type.

Alternative names
They are just alternative names and call the existing functions.
  • Float => Single
  • Int16, UInt16 => Short, UShort
  • Int32, UInt32 => Int, UInt
  • Int64, UInt64 => Long, ULong

Direct Functions

You have also the possibillity to set the entries, directly from the InIReader class using methods like this one:

public void InIReader.Add(string section, string key, string value)

They work exaclity the same way, as it would work with calling first the section and then the entry. Of course you also have all the overloads mentioned above.

Last edited Dec 13, 2012 at 5:58 PM by IqonX, version 2

Comments

No comments yet.