473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Oct 25 '08 #1
7 1336
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
Oct 25 '08 #2
Looks like I just attempted an answer to your second post.

"Chris Mitroka" <cm******@comcast.netwrote in message
news:OZ**************@TK2MSFTNGP03.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?
Oct 25 '08 #3
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);
Oct 26 '08 #4
On Sat, 25 Oct 2008 20:21:04 -0700, Chris Mitroka <cm******@comcast.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.GetField("Album");
myFieldInfo.SetValue("Album",CodeValue);
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
Oct 26 '08 #5
On Sat, 25 Oct 2008 21:50:25 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
void SetIt(ref string WhatToSet, string SetItTo)
This, of course, should be:

void SetIt(out string WhatToSet, string SetItTo)

Sorry.
Oct 26 '08 #6
I REALLY appreciate all the help. Using reflection, I was able to
accomplish my goal. Again, thank you all for your assistance.
Oct 26 '08 #7
On Oct 25, 5:29 pm, "Rudy Velthuis" <newsgro...@rvelthuis.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.
Oct 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: robin.pain | last post by:
I was looking for GBS's phonetic dictionary when I came across an American who was having problems with it... of course, it can never work for... Geordies, Irish, Brummies, Strynes, South ifrrikuns...
5
by: Nobody | last post by:
excuse the "windows" code below, but this is a C++ question... or maybe a C. Anyways, I want a function where I can pass in an array of strings (variable count, variable size), so I defined this:...
2
by: Tobias Erbsland | last post by:
Hello I have a problem with an error message. I created a new class (as OpenSSL helper): class DataBlock { public: DataBlock(); DataBlock( unsigned int size );
4
by: Casper | last post by:
In a recent posting, my notation was corrected: > tree* prootNode = new tree; > tree* psubNode = new tree; > > prootNode->child = psubNode; > psubNode->parent =...
10
by: blimeyoreilly | last post by:
Hi I've a small problem .. can anyone figure it out? I am working in VS.NET in C++ with MFC. I have a CWinApp-based class called CTestHarnessApp. I keep getting the 'differs in levels of...
19
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A...
6
by: Angus | last post by:
I am using a global which is a void* I have it defined in one file as: void* hFlag; and one other header file as: extern void* hFlag; But I get this compile error:
2
by: =?Utf-8?B?Y21pdHJva2E=?= | last post by:
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,...
3
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.