7 Replies - 10698 Views - Last Post: 29 June 2011 - 07:40 AM Rate Topic: -----

#1 greyEd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-March 11

Associating names with individual registers (values) in a range

Posted 28 June 2011 - 07:36 AM

Hi there,

I have a list, which I obtain from an XML file (I am using it as a configuration file) which contains the addresses of registers and the number of subsequent registers to read from each point. My problem is, I need the name of each individual parameter/register to allow me to insert into a similarly named database column, but specifying a range instead of single value elements precludes me from individually associating them with their own name.

At the moment I am considering another XML file from which I could build a list/dictionary to handle this association for me, but this means two config files, an additional list, and perhaps another custom class. Is this too much?

Could anyone offer a better solution? This is my first time working with XML/config files so I am unsure what is considered bad practice in this regard. Any help much appreciated, Cheers!

This post has been edited by greyEd: 28 June 2011 - 07:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Associating names with individual registers (values) in a range

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Associating names with individual registers (values) in a range

Posted 29 June 2011 - 04:04 AM

C# has a generic collection called Dictionary, which can do just that.
Dictionary<string, string> dict;
You can pair any variable type you, from string and objects to objects and strings.
msdn.microsoft.com/en-us/library/xfhwa508.aspx

This post has been edited by codeprada: 29 June 2011 - 04:07 AM

Was This Post Helpful? 1
  • +
  • -

#3 greyEd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-March 11

Re: Associating names with individual registers (values) in a range

Posted 29 June 2011 - 06:03 AM

Thanks codeprada,

A dictionary does provide assocaiation, but not with the flexibility I need, I dont know why it did not occur to me before but a custom object is what I implemented to solve this problem, a field for each parameter, 3 in total.

In order to get the desired result, a base address with a certain range, I stored all the values in their own object and used the parameters to calculate the appropriate number of registers between each base, using the 'range' parameter.

This now leads me to another question, but I will start a new thread for that :P
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Associating names with individual registers (values) in a range

Posted 29 June 2011 - 06:14 AM

If it's related to this thread then it'll only be joined back here :)
Was This Post Helpful? 0
  • +
  • -

#5 greyEd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-March 11

Re: Associating names with individual registers (values) in a range

Posted 29 June 2011 - 06:44 AM

Hmmm, it is somewhat related, if you could help that would be great :D

I am not too knowledgable about configuration files just yet, so im really just wondering how to go about this...

I now have my list pulled from the XML file and it works fine :) However, It does not contain all the configuration info I require. In particular, I need to store one value, a single int. It is critical to my program and needs to be externally configurable via a GUI. I was wondering should I store this in the same XML file, if so, how would I access this - would serialisation be overkill? would the entire file require a re-write?

Should I consider storing it as a C# property? Or are different/multiple modes of configuration bad practice? Furthermore, there is another list of ints I require, should these all go in the same XML file as the first or another, and can I access/edit values in each seperately?

Any advice much appreciated!
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Associating names with individual registers (values) in a range

Posted 29 June 2011 - 07:16 AM

Usually when a program is first started all the configuration is loaded and implemented. That's why you are sometimes asked to restart a program for configuration changes to take effect. After you've loaded the values from the XML set them in a publicly accessible class.

I had to deal with a problem like that just recently and what I did was to make a class file that stores the configuration. The class' variables were made static so it's kept the same throughout the entire program. I'm new to C# so if there's a better way the other guys will assist you :)
class Config
{
    private Config() { } //prevent the instances of this class

    public static int Width { get; set; }
    public static int Height { get; set; }
}


//Set the values
Config.Width = 500; //let's say you got these values from your XML
Config.Height = 500;


How are you reading your XML file?

This post has been edited by codeprada: 29 June 2011 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 greyEd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-March 11

Re: Associating names with individual registers (values) in a range

Posted 29 June 2011 - 07:29 AM

This is similar to what I have alright :) My class reads from the XML and then I use its members in my code. Im using serialisation to read the XML file, i.e. its copied into a list of objects that I can work with.

List<MyObject> list;
XmlSerialiser deserialiser = new XmlSerialiser(typeof(List<MyObject>));
using (TextReader textReader = new StreamReader("path")
{
    list = (List<MyObject>) deserialiser.Deserialize(textReader);
}
return list



Thats my problem though, I need another list, and was wondering if I could store it in the same file and read/serialise it seperately, but how do I refer to another section of an XML file? :P
Was This Post Helpful? 0
  • +
  • -

#8 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Associating names with individual registers (values) in a range

Posted 29 June 2011 - 07:40 AM

If it's going to accessed externally by another program store it in a separate XML file. I'm sure the other elements in your XML file are not needed or to be even accessed by the external program.

This is a perfect example that would answer your question. The serialized values will fill your class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1