473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2258
Have you tried using reflection?

Something like:

names obj;

foreach(Propert yInfo pi in obj.GetType().G etProperties())
{
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.comwrot e in message
news:ea******** *************** ****@news.chell o.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
internationaliz ation issues).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"PiotrKolodziej " <pi************ *@gmail.comwrot e in message
news:ea******** *************** ****@news.chell o.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.co m

"Simon Tamman" <i_************ *************** *******@NOSPAMh otmail.com>
wrote in message news:0t******** **********@news fe7-win.ntli.net...
Have you tried using reflection?

Something like:

names obj;

foreach(Propert yInfo pi in obj.GetType().G etProperties())
{
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.comwrot e in message
news:ea******** *************** ****@news.chell o.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
internationaliz ation 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
international ization 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 executingAssemb ly = typeof(ProgramC s20).Assembly;

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

// Build resource name: DefaultNameSpac e.FileName.reso urces
// Note: if you have sub folders in your project, you have to
// insert the folder names, too:
// DefaultNameSpac e.Folder.SubFol der.FileName.re sources
string resname =
string.Format(" ConsoleApplicat ion7.{0}.resour ces", stringTableName );

using (ResourceSet resSet =
new ResourceSet(exe cutingAssembly. GetManifestReso urceStream(resn ame)))
{
foreach (DictionaryEntr y entry in resSet)
Console.WriteLi ne(string.Forma t("Key: {0}, Value: {1}",
entry.Key, entry.Value));
}

HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gm xNOSPAm.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
2069
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 with potential to be useful, but I would like to hear more opinions first. If this is deemed to be useful, I *may* try to write a PEP for it. This is not a promise or even a proposal, at this point. Broadly generalizing, classes in Python have...
8
3946
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
3
3615
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 non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
2
9609
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 define the behavior for most, but not all of its members. The derived classes will define the behavior for the remaining members (the undefined members). I'd like to force the derived classes to implement the undefined members in the base class. I...
10
25744
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 ) Is there a way to get this information ? Thnaks in advance
3
4355
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 is to be filled out online, but I can imagine, for example, one of our members giving a talk and printing a pile of application forms to hand out to the audience. I am trying to add some specific styles in the print stylesheet so the forms...
11
3846
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
10
4702
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 no-parameter constructor for my class with an empty function body. I then instantiated an object and tried printing its values, amazingly the members were already initialized. But how is this possible if I have not included any code for doing so. The...
2
7996
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 information from the PrintJob (Cant seem to find anything on this) 4) Delete the Job when I have the RAW information (I know this can be done)
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.