Please try the code at the bottom of this message.
Line 77 gives an error:
[MyTestAttribute( ToolHelper.globalString )]
"Form1.cs(77): An attribute argument must be a constant expression,
typeof expression or array creation expression"
That is why your method does not work. I asked for a way to define a
constant value, ala DEFINE in C++, at design-time that will be
interpreted at compile-time. You gave a solution for "runtime
constants", which is not what I need.
The code at the bottom of this message illustrates why your solution
does not work.
Thanks.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestConst
{
[AttributeUsage(AttributeTargets.All , Inherited=true ,
AllowMultiple=false )]
public class MyTestAttribute : Attribute
{
private string _validData;
public MyTestAttribute( string validData )
{
this._validData = validData;
}
public string ValidData
{
get
{
return this._validData;
}
}
}
public class ToolHelper
{
public static string globalString = "Global String";
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnHelloWorld;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.btnHelloWorld = new System.Windows.Forms.Button();
this.SuspendLayout();
this.btnHelloWorld.Location = new System.Drawing.Point(72,
24);
this.btnHelloWorld.Name = "btnHelloWorld";
this.btnHelloWorld.TabIndex = 0;
this.btnHelloWorld.Text = "Hello World";
this.btnHelloWorld.Click += new
System.EventHandler(this.btnHelloWorld_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 268);
this.Controls.Add(this.btnHelloWorld);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
[MyTestAttribute( ToolHelper.globalString )]
private void btnHelloWorld_Click(object sender, System.EventArgs e)
{
}
}
}
Hi,
Thanks for your feedback!
Yes, there is #define preprocessor directive in C#, but it can only
be used for conditional compile. In C#, the standard way of doing
similiar as C++'s DEFINE macro is const value, that is the #1 way
option in my last reply.(You may modify the "static" keyword as
"const")
Why does not the const value way work for you? If you have any
concern,
please feel free to tell me, Thanks!
==========================================
Thank you for your patience and cooperation. If you have any
questions or
concerns, please feel free to post it in the group. I am standing by
to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.