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 / .NET Framework / New Users / November 2004

Tip: Looking for answers? Try searching our database.

Saving settings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
timb - 29 Nov 2004 16:31 GMT
Hi,
   is it possible to access the properties of a class or structure in a for
each statement?

I want to iterate through the stirng properties in my class or structure and
then write them away to the config file.

thanks for your help in advance

Signature

--
TimB

Matt Berther - 29 Nov 2004 16:42 GMT
Hello timb,

MyClass c = new MyClass();
// init c here

foreach (PropertyInfo pi in c.GetType().GetProperties())
{
   object o = pi.GetValue(c, null);
   // do something here...
}

--
Matt Berther
http://www.mattberther.com

> Hi,
> is it possible to access the properties of a class or structure in
[quoted text clipped - 4 lines]
>
> thanks for your help in advance
Anand[MVP] - 29 Nov 2004 17:35 GMT
Try Serializing the class and storing it away. This is much easier that doing
it all by yourself.

Rgds,
Anand M
VB.NET MVP
http://www.dotnetindia.com

> Hi,
>     is it possible to access the properties of a class or structure in a for
[quoted text clipped - 4 lines]
>
> thanks for your help in advance
Dennis - 30 Nov 2004 00:25 GMT
> Try Serializing the class and storing it away. This is much easier that doing
> it all by yourself.
[quoted text clipped - 12 lines]
> >
> > thanks for your help in advance
Dennis - 30 Nov 2004 00:33 GMT
Try the following which will put all the string values from your objext into
an array "val".

Dim properties As System.ComponentModel.PropertyDescriptorCollection
properties = System.ComponentModel.TypeDescriptor.GetProperties _(myobject)
Dim pd as PropertyDescriptor  
dim val() as string
dim i,n as integer
While i < properties.Count
 pd  = properties.Find(prop, True)
 if pd.ProperType.Name = "String" then
      val(n) = properties.Find(prop, True).GetValue(myObject)
      n = n + 1
 end if                
 i = i + 1
Wend

> Try Serializing the class and storing it away. This is much easier that doing
> it all by yourself.
[quoted text clipped - 12 lines]
> >
> > thanks for your help in advance
Ian Griffiths [C# MVP] - 30 Nov 2004 08:33 GMT
Do you mean XML Serialization or CLR Serialization?

I'm not sure I'd recommend CLR Serialization as a technique for saving
settings.  What happens when you want to add new settings?  Dealing with
versioning is possible but not straightforward.  I tend to avoid this
approach if I think the serialized data is likely to outlive the class in
question.  This tends to be the case with settings - they're going to be
persisted, and you're probably going to want them to continue to work when
thhe software is updated.

But I would use XML Serialization - it is reasonably robust in the face of
gradual changes to the data.  (Of course if you completely revamp things and
change the names, it's going to be hard whatever you do...)  It's also able
to produce files that are easier to edit by hand.

Signature

Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

> Try Serializing the class and storing it away. This is much easier that
> doing
[quoted text clipped - 8 lines]
>> and
>> then write them away to the config file.

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.