Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Print members of a class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PiotrKolodziej - 27 Aug 2006 16:26 GMT
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";
       }
Simon Tamman - 27 Aug 2006 17:05 GMT
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.

> Hi
> A have a class containing elements of string type. I need to print them
[quoted text clipped - 34 lines]
>             public const string WinampPrev = "Winamp Previous Track";
>         }
Nicholas Paldino [.NET/C# MVP] - 27 Aug 2006 17:33 GMT
It's almost right.  You will want to get the fields, not properties.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Have you tried using reflection?
>
[quoted text clipped - 55 lines]
>>             public const string WinampPrev = "Winamp Previous Track";
>>         }
PiotrKolodziej - 28 Aug 2006 10:13 GMT
Thank you both
Piotr Kolodziej
Nicholas Paldino [.NET/C# MVP] - 27 Aug 2006 17:34 GMT
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).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi
> A have a class containing elements of string type. I need to print them
[quoted text clipped - 35 lines]
>            public const string WinampPrev = "Winamp Previous Track";
>        }
PiotrKolodziej - 28 Aug 2006 10:22 GMT
That's an interesing point.
Will try to do this both ways.
PiotrKolodziej - 28 Aug 2006 11:17 GMT
>    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.
Andreas Mueller - 28 Aug 2006 12:24 GMT
>>   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
[quoted text clipped - 6 lines]
> 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
Signature

You can email me directly by removing the NOSPAm below
xmenNOSPAm40@gmxNOSPAm.netNOSPAm

PiotrKolodziej - 28 Aug 2006 12:31 GMT
Wow.
Is it really the simplest way?
It isn't hard but doesn't look friendly too
:)
Piotr Kolodziej

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.