c# - Function as variable throwing error NotImplementedException unhandled -
okay have added function c# script fetch ip address , send output string in variable heres source
using system; using system.collections.generic; using system.linq; using system.text; using system.collections.specialized; using system.net; using system.io; namespace consoleapplication1 { class program { static void main(string[] args) { string url = "http://localhost/test2.php"; webclient webclient = new webclient(); namevaluecollection formdata = new namevaluecollection(); formdata["var1"] = formdata["var1"] = string.format("machinename: {0}", system.environment.machinename); formdata["var2"] = stringgetpublicipaddress(); formdata["var3"] = "dgpass"; byte[] responsebytes = webclient.uploadvalues(url, "post", formdata); string responsefromserver = encoding.utf8.getstring(responsebytes); console.writeline(responsefromserver); webclient.dispose(); system.threading.thread.sleep(5000); } private static string stringgetpublicipaddress() { throw new notimplementedexception(); } private string getpublicipaddress() { var request = (httpwebrequest)webrequest.create("http://ifconfig.me"); request.useragent = "curl"; // simulate curl linux command string publicipaddress; request.method = "get"; using (webresponse response = request.getresponse()) { using (var reader = new streamreader(response.getresponsestream())) { publicipaddress = reader.readtoend(); } } return publicipaddress.replace("\n", ""); } } }
basically have created function
private static string stringgetpublicipaddress()
and sending variable
formdata["var2"] = stringgetpublicipaddress();
i getting error
throw new notimplementedexception(); === notimplementedexception unhandled
you... didn't implement method. have this:
private static string stringgetpublicipaddress() { throw new notimplementedexception(); }
so of course time call method, it's going throw exception. looks did implement method want here, though:
private string getpublicipaddress() { // rest of code }
maybe result of copy/paste error? try getting rid of small method throws exception , changing implemented method static:
private static string getpublicipaddress() { // rest of code }
then update anywhere call this:
stringgetpublicipaddress();
to this:
getpublicipaddress();
this looks copy/paste errors gone wrong in strange ways. or perhaps you're struggling difference between static , instance methods? maybe implemented method, compiler suggested needed static method? there's lot read static vs. instance methods/members won't here. it's significant concept in object-oriented programming.
in particular case, since you're in context of static method in main
, call main
on class (the program
class) needs static, unless create instance of program
this:
var program = new program();
this allow call instance methods on program
:
program.somenonstaticmethod();
but small application such this, isn't necessary.
Comments
Post a Comment