0

I have an object to manage ini files.

I concept this object to bee used in two ways: static and non-static. So I can call IniFile.Read directly when I just need a value or I can instantiate IniFile Object and do some operations. All non-static functions call static equivalent one, myIniFile.Read(sectionName, value, defaultValue) calls IniFile.Read(iniPath, sectionName, value, defaultValue). Read() function has default value for the last parameter.

My problem is when I call IniFile.Read() function, compiler doesn't know if I call the static function or the other one. Is there a way to resolve this problem ?

public static string ReadValue(string filePath, string section, string key, string defaultValue="")
public string ReadValue(string Section, string Key, string defaultValue="")
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Ok, i found the matter [here](https://stackoverflow.com/questions/160118/static-and-instance-methods-with-the-same-name) Thanks you all – olivier sow Nov 22 '18 at 10:58

3 Answers3

2

To call static function :

ClassName.Function();

for unstatic:

ClassName class_name = new ClassName();

class_name.Function();
Skogandr
  • 133
  • 6
  • I know how static/non-static work, if i call IniFile.ReadValue(myPath, mySection, myValue); it should call the static function, but compilator give me the error "An object reference is required for the non-static method ..." – olivier sow Nov 22 '18 at 10:48
  • As @Skogandr says, you would need to instantiate the class, and call it from the instantiated variable. For example, var iniFile = new InitFile(); iniFile.ReadValue(... – Paul Michaels Nov 22 '18 at 10:53
  • No because in the case i want to call the static function – olivier sow Nov 22 '18 at 10:55
  • So in your case you call IniFileClass.ReadValue(); – Skogandr Nov 22 '18 at 10:57
1

An alternative to disambiguate - given that you, by definition, must have a different signature, is to use the named parameters; for example:

iniFile.ReadValue(Section: "test", Key: "key");
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
0

If you explicitly prefix the method with the type name, it should invoke the static method; for example:

public void InstanceCallSite()
{
    ReadValue("a", "b", "c");
    // or in the general case: someInstance.ReadValue("a", "b", "c");
    Foo.ReadValue("a", "b", "c");
}

public static void StaticCallSite()
{
    ReadValue("a", "b", "c");
}

public static string ReadValue(string filePath, string section, string key, string defaultValue = "")
{
    Console.WriteLine("static");
    return "";
}
public string ReadValue(string Section, string Key, string defaultValue = "")
{
    Console.WriteLine("instance");
    return "";
}

The InstanceCallSite usage outputs:

instance
static
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • yeah, it should, but compilator tries to use the bad method. And i get compilation error in the other way when i call myIniFile.WriteValue(INI_SECTION_NAME, KEYNAME, myValue, myDefaultValue); the compilator thinks i use the static function :( – olivier sow Nov 22 '18 at 10:54
  • @oliviersow can you show a minimal example that actually shows that exception? also: what compiler version are you using? the overload resolution code has been improved at multiple points – Marc Gravell Nov 22 '18 at 12:00