Quote:
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?
Quote:
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.