I thought this was a capture issue but nothing I do works. try to simplify here:
foreach (Question question in Test.Questions)
{
int id= question.id;
if(someIDictionary.TryGetValue(id, out value)
{
question.answerobject.number=someinteger;
}
else
{
question.answerobject.number=someotherinteger;
}
}
I tried making a temp for question object, but still not working. all results are always last iteration value.
EDIT: Each answerobject is created within each question object as the question is created using linq to sql. then the questions are returned as an IList.
EDIT 2: The issue does not occur if I assign values to another field of question. the issue is only with the answerobject.the values of answerobject are same for all questions (the last assignment).
What is the problem you are seeing? The "capture" problem only affects async/deferred/threaded code - it shouldn't affect this case.
I wonder if the most likely problem here is that all your Question objects have the same answerobject instance - or even that you have the same Question instance lots of times.
illustration of the "capture propblem" (see comments): this is a problem seen when using a lambda/anon-method; if the iteration variable (question above) is used in the lambda/anon-method, it is "captured" - but in a slightly counter-intuitive way...
For example; we might expect this to print (in an unpredictable order) the numbers 0-9:
int[] vals = {0,1,2,3,4,5,6,7,8,9};
foreach(int i in vals) {
ThreadPool.QueueUserItem(delegate {
Console.WriteLine(i);
});
}
Console.ReadLine();
But it doesn't... we fix it by adding an extra variable:
int[] vals = {0,1,2,3,4,5,6,7,8,9};
foreach(int i in vals) {
int tmp = i;
ThreadPool.QueueUserItem(delegate {
Console.WriteLine(tmp);
});
}
Console.ReadLine();
This is because the behaviour of captured variables is determined by their lexical scope... the scope of the iteration variable is a bit larger that we would like.
The issue appears to occur when answerobject.number (int?) is assigned "Null" when the object is created. If I try to assign a value during the loop i get the issue above. If howwever I assign an integer when the object is created the problem is gone and i can then addign new values during the loop. Can you explain to me cause I dont get it.
Related
I have this loop here, which for each question it is supposed to create, it generates and then formats a 'worded question' from an array of questions, such as; 'What is the sum of {0} + {1}?'. This loop then formats it, adds the worded question and the answer to an array.
// Use for loop to create the correct amount of questions
for (int i = 0; i < Data.Questions.numQuestions; i++)
{
Random rnd = new Random();
Data.Questions.Sum sum = new Data.Questions.Sum();
// Create part one and part two of the question using random numbers
// ex. 3 + 5
// 3 = partOne, 5 = partTwo
int partOne = rnd.Next(Data.Questions.Sum.min, Data.Questions.Sum.max);
int partTwo = rnd.Next(Data.Questions.Sum.min, Data.Questions.Sum.max);
// Randomly select one of the word questions
string fullQuestion = Data.Questions.Sum.wordedQuestions[rnd.Next(0, Data.Questions.Sum.wordedQuestions.Length)];
// Format the string with the generated numbers
fullQuestion = string.Format(fullQuestion, partOne, partTwo);
// Set out-of-class variables to be displayed to the user
Data.Questions.Sum.questions[i] = fullQuestion;
Data.Questions.Sum.answers[i] = partOne + partTwo;
}
Both Data.Questions.Sum.questions and Data.Questions.Sum.answers are List<string>'s and List<int>'s.
However, when this loop is run, with i = 0, I am thrown;
System.ArgumentOutOfRangeException: 'Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index'
Does anyone know what I'm doing wrong? As far as I know lists are dynamic, and I've defined like this;
// Arrays containing all questions and answers
// used to display questions and check answers
public static List<string> questions = new List<string>();
public static List<int> answers = new List<int>();
Also, to clarify, I do not want to use .Add(), as I have a settings panel which when you hit apply, re-runs this loop so the questions are up to date to the current settings. I need the loop to override the previous values.
Edit:
When using arrays, the better option here, I get;
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
On assigning Data.Questions.Sum.answers[i], after assigning the array like so; public static int[] answers {};
If you can't .Add() in this lists - create a copy of this lists and .Add() there. Lists has special for that kind of thing: new List<T>(IEnumerable<T>)
If you need to dynamically scale the collection, but you also need to iterate over it multiple times, then you'll need to check whether it is large enough to either insert, or just update.
You can do this with an extension method such as this...
public static class ListExtentions
{
public static void AddOrUpdate<T>(this List<T> that, int index, T value)
{
if (that.Count > index)
{
that[index] = value;
}
else
{
that.Add(value);
}
}
}
...which can then be called like this...
list.AddOrUpdate(index, value);
...however you can make things easier for yourself if you know how many questions you are going to have to start with.
If the number of questions changes when your UI changes, then you will also have to deal with the issue have scaling down the collection to ensure old elements are removed, which is much simpler if you just re-instantiate the collections every time you need to regenerate the questions / answers.
This is likly to be cause of your problem,(I have asked for clarification in comments where you didn't reply).
Still putting this as an answer, as it is potential error spot and you need to fix this.
As you mentioned you are facing this exception on i=0. there are high chanced that this is every time case not any specific case.
If Data.Questions.Sum.questions is empty then, Data.Questions.Sum.questions[i] = fullQuestion; , will surely throw such exception. Same way for Data.Questions.Sum.answers too.
In such case, you must use .Add() to insert into list.
so your code should be,
if (Data.Questions.Sum.questions.Count > i)
Data.Questions.Sum.questions[i] = fullQuestion;
else
Data.Questions.Sum.questions.Add(fullQuestion);
But if they are not empty, it must not be the cause of this exception.
One more thing i have noticed in your code is Data.Questions.Sum.wordedQuestions.
Even if you have valid list (here Data.Questions.Sum.wordedQuestions) - as you have Length prop, it must be Array not list.
If it is empty, while doing this
string fullQuestion = Data.Questions.Sum.wordedQuestions[rnd.Next(0, Data.Questions.Sum.wordedQuestions.Length)];
this line will surely throw
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
as you are trying to get 0th index's data from it.
So before fetching data from list or array, first you need to check if it does have data init, and also it does have that index which you are asking.
something like
string fullQuestion = string.Empty;
if (Data.Questions.Sum.wordedQuestions != null &&
Data.Questions.Sum.wordedQuestions.Length > 0)
{
//here the way you are creating random number,
// you are assured about index is present in array.
int indexForWordedQuestion = rnd.Next(0, Data.Questions.Sum.wordedQuestions.Length);
fullQuestion = Data.Questions.Sum.wordedQuestions[indexForWordedQuestion];
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I was wondering if it's possible to use some sort of Array.Copy method in C# to copy elements of one array to and array of same size but different type.
I get a struct from a native c++ library that contains a pointer to Integer and a size element. These integer values represent enum Values from enum Foo.
At the moment I'm using a for loop. Is there a better/safer approach?
Thanks!
Using Array.Copy throws an ArrayTypeMismatchException as shown below:
using System;
public class Program
{
public enum Foo
{
FOO_1,
FOO_2
}
public static void Main(string[] args)
{
int nCount = 1;
Foo[] fooArr = new Foo[nCount];
Int32[] RawData = new Int32[nCount];
RawData[0] = 100;
Array.Copy(RawData, fooArr, nCount);
}
}
You could do it with LINQ:
fooArr = RawData.Select(r => (Foo)r).ToArray();
For some reason, neither Array.Copy nor Buffer.BlockCopy is happy to copy from an int[] to a Foo[], although Array.Copy can go in the other direction perfectly happily.
It looks to me like your options are:
The kind of copy you showed in your original post, although I'd write it as:
for (int i = 0; i < RawData.Length; i++)
{
fooArr[i] = RawData[i];
}
.. that way you don't have a redundant variable outside the scope of the loop
A LINQ-based approach as suggested by Romano Zumbé, although in that case you shouldn't create the array beforehand as well
A LINQ-like-but-not-LINQ approach using Array.ConvertAll:
var fooArr = Array.ConvertAll(RawData, x => (Foo) x);
... this is more efficient than the LINQ approach as it can create an array of the correct size to start with.
If you don't need the execution-time type to actually be Foo[], you can just cast without any copying... because the CLR is happy to treat an int[] as a Foo[]:
Foo[] fooArr = (Foo[]) (object) RawData;
At that point, you have a single array object, which you're viewing as either an int[] (via RawData) or a Foo[] (via fooArr). Changes via either variable will be visible in the other variable. This is clearly very efficient, but could lead to problems elsewhere depending on how you use fooArr. If you just use it in foreach loops or via regular index accesses, it would be fine.
Recently had a quiz in my C# class and got some things wrong. I think I have the answers but I want to make sure I am right.
First one:
Explain the result
int[] myArray = {5,10,15,20,25};
DoWork(myArray);
void DoWork(int[] theArray)
{
for (int c = 0; c < theArray.Length; c++)
{
theArray[c] = 1;
}
theArray = null;
}
For this one, I only got half of it right. I said that the loop would set the value for each element in the array to 1. So my question is, what happens when you set the array to null?
Second one:
Explain the result
int[] myArray = {5,10,15,20,25};
DoWork(myArray[1]);
void DoWork(int theItem)
{
theItem = -1;
}
This one I got completely wrong. The correction was that myArray[1] = 10 still. Is this because it is not being passed by reference? This just confused me a lot because I ran a little test program on the first one (without the null part) and all the values were set to 1 in the array but I was not passing by reference.
Q: what happens when you set the array to null?
A: "theArray" (inside the routine) is set to null. But "myArray" (outside of the routine) is UNCHANGED. The reason is that "myArray" is an object reference, which is passed by value into DoWork().
Q: Is this because it is not being passed by reference?
A: Yes, exactly. From the link above:
https://msdn.microsoft.com/en-us/library/9t0za5es.aspx
Any changes to the parameter that take place inside the method have no
affect on the original data stored in the argument variable.
These links explain further:
C# Parameter Passing, Ref and Out
C# - Passing Parameters by Reference
This question already has answers here:
Directly modifying List<T> elements
(6 answers)
Closed 8 years ago.
I am writing a function which is passed in a list which is partially filled. I'd like to set some of the fields within the list inside this function. I thought that passing it as a reference would allow me to do this, however, I get the following error:
Error 1 Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable
I am wondering what I might need to do to tell C# that I wish to have the option of modifying the contents of the list.
Here is a summarized version of my code:
public static void Determine_RTMM_Descriptor(ref List<Struct_Descriptor_Type> symbols, string Dwarf_Output_Filename)
{
...
lines = System.IO.File.ReadAllLines(Dwarf_Output_Filename);
//loop on symbol names
for (int idx = 0; idx < symbols.Count; idx++)
{
if(symbols[idx].size == 0)
symbols[idx].size = (int)new System.ComponentModel.Int32Converter().ConvertFromString(split_line[DwarfInterface.SIZE_INDEX]);
...
}
Thanks in advance for any help.
The underlying issue here is that you have a list of value types. When you use the indexer of the list to get an item from the list you are getting a copy of that type. The code symbols[idx] is the value of that item. It is not a variable representing that item, as the error message is telling you.
You're trying to mutate the size of the copy, which will have no effect on the item of the list. This is such a common mistake that the compiler even makes this an error.
If you really are sure that you want to have a mutable value type (hint: you aren't, and you shouldn't have one; you almost certainly just want to have a class here to avoid this problem entirely) then you would need to get the value of the item, mutate it, and then set the item again:
if(symbols[idx].size == 0)
{
var symbol = symbols[idx];
symbol.size = 42;
symbols[idx] = symbol;
}
Your return type on the function is "void" when you should set the return type to the list. That should allow you to change it and return it modified.
This question already has answers here:
Variable scope confusion in C#
(4 answers)
Closed 9 years ago.
Reference Code
public void MyMethod()
{
string x;
List sampleList = populateList();
foreach(MyType myType in sampleList)
{
string x; // why is this not allowed?
doSomethingwithX(x);
}
}
I recently started learning C# and today ran into issue with code similar to the above. VS2010 flagged the commented code with this message a local variable named x cannot be declared in this scope because it would give a different meaning to variable x which is already used in parent or current scope to denote something else....
I dont get it...isnt that the whole essence of block statements and scoping...I know i can just change my variable names and go ahead.but i'd like to know WHY?
I dont get it...isnt that the whole essence of block statements and scoping...
No, not really. The intention of scoping isn't "to allow you to reuse names".
I know i can just change my variable names and go ahead.but i'd like to know WHY?
It reduces the possibilities for confusing code, basically. There are various situations where C# prevents you from writing code which is confusing. Not as many as we might like, of course, but where there's no clear benefit from allowing something, and a clear benefit from disallowing it, it makes sense to disallow it.
Would you ever want to work with code that had the same local variable name in scope twice? Wouldn't you always prefer the original developer to use a different name? I would - so I'm glad the compiler enforces that.
Note that this doesn't prevent the same local variable name being used twice in the same method - they just can't be in the same scope. So this is valid:
public void Foo()
{
{
int x = 10;
...
}
{
int x = 10;
...
}
}
But this isn't:
public void Foo()
{
{
int x = 10;
...
}
int x = 10;
...
}
If the second example is confusing, you need to bear in mind that the scope of a local variable is the block in which it was declared - not from the declaration point onwards.
Previously defined x is still in scope that's why compiler stops you from declaring other one.
You can verify this by limiting the scope of previous variable by wrapping it in curly braces -
public void MyMethod()
{
{
string x;
}
List sampleList = populateList();
foreach(MyType myType in sampleList)
{
string x; // This will be fine.
doSomethingwithX(x);
}
}
The x in the for loop is not visible outwith the loop, but the x you declared at the start of the method is visible within the for loop.
Outwith the loop you have one x but inside it there are two, both of which are being declared
You cannot declare string x; again. .. it will have different meaning. Since string x; is declared inside a method. The scope of x will be available through out the method. . So please declare some other variable inside for each loop. ..
Or you can just x. Instead of declaring again. .