jwcolby
jwcolby at colbyconsulting.com
Fri Oct 9 11:24:52 CDT 2009
I am very much a novice at C#. For my homework project I am trying to use classes (next semester stuff) and I have some processing that needs to manipulate controls back on the form. I have three instances of a Grades (plural) class which has to display the current grade, but only when a grade is added, deleted or modified in that specific instance. Additionally I have a set of controls on the form that are filled with data about all of the grades so the next level class (supervisor to clsGrades) has to update those controls. So I'm thinking I would create a clsGradesDisplay, instantiate two of this class up in the form, and pass in the four text boxes that have to be manipulated. Then I can pass that class off to the child classes so that they can manipulate the text boxes as they need to. This makes sense because those child classes actually contain the data that has to be displayed. ATM I simply pass the data back up the chain and have the form do the update, but it would be cleaner and less confusing to have the class that contains the data do the updates directly. My problem is that I get a compile error when I try to pass in the text boxes to the constructor of this clsGradesDisplay. The class constructor looks like this: class clsGradeDisplay { public TextBox txtGradeCnt; public TextBox txtGradeSum; public TextBox txtGradeAvg; public TextBox txtGradeWeightedAvg; /*Constructor - initializes the pointers to the text boxes */ public clsGradeDisplay(TextBox ltxtGradeCnt, TextBox ltxtGradeSum, TextBox ltxtGradeAvg, TextBox ltxtGradeWeightedAvg) { txtGradeCnt = ltxtGradeCnt; txtGradeSum = ltxtGradeSum; txtGradeAvg = ltxtGradeAvg; txtGradeWeightedAvg = ltxtGradeWeightedAvg; } } Back in the header of the form I am tring to initialize the instances as follows: clsGradeDisplay mGDCurrent = new clsGradeDisplay( txtCntOfGrades, txtSumOfGrades, txtAvgGrade, txtWeightedScore); The error is: Error 4 A field initializer cannot reference the non-static field, method, or property 'Grade_Calculator.frmGradeCalculator.txtWeightedScore' It seems like it should work. I am allowed to initialize a class passing in values correct? Is there some special syntax for passing pointers to controls? What am I doing wrong? TIA, -- John W. Colby www.ColbyConsulting.com