Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C#  using string as part of int

Posted on 2010-09-16
6
347 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
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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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