c# force call static function with default value instead of non-static
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="")
c# object static
add a comment |
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="")
c# object static
Ok, i found the matter here Thanks you all
– olivier sow
Nov 22 '18 at 10:58
add a comment |
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="")
c# object static
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="")
c# object static
c# object static
edited Nov 22 '18 at 10:46
HimBromBeere
23.7k43259
23.7k43259
asked Nov 22 '18 at 10:35
olivier sowolivier sow
164
164
Ok, i found the matter here Thanks you all
– olivier sow
Nov 22 '18 at 10:58
add a comment |
Ok, i found the matter here Thanks you all
– olivier sow
Nov 22 '18 at 10:58
Ok, i found the matter here Thanks you all
– olivier sow
Nov 22 '18 at 10:58
Ok, i found the matter here Thanks you all
– olivier sow
Nov 22 '18 at 10:58
add a comment |
3 Answers
3
active
oldest
votes
To call static function :
ClassName.Function();
for unstatic:
ClassName class_name = new ClassName();
class_name.Function();
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
add a comment |
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");
great idea, nice one
– olivier sow
Nov 22 '18 at 13:16
add a comment |
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
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428995%2fc-sharp-force-call-static-function-with-default-value-instead-of-non-static%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
To call static function :
ClassName.Function();
for unstatic:
ClassName class_name = new ClassName();
class_name.Function();
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
add a comment |
To call static function :
ClassName.Function();
for unstatic:
ClassName class_name = new ClassName();
class_name.Function();
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
add a comment |
To call static function :
ClassName.Function();
for unstatic:
ClassName class_name = new ClassName();
class_name.Function();
To call static function :
ClassName.Function();
for unstatic:
ClassName class_name = new ClassName();
class_name.Function();
edited Nov 22 '18 at 10:55
answered Nov 22 '18 at 10:41
SkogandrSkogandr
1086
1086
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
add a comment |
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
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
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
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
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
So in your case you call IniFileClass.ReadValue();
– Skogandr
Nov 22 '18 at 10:57
add a comment |
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");
great idea, nice one
– olivier sow
Nov 22 '18 at 13:16
add a comment |
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");
great idea, nice one
– olivier sow
Nov 22 '18 at 13:16
add a comment |
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");
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");
answered Nov 22 '18 at 10:56


Paul MichaelsPaul Michaels
6,71530120219
6,71530120219
great idea, nice one
– olivier sow
Nov 22 '18 at 13:16
add a comment |
great idea, nice one
– olivier sow
Nov 22 '18 at 13:16
great idea, nice one
– olivier sow
Nov 22 '18 at 13:16
great idea, nice one
– olivier sow
Nov 22 '18 at 13:16
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
answered Nov 22 '18 at 10:42


Marc Gravell♦Marc Gravell
787k19521502556
787k19521502556
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
add a comment |
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
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
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
@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
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428995%2fc-sharp-force-call-static-function-with-default-value-instead-of-non-static%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Ok, i found the matter here Thanks you all
– olivier sow
Nov 22 '18 at 10:58