Improve company productivity with a Business Account.Sign Up

x
?
Solved

C#  using string as part of int

Posted on 2010-09-16
6
Medium Priority
?
356 Views
Last Modified: 2012-05-10
Hi,
Can you help with the following:

I need to have a int that has a string as part of the name.
Example:
int XYZ100;
and want to assign a value to int XYZ100 by a string so that when
the string = XYZ, it will know to use int XYZ100.

This is for streaming data so it needs to be fast and not looking for simple if statement,
I'm currenly using If statements but need to expand to more than 200 different int and think there is faster way then if statements (hardcoded).

I'm not sure if Dictionary is the fastest?
I would like someone to show how to do the above by using just string and the int.
Reflection has been suggested but as a slower solution.
Any suggestions and examples greatly appreciated.
thanks
newbie
0
Comment
Question by:SSschultz
6 Comments
 
LVL 12

Accepted Solution

by:
w00te earned 1200 total points
ID: 33698214
You will not find a way to handle the names of your variables without using reflection, so I'm not sure what your aiming at.

You can easily have a dictionary<int, string> that has specific string values assigned to specific integer values, but you will not have the names of the integers (or the strings) accessible to you in any way shape or form.  If you do use a dictionary in your implementation you should know its implemented with an internal hash table and has incredibly fast average lookup rates (you won't beat them, so you should use it for speed in most cases).

Moving on... why would you even care what the name of a particular integer was?  Names are irrelevant for anything but logging really (and usually even then) -- it's a very strange request.  Maybe if you show us you're best attempt in code we'll actually know what you're looking to make - it would also help if you said what problem you're aiming to solve with the code.  There might be alternate solutions at least :)

Have a good night!

-w00te
0
 

Author Comment

by:SSschultz
ID: 33698344
w00te,

I'm using public static variables/int to use in other classes forms etc.
I guess I can use public static dictionary and do lookup outside collection classes and
would work same as public static var?
Do you think public static Dictionary would be as fast as public static int?
Got your point that dictionary is probably fastest within the collection class.

The reason for having the stringname in the integer name is because the value collection from the streaming collection is applicable to specific characters, so in order to keep track of it I was/am currently using int with name of the string including to separate each string char because there are certain caluclations on int for each different stringname.
Make sense.
0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 400 total points
ID: 33698645
Definitely there's no way you can work it out with relying on reflection since you're setting value dynamically. Having said that reflection will have performance impact not unless you're doing most of the stuff using reflection.

Since you're only trying to set property value on an instance I don't think there should be significant impact. The below code is an extension method so that you encapsulate the reflection logic and the Method SetPropertyValue is available  instances of class. It's now very easier for you to set the values on the preferred objects.
    static class ObjectExtension
    {
        public static void SetPropertyValue(this object _this, string _prefix, int _value, bool _isStatic)
        {
            System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.NonPublic;
            if(_isStatic)
                flags |= System.Reflection.BindingFlags.Static;

            System.Reflection.PropertyInfo pi = _this.GetType().GetProperty(_prefix + _value, flags);
            if (pi != null)
            {
                if (_isStatic)
                    pi.SetValue(null, _value, null);
                else
                    pi.SetValue(_this, _value, null);
            }
        }

        public static void SetPropertyValue(this object _this, object _targetObject, string _prefix, int _value, bool _isStatic)
        {
            _targetObject.SetPropertyValue(_prefix, _value, _isStatic);
        }
    }

    class Target
    {
        public static int XYZ100;
    }

    class Test
    {
        private static int XYZ100;
        private Target t = new Target();

        public Test()
        {
            this.SetPropertyValue("XYZ", 100, true);

            this.SetPropertyValue(t, "XYZ", 100, true);
        }
    }

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 400 total points
ID: 33698663
if you would use a Dictionary<string, int>, then you can access the "int" values (read/write) by using a string which is in this case the "key", not the variable name.  Also, you the number of items in your dictionary is not limited, like it would be if your would be using regular variables and reflection.

However, if your string = "XYZ", how will the application know it should use key "XYZ100" and not "XYZ101" or "XYZ99"?  If you want performance, make sure you know exactly what the key is, so you don't have to loop through all the keys to find out which key contains the string you are looking for.
0
 
LVL 7

Expert Comment

by:gsiric
ID: 33699004
HiI SSschultz i suggested you to user  IDictionary in your previous question (not closed btw) so i am supprised why are you oppening new one :)

If your set of XYZnn is determinstic you can fill your dictionary with all posible combinations so you only need to set value when data arrive.  This way you will get some on speed.

0
 

Author Comment

by:SSschultz
ID: 33699101
Hi qsiric,
Yes, I opened a second question because I wanted to see if I asked the question a different way if some different ideas would be suggested. I'll be closing both questions within the day.
It appears that the public static dictionary is working across classes.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question