Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to have a constant string throughout my entire Silverlight C# project?

I want to have a constant string kURL = "http://www.myurl.com/"; throughout my entire project. What's the proper way to do this in a Windows Phone 7 app?

Where would I put this code and what should the proper syntax be?

like image 439
Ethan Allen Avatar asked Dec 06 '22 16:12

Ethan Allen


2 Answers

Create a .Common project for things that you may need to access from all of your projects in your solution (like constants, extension methods, utils etc.), in there simply create Constants class with any Constants that you may need.

Like this:

public static class Constants
{
    #region Nested type: Urls

    public static class Urls
    {
        public static readonly string MyUrl = "http://blablabla.com";
    }

    #endregion

}

Usage would be:

Constants.Urls.MyUrl

Good luck.

Edit Note: Changed to const as per Gabes suggestion

Edit Note2: Changed to static readonly per lukas suggestion

like image 131
shenku Avatar answered Dec 09 '22 05:12

shenku


Here is a simple tutorial on creating Global Consts in C#. I've used this for .NET, but not Windows Phone. I would assume the same conventions are followed.

http://www.dotnetperls.com/global-variable

like image 31
evanmcdonnal Avatar answered Dec 09 '22 04:12

evanmcdonnal