Indirection Functionality

C

Chris Mitroka

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?
 
R

Rudy Velthuis

Chris said:
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.
 
C

Chris Mitroka

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()
{
InitializeComponent();
}

private void button1_Click(object 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.GetField("Album");
myFieldInfo.SetValue("Album",CodeValue);
 
C

Chris Mitroka

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

Ignacio Machin ( .NET/ C# MVP )

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.

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.
 
R

Rudy Velthuis

Ignacio said:
I do not see how the above code solve the problem. I think you did not
understand the post.

Your intention was indeed not clear to me. I see you need reflection.

--
Rudy Velthuis http://rvelthuis.de

"I don't know why we are here, but I'm pretty sure that it is
not in order to enjoy ourselves."
-- Ludwig Wittgenstein (1889-1951)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top