CodeDOM, declaring and setting a constant private field

G

Guest

Using CodeDOM, is there a way to declare and set a private field.

Something like:



private const int tableConstant = 10;

//Provide the type and variable name
CodeMemberField mymemberfield = new CodeMemberField(fieldType,fieldName);
mymemberfield.Attributes = MemberAttributes.Private;
//Add the member to the class
newClass.Members.Add(mymemberfield);

Thanks,

Dan H.
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

Once you create the CodeMemberField, you can set the InitExpression
property to the expression you want to initialize the field.

Hope this helps.
 
G

Guest

HI Dan

Try this

CodeMemberField cmf = new CodeMemberField(typeof(System.Int32
),"tableConstant") ;
cmf.Attributes = MemberAttributes.Const | MemberAttributes.Private ;
cmf.InitExpression = new CodePrimitiveExpression(10);

hope it helps

regards

Ronnie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top