Indirection Functionality

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Mitroka

    Indirection Functionality

    I'm writing a C# class and need to do something known to me in Cache
    (another language) as indirection. Not sure if the functionality exists in
    C#, but could really use some help here. Basically, pretend a method takes
    in two string variables: field (and we'll say the value is Name) and
    fieldvalue (and we'll say the value is Chris). This is within a public
    class having a field of Name. I need that variable to now be set to Chris.
    Any ideas how I can accompilsh this without a switch or if/else?


  • Rudy Velthuis

    #2
    Re: Indirection Functionality

    Chris Mitroka wrote:
    I'm writing a C# class and need to do something known to me in Cache
    (another language) as indirection. Not sure if the functionality
    exists in C#, but could really use some help here. Basically,
    pretend a method takes in two string variables: field (and we'll say
    the value is Name) and fieldvalue (and we'll say the value is Chris).
    This is within a public class having a field of Name. I need that
    variable to now be set to Chris. Any ideas how I can accompilsh this
    without a switch or if/else?
    Are you perhaps talking about reference parameters? Hmmm...

    public void SetField(ref string field, string fieldvalue)
    {
    field = fieldvalue;
    }

    And it is called like:

    SetField(ref FirstName, "Chris");
    SetField(ref LastName, "Mitroka");

    Of course, the types must match, but you could overload to make it work
    for several types.

    But I am not entirely sure that is what you are talking about.
    --
    Rudy Velthuis http://rvelthuis.de

    "I'm so poor I can't even pay attention." -- Unknown

    Comment

    • Family Tree Mike

      #3
      Re: Indirection Functionality

      Looks like I just attempted an answer to your second post.

      "Chris Mitroka" <cmitroka@comca st.netwrote in message
      news:OZPQ8duNJH A.1552@TK2MSFTN GP03.phx.gbl...
      I'm writing a C# class and need to do something known to me in Cache
      (another language) as indirection. Not sure if the functionality exists
      in C#, but could really use some help here. Basically, pretend a method
      takes in two string variables: field (and we'll say the value is Name) and
      fieldvalue (and we'll say the value is Chris). This is within a public
      class having a field of Name. I need that variable to now be set to
      Chris. Any ideas how I can accompilsh this without a switch or if/else?
      >

      Comment

      • Chris Mitroka

        #4
        Re: Indirection Functionality

        Just a heads up - I've never used 'Newsgroups', so I'm not sure if my
        responses are working correctly or going to the right people.

        I think I did a bad job explaining what I'm trying to accomplish. Here's a
        quick example with some code:
        public partial class Form1 : Form
        {
        private string FName;
        private string LName;
        public Form1()
        {
        InitializeCompo nent();
        }

        private void button1_Click(o bject sender, EventArgs e)
        {
        SetIt("FName", "Chris");
        }
        private void SetIt(string WhatToSet, string SetItTo)
        {
        //Code would set the global var FName (or LName) to whatever
        SetItTo is
        }
        }
        So if anyone knows code that could make this work (IE: Fill in the SetIt
        method), it would be appreciated.

        Now I think the reflection thing was heading down the right path, but
        couldn't get the set to work. Here some code of what I tried:
        Type myTypeA = typeof(ID3v2);
        FieldInfo myFieldInfo = myTypeA.GetFiel d("Album");
        myFieldInfo.Set Value("Album",C odeValue);


        Comment

        • Peter Duniho

          #5
          Re: Indirection Functionality

          On Sat, 25 Oct 2008 20:21:04 -0700, Chris Mitroka <cmitroka@comca st.net>
          wrote:
          [...]
          So if anyone knows code that could make this work (IE: Fill in the SetIt
          method), it would be appreciated.
          >
          Now I think the reflection thing was heading down the right path, but
          couldn't get the set to work. Here some code of what I tried:
          Type myTypeA = typeof(ID3v2);
          FieldInfo myFieldInfo = myTypeA.GetFiel d("Album");
          myFieldInfo.Set Value("Album",C odeValue);
          Yes, reflection would be one way to approach the problem.

          However, IMHO it's not a great way to go. It's slow, and it's not very
          maintainable. It seems to me that if you want to get and/or set values
          based on a string rather than a compiled identifier, you should store your
          values in a Dictionary.

          That's assuming, of course, that specifying the value to set by a string
          is in fact the best approach in the first place; in the code you posted,
          it's not clear at all why you wouldn't just use the variable itself (or,
          frankly, why you're calling a separate method at all...a different example
          where it's more clear _why_ you want to do this might help). It's
          possible that what you really want is something like this:

          SetIt(out FName, "Chris");

          with:

          void SetIt(ref string WhatToSet, string SetItTo)
          {
          WhatToSet = SetItTo;
          }

          If you want to be able to access the value by string _and_ a compiled
          identifier, you could always add a property that goes through the
          Dictionary to set and get the value.

          By the way, I wouldn't call "FName" and "LName" global variables. They
          are member fields of the class Form1. C# doesn't really have the concept
          of global variables at all.

          Pete

          Comment

          • Peter Duniho

            #6
            Re: Indirection Functionality

            On Sat, 25 Oct 2008 21:50:25 -0700, Peter Duniho
            <NpOeStPeAdM@nn owslpianmk.comw rote:
            void SetIt(ref string WhatToSet, string SetItTo)
            This, of course, should be:

            void SetIt(out string WhatToSet, string SetItTo)

            Sorry.

            Comment

            • Chris Mitroka

              #7
              Re: Indirection Functionality

              I REALLY appreciate all the help. Using reflection, I was able to
              accomplish my goal. Again, thank you all for your assistance.


              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: Indirection Functionality

                On Oct 25, 5:29 pm, "Rudy Velthuis" <newsgro...@rve lthuis.dewrote:
                Chris Mitroka wrote:
                I'm writing a C# class and need to do something known to me in Cache
                (another language) as indirection. Not sure if the functionality
                exists in C#, but could really use some help here. Basically,
                pretend a method takes in two string variables: field (and we'll say
                the value is Name) and fieldvalue (and we'll say the value is Chris).
                This is within a public class having a field of Name. I need that
                variable to now be set to Chris. Any ideas how I can accompilsh this
                without a switch or if/else?
                >
                Are you perhaps talking about reference parameters? Hmmm...
                >
                public void SetField(ref string field, string fieldvalue)
                {
                field = fieldvalue;
                }
                >
                And it is called like:
                >
                SetField(ref FirstName, "Chris");
                SetField(ref LastName, "Mitroka");
                >
                Of course, the types must match, but you could overload to make it work
                for several types.
                >
                But I am not entirely sure that is what you are talking about.
                --
                Rudy Velthuis http://rvelthuis.de
                >
                "I'm so poor I can't even pay attention." -- Unknown
                Hi,

                I do not see how the above code solve the problem. I think you did not
                understand the post. The method receive a parameter that indicate
                which property of the class will be set.

                Comment

                Working...