8 Replies - 385 Views - Last Post: 23 February 2012 - 05:28 PM Rate Topic: -----

#1 BrockleyJohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 12

A way of using substitution in property names

Posted 22 February 2012 - 01:10 PM

I'm new to C# and though I've solved similar problems in other languages, I can't find any approach that seems to work in C#.

I'm processing data extracted from a text file that I'd like to load up into properties, and am seeking a way of doing it that isn't hard-coded. The following is an example of what's being processed:
        public void loadField(string fld, string val)
        {
            switch (fld)
            {
                case "SCOMPANY":
                    sys.SCOMPANY = val;
                    break;
                case "SHELP":
                    sys.SHELP = val;
                    break;
                case etc...........
            }
        }


Since it's extracted from a text file, it's all type string at this stage.

Any pointers appreciated - even what this sort of problem is properly called; Google hasn't come up with much around 'substitution'.

Is This A Good Question/Topic? 0
  • +

Replies To: A way of using substitution in property names

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8916
  • View blog
  • Posts: 33,436
  • Joined: 12-June 08

Re: A way of using substitution in property names

Posted 22 February 2012 - 01:20 PM

The question is how is your text file structured? Is it like a CSV where you might have a column header bit? Is it XML related where you might find the data in named nodes? I am afraid you don't have some sort of way to ID what is a column name and what isn't then it is not evident to the app and subsequently you need to explicitly define them.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,651
  • Joined: 02-June 10

Re: A way of using substitution in property names

Posted 22 February 2012 - 01:57 PM

So you're just trying to match up the property name string from the file to the property of the same name in your sys class, is that right?

You can use reflection to get all the properties in a class.


                PropertyInfo[] propertyInfos;
                propertyInfos = typeof(sys).GetProperties();
                // sort properties by name
                Array.Sort(propertyInfos,
                        delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
                        { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });

                // write property names
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    Console.WriteLine(propertyInfo.Name); // display the name
                    object dork = propertyInfo.GetValue(this.sysInstance, new object[] { });//get the value example
                    propertyInfo.SetValue(this.sysInstance, dork, null);// set the value to itself as an example of setting

                }


YOu should be able to base something on this.
  • Read a value from the file
  • loop through the properties of hte class until you have a match
  • set the value
  • [repeat with next value from file]


Best to make sure your properties are browseable


[Browsable(true)]
        public string SCompany { get; set;}

This post has been edited by tlhIn`toq: 22 February 2012 - 01:56 PM

Was This Post Helpful? 2
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: A way of using substitution in property names

Posted 22 February 2012 - 03:47 PM

Instead of having to loop, this would also work..

public void loadField(string fld, string val)
{
    PropertyInfo propInfo = sys.GetType().GetProperty(fld, BindingFlags.Public | BindingFlags.Static);

    if (propInfo != null && propInfo.CanWrite)
    {
        propInfo.SetValue(sys, val, null);
    }
}



And that should be all the code you need.
Was This Post Helpful? 2
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: A way of using substitution in property names

Posted 22 February 2012 - 03:51 PM

What if the property isn't a String?
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: A way of using substitution in property names

Posted 22 February 2012 - 03:54 PM

The PropertyInfo.SetValue method uses an object as the type of the value when setting it. I assume the framework does it's magic to go from object to the actual type.

EDITED: oh wait...do you mean the value that's passed to his loadField method? Yeah, I would probably make that generic.
Was This Post Helpful? 1
  • +
  • -

#7 BrockleyJohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 12

Re: A way of using substitution in property names

Posted 22 February 2012 - 04:55 PM

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#8 BrockleyJohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 12

Re: A way of using substitution in property names

Posted 23 February 2012 - 05:06 PM

Once again, thanks. I tried implementing your suggestions as follows:
    public class SystmLoader : Systm
    {
        public void loadField(string fld, string val)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fld, BindingFlags.Public | BindingFlags.Static);

            if (propInfo != null && propInfo.CanWrite)
            {
                propInfo.SetValue(this, val, null);
            }
        }
...

but it seems that propInfo always comes out null so the values don't get set. Can you only set a property that's already got a value to a different one; should I be initialising them somewhere?

Or have I just done something daft in applying it?
Was This Post Helpful? 0
  • +
  • -

#9 BrockleyJohn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 12

Re: A way of using substitution in property names

Posted 23 February 2012 - 05:28 PM

Here's an example definition of one of columns:
		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_SCOMPANY", DbType="VarChar(32) NOT NULL", CanBeNull=false)]
		public string SCOMPANY
		{
			get
			{
				return this._SCOMPANY;
			}
			set
			{
				if ((this._SCOMPANY != value))
				{
					this.OnSCOMPANYChanging(value);
					this.SendPropertyChanging();
					this._SCOMPANY = value;
					this.SendPropertyChanged("SCOMPANY");
					this.OnSCOMPANYChanged();
				}
			}
		}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1