Defining Public strings Visual Web Developer  
Author Message
YachtKeepers





PostPosted: Visual C# Language, Defining Public strings Visual Web Developer Top

I am using Visual Web Developer, it's a great product, but I'm stuck,

I have done a lot of research on trying to define public strings,

Examples I find are

public class SData

{

public string sdata

}

This is quite logical, but the problem is using them in the code generated by Visual Web Developer

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

// I would like to define some public strings of data that

// could be used thru out, but am at a lose as to where to

// Place them.

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

}

}

Thanx for any assistance, I know it's something simple.




Visual C#19  
 
 
Paul Louth





PostPosted: Visual C# Language, Defining Public strings Visual Web Developer Top

You could create a static class

public static class MyStrings
{
    public static string HelloWorld = "Hello World";
    public static string HelloEverybody = "Hello Everybody";
}

Then access them from anywhere using this:

string myString = MyStrings.HelloWorld;

or you could put the strings into your app.config file and then use the ConfigurationManager to access them (make sure you add a referrence to System.Configuration).

app.config:

<configuration>
 <appSettings>
  <add key="HelloWorld" value="Hello World!"/>   
 </appSettings>
</configuration>

code:

string myString = ConfigurationManager.AppSettings["HelloWorld"];

or you could open up the Settings in your Project properties.  Enter the string there.  And access it thus:

string myString = Settings.Default.HelloWorld;

 


 
 
YachtKeepers





PostPosted: Visual C# Language, Defining Public strings Visual Web Developer Top

Thank You,

Works great.



 
 
Paul Louth





PostPosted: Visual C# Language, Defining Public strings Visual Web Developer Top

Cool, glad to help  :)