Project Wide Constant?

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

I want to create a constant (string) value for a project and use it to
assign values to Attributes that decorate certain WinForms. Then I
will only need to change certain annotations in one place.
How can I do that?


Thanks.
 
localhost said:
I want to create a constant (string) value for a project and use it to
assign values to Attributes that decorate certain WinForms. Then I
will only need to change certain annotations in one place.

\\\
Public Module Globals
Public Const ProjectName As String = "Hello World"
End Module
///
 
Hi primpilus,

Based on my understanding, you want to create a one copy constant to use in
your winform application.

There are a couple of ways to achieve this, for example:
1. In your namespace, you may create a toolhelper class, then place the
constant string in this toolhelper class as public static property or
field, then we can use it freely with only one copy. Like this:
public class toolhelper
{
public static string global_str="global strings";
}
Also, we can place some global methods and functions in this class for
project wide usage.

2. For configurable const value, we may store it in app.config file, then
use System.Configuration.AppSettingsReader to read the values from the
config file. This is usually used for storing application wide settings,
such as database related connection string etc.., for more information,
please refer to:
"Persisting Application Settings in the .NET Framework"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/persistappsettnet.asp
===============================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

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.
 
That is not exactly what I am asking. What you are showing me is for
runtime work. I want to make a design-time constant. I if was doing
C++, I could DEFINE or put a set of DEFINEs in a .h file, and at
compile-time the string I want would replace the constants in the
project. I need to do the same thing in a Visual Studio .NET 2003 C#
project.

I am setting a string property on attributes, which cannot by
dynamically set at runtime. The attribute decorates several classes
and I need to make the change in several places at the same time.


Thanks.
 
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.
 
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)
{

}
}




}
 
Hello localhost,

You should be able to declare your values as const and then add them to attributes...

class Foo
{
public const string MyConstant = "MyConstant";
}

[DefaultProperty(Foo.MyConstant)]
class Bar : Form
{
...
}
 
Hello localhost,

Instead of static declare it as const...

--
Matt Berther
http://www.mattberther.com
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.
 
Hi,

Just as Matt pointed out, in attrubute parameter, we can only use const
expression, not static variable. So in my original reply, I have suggested
you that "(You may modify the "static" keyword as "const")"

Once modifying static as const, it should work for you.

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.
 
Back
Top