Print members of a class

P

PiotrKolodziej

Hi
A have a class containing elements of string type. I need to print them
using 'for' loop.
How to do this. I spent few hours trying to do this from the other approach.
Thanks for any help.

Here's the class:

public static class names
{
public const string VolumeUp = "Volume Up";

public const string VolumeDn = "Volume Down";

public const string Mute = "Mute";

public const string Custom = "Browse Program";

public const string CloseWindow = "Close Window";

public const string WinampPause = "Winamp Pause";

public const string WinampPlay = "Winamp Play";

public const string WinampStop = "Winamp Stop";

public const string WinampFFWD = "Winamp FFWD 5 sec";

public const string WinampREW = "Winamp REW 5 sec";

public const string WinampVolumeUp = "Winamp Volume Up";

public const string WinampVolumeDn = "Winamp Volume Down";

public const string WinampNext = "Winamp Next Track";

public const string WinampPrev = "Winamp Previous Track";
}
 
S

Simon Tamman

Have you tried using reflection?

Something like:

names obj;

foreach(PropertyInfo pi in obj.GetType().GetProperties())
{
string theValue = pi.GetValue(obj);
}

It'd be different code for constants although i'm sure you can get them out.
The code above is probably not correct but it gives you the idea of roughly
what you are looking for.

Additionally the class doesn't need to be static if it's members are just
consts.
 
N

Nicholas Paldino [.NET/C# MVP]

It's almost right. You will want to get the fields, not properties.
 
N

Nicholas Paldino [.NET/C# MVP]

I am curious, why do you not have this stuff in a resource file? It
makes more sense that way, since it seems like these are display strings
(and this doesn't account for changes in the display string, as well as
internationalization issues).
 
P

PiotrKolodziej

I am curious, why do you not have this stuff in a resource file? It
makes more sense that way, since it seems like these are display strings
(and this doesn't account for changes in the display string, as well as
internationalization issues).

I have tried to do my task this way. The only problem is that i need to get
all string values from resource at one time. OF course when i need to access
one string value by it's name and i have this name its not problem while one
can use resourcemenager.
I would be grateful for any hint how to get all values at one time.
 
A

Andreas Mueller

PiotrKolodziej said:
I have tried to do my task this way. The only problem is that i need to get
all string values from resource at one time. OF course when i need to access
one string value by it's name and i have this name its not problem while one
can use resourcemenager.
I would be grateful for any hint how to get all values at one time.
With this piece of code you can iterate ofer all enries in a resource file:

Assembly executingAssembly = typeof(ProgramCs20).Assembly;

// name of the resource file wihout extension
string stringTableName = "Resource1";

// Build resource name: DefaultNameSpace.FileName.resources
// Note: if you have sub folders in your project, you have to
// insert the folder names, too:
// DefaultNameSpace.Folder.SubFolder.FileName.resources
string resname =
string.Format("ConsoleApplication7.{0}.resources", stringTableName);

using (ResourceSet resSet =
new ResourceSet(executingAssembly.GetManifestResourceStream(resname)))
{
foreach (DictionaryEntry entry in resSet)
Console.WriteLine(string.Format("Key: {0}, Value: {1}",
entry.Key, entry.Value));
}

HTH,
Andy
 
P

PiotrKolodziej

Wow.
Is it really the simplest way?
It isn't hard but doesn't look friendly too
:)
Piotr Kolodziej
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top