-
January 16th, 2012, 03:38 PM
#1
Symbol to a symbol?
So this has always been something that has eluded me, and I know in the sample code below there are certainly much better ways to do this, but I am using this to illustrate my problem. I want to be able to dynamically use a variable, for example...
string item1;
string item2;
string item3;
string item4;
for (int i = 1; i < 4; i++)
item + i = "hello";
This would go through and assign each item# to "hello". Obviously this code does not work, but does anyone know of a way to accomplish something similar? Please don't come back and say use an array or build a class around it because I know you could do that in this situation, but that is not what I am trying to accomplish. I am actually trying to slim down some query code that I am using in entity framework
-
January 16th, 2012, 04:37 PM
#2
Re: Symbol to a symbol?
This is a very typical beginner question. The answer is...
Don't do that.
Really, don't do that. You never need to do that, you simply aren't aware of better options. You should be using a collection for your strings (i.e., string[], or better yet, a List<string>). Tying your code to variables names would be a horrible idea even if it were allowed (and it is allowed in some languages).
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
January 16th, 2012, 05:10 PM
#3
Re: Symbol to a symbol?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
January 16th, 2012, 05:23 PM
#4
Re: Symbol to a symbol?
OK, like I said, please avoid the obvious answer, now the example will be uglier since I am going to try to show you exactly what I am doing...
Using EF and linq...
var query = from t in checkData.imageData where t.checkFrontOriginalGuid == checkImageReq.checkFrontGUID select new { checkFrontB64 = t.checkFrontOriginalB64 };
I want to be able to dynamically use things such as t.checkFrontOriginalGuid, however, since we must instantiate t within the linq expression, I cannot do that. This snippet of code is dependent on about 15 more lines of code, which I must repeat 4 times for 4 different queries.
-
January 16th, 2012, 06:45 PM
#5
Re: Symbol to a symbol?
Originally Posted by Mutant_Fruit
Sry, but what named and optional parameters have to do with this?
Furthermore, although introduction of named parameters brings C# a (tiny) step away from the C-language family, there's nothing wrong with that. If anything, they increase readability when used. Consider a method that takes a bool parameter; very often, it's not obvious what the method does just from reading the code - since it's not enough, you have no other choice then to go and check the docs.
It's this:
this.Invalidate(true); vs
this.Invalidate(invalidateChildren: true);
As for the optional arguments - there are pros and cons, but ultimately, they can are useful - if the method is carefully designed. And, when it comes to COM interop, you can't possibly prefer typing Type.Missing to just omitting the arguments, can you?
Originally Posted by jurickk
OK, like I said, please avoid the obvious answer, now the example will be uglier since I am going to try to show you exactly what I am doing...
Using EF and linq...
Code:
var query = from t in checkData.imageData
where t.checkFrontOriginalGuid == checkImageReq.checkFrontGUID
select new { checkFrontB64 = t.checkFrontOriginalB64 };
I want to be able to dynamically use things such as t.checkFrontOriginalGuid, however, since we must instantiate t within the linq expression, I cannot do that. This snippet of code is dependent on about 15 more lines of code, which I must repeat 4 times for 4 different queries.
As for your original question (the way you asked it), BigEd781 already gave you an answer, but maybe you've missed it - simply use an array or a List<T>, and then iterate through the array/list. With a list, you can dynamically add/remove elements.
Code:
List<string> myList = new List<string>();
for (int i = 0; i < 4; i++) // adds 4 elements
myList.Add("hello");
myList[2] = "Hello there!"; // changes the 3rd element
for (int i = 0; i < 4; i++) // makes all elements uppercase
myList[i] = myList[i].ToUpper();
foreach (string s in myList) // prints out all the elements using a foreach loop
Console.WriteLine(s);
Hoverer, since I'm not sure I quite follow you on your last post, what BigEd781 suggested may or may not apply here.
The "t" in the query is more akin to a function parameter than anything else, or even closer, like the variable used for the foreach loop. It simply automatically gets assigned values from the checkData.imageData collection, so it's not you who initializes the variable.
When you say "I want to be able to dynamically use things such as t.checkFrontOriginalGuid", I still don't fully understand what do you exactly mean by that, or what's preventing you to do whatever you're trying to do.
Sounds like you could refactor some of the redundant lines of code into a method, but I can't say much without actually seeing the code itself.
I'm guessing that there is something wrong with how you approached the problem, but you haven't provided enough information for us to see exactly what.
Could you at least post the representative 15 lines of code - that is not a lot; just please use the [code][/code] tags to preserve code formatting.
Last edited by TheGreatCthulhu; January 16th, 2012 at 06:54 PM.
-
January 17th, 2012, 10:02 AM
#6
Re: Symbol to a symbol?
I can't really post the surrounding code, but t has more members than just checkFrontOriginalB64, such as checkBackOriginalB64. So, ideally, given the line of code I did post, I would wrap that, along with it's supporting code, into a method and pass in a symbol which represents checkFrontOriginalB64, or checkBackOriginalB64, or whatever member of t I may be querying for, this way I do not have to duplicate the supporting code.
-
January 17th, 2012, 10:29 AM
#7
Re: Symbol to a symbol?
You can do this using delegates :
e.g.
Code:
// have to fill in the types with '...' yourself, don't know what they are
IEnumerable<...> GetQuery(... checkData, Guid guidToFind, Func<Guid, ...> getItem)
{
return from t in checkData.imageData
where getItem(t) == guidToFind
select t.checkFrontOriginalB64;
}
void Show(... checkData)
{
var q1 = GetQuery(checkData, checkImageReq.checkFrontGUID,
(x) => x.checkFrontOriginalGuid);
var q2 = GetQuery(checkData, checkImageReq.checkFrontGUID,
(x) => x.checkOtherOriginalGuid);
}
Get the idea ? The Func<> job is to return the value to compare checkFrontGUID to.
Sorry but I don't know what your types are so I've filled these in with '...'
Darwen.
-
January 17th, 2012, 10:59 AM
#8
Re: Symbol to a symbol?
Yes! That is what I was looking for. Makes perfect sense, thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|