Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C#  using string as part of int

Posted on 2010-09-16
6
Medium Priority
?
353 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

564 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