Solved

C#  using string as part of int

Posted on 2010-09-16
6
342 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 300 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 100 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 100 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now