[dba-VB] Error when passing controls to the constructor of a class

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Fri Oct 9 17:00:05 CDT 2009


Hi John,

You can put your initialization code into your form constructor or Form_Load
event processing method.

BTW, I see you still use Hungarian notation/LRNC - try to forget it ASAP
when programming on C# - and you'll feel a great relieve.
Guaranteed/tested/proven by many developers. There are .NET natural naming
conventions recommended by MS. Try to find and use them. Here is one source:

http://www.irritatedvowel.com/Programming/Standards.aspx

Here is a VS plug-in to help you get accustomed to the new naming
conventions:

http://www.codeplex.com/StyleCopForReSharper

Although I haven't used it yet.

--Shamil



-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Friday, October 09, 2009 8:25 PM
To: VBA
Subject: [dba-VB] Error when passing controls to the constructor of a class

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
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4494 (20091009) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru


 

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4494 (20091009) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru
 




More information about the dba-VB mailing list