c# - Invisible constructors, despite use of public access modifier? -
i wrote class multiple overloads, when attempt utilise them outside class, default constructor can used. otherwise receive error stating no such constructor exists.
public module()//default module class { code = "undefined"; title = "undefined"; credits = 0; mark = 0; examweighting = 0; exammark = 0; courseworknumber = 1; term = "ay"; coursework1name = "undefined"; coursework1type = "undefined"; coursework1weight = 1; coursework1mark = 0; } public module(string code, string title, int credits, string moduleterm, double exweight, double exmark, string cw1name, string cw1type, double cw1weight, double cw1mark) { code = code; title = title; credits= credits; mark = 0; examweighting = exweight; exammark = exmark; courseworkweight = 1.0 - exweight; courseworknumber = 1; term = moduleterm; coursework1name = cw1name; coursework1type = cw1type; coursework1weight = cw1weight; coursework1mark = cw1mark; }
i ensured had correct number , type of parameters, unless of course i'm missing something.
string code = temparray[0]; string title = temparray[1]; int credits = convert.toint16(temparray[2]); string moduleterm = temparray[3]; double exweight = convert.todouble(temparray[4]); double exmark = convert.todouble(temparray[5]); string cw1name = temparray[10]; string cw1type = temparray[11]; double cw1weight = convert.todouble(temparray[12]); double cw1mark = convert.todouble(temparray[13]); // (string code, string title, int credits, string moduleterm, double exweight, double exmark, string cw1name, string cw1type, double cw1weight, double cw1mark) tempmodule = new module(code, title, credits, moduleterm, exweight, exmark, cw1name, cw1type, cw1weight, cw1mark);
having parameter list mile long anti-pattern. you've discovered why case messing call, because either you've:
- got wrong number of parameters
or
- got incorrect types in parameters you're using
at call site.
if inevitable need large number of parameters, might consider using options class encapsulates these parameters. allows sensible defaults too. so
public class moduleoptions { public moduleoptions() { //supply sensible defaults here code = "not set"; title = "not set"; //etc } public string code{get;set;} public string title{get;set;} //etc... }
then
public class module { public module(moduleoptions options) { this.code = options.code; //etc... } }
so call site looks like
var opts = new moduleoptions { code = "1234", //etc }; var module = new module(opts);
now it's lot easier see what's happening , you're far less screw call.