473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Print members of a class

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";
}
Aug 27 '06 #1
8 2236
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.
"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:ea***************************@news.chello.pl. ..
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";
}


Aug 27 '06 #2
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:ea***************************@news.chello.pl. ..
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";
}

Aug 27 '06 #3
It's almost right. You will want to get the fields, not properties.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Simon Tamman" <i_**********************************@NOSPAMhotmai l.com>
wrote in message news:0t******************@newsfe7-win.ntli.net...
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.
"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:ea***************************@news.chello.pl. ..
>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";
}



Aug 27 '06 #4
Thank you both
Piotr Kolodziej
Aug 28 '06 #5
That's an interesing point.
Will try to do this both ways.
Aug 28 '06 #6
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.
Aug 28 '06 #7
PiotrKolodziej wrote:
> 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.

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.resourc es
string resname =
string.Format("ConsoleApplication7.{0}.resources", stringTableName);

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

HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Aug 28 '06 #8
Wow.
Is it really the simplest way?
It isn't hard but doesn't look friendly too
:)
Piotr Kolodziej
Aug 28 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
3
by: Garmt de Vries | last post by:
On the website of the Dutch Jules Verne Society (www.jules-verne.nl), we have several forms that visitors can use to order something, or to apply for membership. Of course, a form's primary purpose...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
2
by: RB0135 | last post by:
Hi All, Is this possible: 1) An application prints to a printer/print queue (mainly XP). 2) A vb.net application monitors the print queue (I know this can be done) 3) Retrieve the RAW...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.