.NET Named String Literals

I must be missing something really obvious here; I want to name a set of string literals so that I can refer to them by name subsequently. In Ye Olde Days of C it would probably have been with a 

#define LINK_BASE_URL “http://www.kfs.org/”

And then in C++

namespace Links
  static const char* baseUrl = “http://www.kfs.org/” ;
Right now I’m experimenting with Managed C++ (yes, that is the right choice for this scenario) but I can’t seem to find a painless way of simply naming a string literal. I don’t want the same string constant repeated over and over.


In C#

private static const string strSomething = “lalala”


static readonly string strSomething = “lalala”

Can’t something like this be done in managed C++? Upper case at will.

I’m not sure where your going with this….

you could just

#pragma once

#pragma managed

using namespace System;

namespace Links
static const System::String ^baseUrl = “http://myurl”;

The only issue is this only works with managed code. Just include this snipet in a header file for all your constants. Then you just reference as Links::baseUrl


namespace Links {
public static string baseUrl=”http://www.kfsone.com/”;


public string getFullUrl() {
return Links.baseUrl + “/stuff.html”;

Unfortunately, “static const String^ baseUrl = “http://…”;” inside a namespace generates an error about static/global variables not being allowed to be of type “String^”. Otherwise I wouldn’t be asking :)

O. i .c

try this then: Wrap it in a singleton object, that way you can access it as mykeys::baseUrl

(shameless rip from elsewhere)

ref class MySettings
	static property MySettings^ Instance
		MySettings^ get()
			return instance;

	static void LoadSetting()
		instance = gcnew MySettings();
		// read registry and set properties of instance

	static MySettings^ instance;

Erh, ok? You still need a way to name a string literal inside your singleton wrapper. If you tell me how you’re going to do that in a one liner that’s as convenient as static const char* baseUrl = “http://”; and that doesn’t create any objects then we can forget the whole singleton thing and just use the string-literal-naming technique you were going to use inside MySettings.

For now I’ll stick with

static const System::String^ baseUrl() { return “http://…” ; }

The code produced by the following three examples seems to be the same:

using namespace System ;
namespace Foo {
String^ AddStrings(String^ left, String^ right) { return left + right ; }

// Example 1: With /clr not /clr:pure or /clr:safe
static const char* c_baseUrl = “http://www.kfs.org/” ;
String^ Foo() { return AddStrings(c_baseUrl, “foo.html”) ; }

// Example 2: A workable but annoying way of naming literals
static const String^ mc_baseUrl() { return “http://www.kfs.org/” ; }
String^ Bar() { return AddStrings(mc_baseUrl(), “bar.html” ; }

// Example 3: Literals.
String^ Baz() { return AddStrings(“http://www.kfs.org/”, “baz.html”) ; }
} ;

My knowledge about C++/CLI ist not very deep, but if I remember right, this could work:


using namespace System;

namespace test
gcroot const foo = "Hello World";


I hate that commet function. Somebody once need to tell me, how I display code in the comments …
Well nopaste FTW:


Olly, noticed you had an abysmal looking tag cloud on the site, have a look at http://www.roytanck.com/ for a better one :)

Ta muchly … By the way, to do the sourcecode thing … you do the following:

[ sourcecode language=’c++’ ] … code [ /sourcecode ]

Without the extra spaces inside the []s.

Can’t say for sure in C++, but in C#, static is redundant if you are doing const. The simplest form is:

public class Literals
public const string FOO = “Foo”;

So in general code you would do:

string myVar = Literals.FOO;

Ah, now I see what you are getting at. I don’t think, in the .NET world, you can have a variable outside of a class even in C++. So what you want (in C# form) is this:

public class Links
///Prevent instantiation
private Links()

public const string BASE_URL = “http://www.kfs.org/”;

which would be referenced as so:

public void SomeMethod()

C++/CLI actually allows you to have non-encapsulated globals (it just puts them in a secret “global” namespace).

But yep, that’s what I was trying to do. Although the scary thing is that the C# construct appears to actually cause each instance of the class to have its own reference to the string in memory which increases the size of the instanced members of the class for every named string literal. Kinda bloaty. But I might be wrong on this – having to spend more time in C++/CLI than C# which is annoying.

Use kw “static” to make it/them class members instead of instance members -> no temporary kicks in.
Using kw “ref” class marks it as a managed class, let it live on the “heap” and avoids copying.

Comes as near to a global named set of constant as possible (to me). I am not familiar with managed C++, so you might want to check it.

#include “stdafx.h”
using namespace System;

ref class Links
static System::String^ baseUrl = “http://www.wwiionline.com”;
static System::String^ kfs = “http://www.kfs.org”;
int main(array ^args)
return 0;

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

You may use these HTML tags and attributes:

<a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: