Author Topic: Iterate through class Properties assigning Values [vb.net]  (Read 7903 times)

0 Members and 1 Guest are viewing this topic.

Ricky_76

  • Guest
Iterate through class Properties assigning Values [vb.net]
« on: October 29, 2008, 03:37:23 AM »
Hi guys,
some time ago, fixo give me a nice solution to iterate through the prop of a class/structure:

       Dim c As New contact
        For Each obj As [Object] In c.GetType.GetFields
            MsgBox(obj.name)
        Next


now I have a collection of values (strings) and I need to associate each of them to the class prop.

Ex:

dim cValues as new collection
cValues.add "Ralph"
cValues.add "Watson"
cValues.add "34"

Public Class Person
    Public Name as string
    Public Surname as string
    Public Age as short
End Class

Dim Prs as new Person



I need to associate in a for cycle the values to the props

For iProp as short = 1 to Prs.GetType.GetFields.Length
    Prs.fields(iProp).value = cValue(iProp) 'doesn't work
Next iProp


Tnx a lot for the help
/r



« Last Edit: October 29, 2008, 06:42:30 PM by Ricky_76 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Iterate through class Properties assigning Values
« Reply #1 on: October 29, 2008, 06:49:11 AM »
Have a play with something like this ..


Code: [Select]
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using System.Linq;
using System.Text;

namespace ConsoleApplication1032
{
    public class Person
    {
        private string name;  // the name field
        public string Name    // the Name property
        {
            get { return name;}
            set { name = value;}
        }
        //
        private string surname;  // the surname field
        public string SurName    // the SurName property
        {
            get { return surname;}
            set { surname = value;}
        }
        private string age;  // the age field
        public string Age   // the Age property
        {
            get { return age;}
            set { age = value;}
        }               
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person peopleEater = new Person();
            peopleEater.Name = "Fred";

            System.Console.WriteLine("Name in the derived class is: {0}",
                peopleEater.Name);

            Collection<string> RW = new Collection<string>();
            RW.Add("Ralph");
            RW.Add("Watson");
            RW.Add("34");

            System.Console.WriteLine("Name in the Collection is: {0}", RW[0]);

            peopleEater.Name = RW[0];
            peopleEater.SurName = RW[1];
            peopleEater.Age = RW[2];

            System.Console.WriteLine("The PurplePeopleEater is: {0} {1} aged {2}",
                peopleEater.Name,
                peopleEater.SurName,
                peopleEater.Age);
        }
    }
}





edit : added the piccy
« Last Edit: October 29, 2008, 06:54:01 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #2 on: October 29, 2008, 07:07:04 AM »
I notice you've just changed the Thread title to include [vb.net]
If you want this in vb just try to translate it with one of the conversion sites available .. I don't do vb :(
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Ricky_76

  • Guest
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #3 on: October 29, 2008, 09:14:23 AM »
Hi Kerry,

it was my fault.
Thanks again for the help, I'll try to translate your code to vb.

I'll let yuo know.
/r

Ricky_76

  • Guest
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #4 on: October 29, 2008, 09:22:39 AM »
Hi Kerry,

you don't associate value to prop in a for cycle like I need!

            peopleEater.Name = RW[0];
            peopleEater.SurName = RW[1];
            peopleEater.Age = RW[2];


I need:

    for iVal = 1 to 3
        peopleEater(iVal).value = RW[iVal];
    next iVal


Bye
/r
« Last Edit: October 29, 2008, 10:31:44 AM by Ricky_76 »

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #5 on: October 29, 2008, 05:27:34 PM »
I know vba uses base 1 for collections, does net vb do this and c# not?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #6 on: October 29, 2008, 06:00:39 PM »
..........
I need:

    for iVal = 1 to 3
        peopleEater(iVal).value = RW[iVal];
    next iVal


Bye


I'm not sure where you got the idea that fields/properties in classes are indexable numerically.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #7 on: October 29, 2008, 10:46:19 PM »
Hi Kerry,

you don't associate value to prop in a for cycle like I need!

            peopleEater.Name = RW[0];
            peopleEater.SurName = RW[1];
            peopleEater.Age = RW[2];


I need:

    for iVal = 1 to 3
        peopleEater(iVal).value = RW[iVal];
    next iVal


Bye
/r

Ignoring for the moment that what you're doing is fundamentally unsound
and that Kerry is quite correct in pointing out that order of elements in the
array returned by GetFields is not something you can or should rely on,
here is how you would do it:

Code: [Select]

'  In VB.NET, everything is 0-based, not 1-based (e.g., 'option base 0')

     Dim fields() as FieldInfo = Prs.GetType.GetFields()

     For iProp as short = 0 to fields.Length - 1
         Dim field As FieldInfo = fields(iProp)          ' big mistake
         field.SetValue( Prs, cValue(iProp) )
     Next iProp


That said, we could just rename this thread 'How to hang yourself'.

« Last Edit: October 30, 2008, 05:31:01 AM by TonyT »

Ricky_76

  • Guest
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #8 on: October 30, 2008, 10:40:27 AM »
Hi Kerry & TonyT,

tnx a lot for the support.
I have only asked how to, if you think it's insane, I'll drop my stupid solution trying to find out onother way to automate my process.  :cry:

THANKS again for you time!!!
sincerely

/r

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #9 on: October 30, 2008, 11:16:06 AM »
...we could just rename this thread 'How to hang yourself'.

Hi Kerry & TonyT,

tnx a lot for the support.

Doesn't sound like support to me.   :|
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #10 on: October 30, 2008, 03:20:08 PM »
whoever;
mixing language expectation and capability is a certain way to death, or worse ...

///----------

Ricky, did you try the methodolody I indicated. ?

Why are you determined to use an indexed method to identify the fields.  ?











kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #11 on: October 30, 2008, 05:33:03 PM »
Hi Kerry & TonyT,

tnx a lot for the support.
I have only asked how to, if you think it's insane, I'll drop my stupid solution trying to find out onother way to automate my process.  :cry:

THANKS again for you time!!!
sincerely

/r

If you take the time to become familiar with serialization and the basic concepts behind it, then you might understand why it is a very bad idea to couple your persistent data to your class.

What happens if you need to add/remove/change a member of the class? In that case, reading your existing data will fail because the order of the fields may no longer correspond to the order of the values in the persisted data.

You are making a mistake by using this scheme, and we are simply trying to show you why.

Labeling that as 'stupid' or 'insane' was your idea.

TonyT

  • Guest
Re: Iterate through class Properties assigning Values [vb.net]
« Reply #12 on: October 30, 2008, 05:35:28 PM »
...we could just rename this thread 'How to hang yourself'.

Hi Kerry & TonyT,

tnx a lot for the support.

Doesn't sound like support to me.   :|

No, it's not support.

It's called Learning.