Error of List<T>.RemoveAt() [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
lock("data"){
if(_requestList.Count>1 && _requestList[1]==null){
Debug.Log("why0");
}
_requestList.RemoveAt(0);
if(_requestList.Count > 0 && _requestList[0] == null){
Debug.Log("why1");
}
doSomething ();
}
_requestList is a List of string
Sometimes it logs "why0", sometimes "why1", and sometimes both, and sometimes nothing.
The elements added is never null.
So why?

First of all you need to use a
private readonly object lockObject = new object();
lock(lockObject)
{
}
You also need to put a lock(lockObject) around anywhere where you are adding to the list.
Hope that helps in some regard.
You could also look into a ConcurrentBag if you don't want to worry about threading.

Related

Javascript regexp.test() .NET equivalent [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How can I do the following in C# :
var re = /^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$/;
re.test('2013/03/05 15:22:00'); // returns true
You can use the Regex.IsMatch instead (docs).
Regex.IsMatch("2013/03/05 15:22:00", #"^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$"); // true if match
The below code should get you where you want to be.
Regex rx = new Regex(#"^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$");
String test = "2013/03/05 15:22:00";
if (rx.IsMatch(test))
{
//Test String matches
}
else
{
//Test String does not match
}

find child node title's text equel a value [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How do i find SiteMap.RootNode.ChildNames title's value equel 'test' in one line?
I don't write linq it doesn't work.
protected SiteMapNodeCollection getParentNodeTitle()
{
SiteMap.RootNode.ChildNames
}
This should do the trick:
var mySiteMap = new SiteMap();
/* Lots of code for populating your SiteMap here */
var nodeTitledTest = mySiteMap.RootNode.ChildNodes.Where(x => x.Title == "test").FirstOrDefault();
This will return the first node with a title equal to "test" or null if no such node could be found.

Environment.CurrentDirectory in Resources [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to put this string in my resources:
Environment.CurrentDirectory + \\Server Files\\
But whenever I load the string, the Environment.CurrentDirectory part is shown as normal text instead of the current directory path :(.
Example:
Console.WriteLine(Resources.ServerFilesLocation); // Doesn't give me a path, but just plain text.
Any runtime environment value cannot be stored as a string in resources. What you should do is create a layer between. Something like:
static class ResourceManager
{
public static string ServerFilesLocation {
get {
return String.Format(Resources.ServerFilesDirectory, Environment.CurrentDirectory);
// ServerFilesDirectory = "{0}\\Server Files\\" or something similar
}
}
}
And use it like: Console.WriteLine(ResourceManager.ServerFilesLocation);

how does the new operator works on exceptions constructor without class declaration? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
***how can you implement the constructor without naming or needing the class such
as
EX:
string x = new string()
how can i just implement
new string() by itself without the need for string x.
I known it's calling the constructor but doesn't the class has to be initialized?
Ex:
new InvalidOperationException(".....")
throw new ArgumentException("...");
Your first example instantiates an InvalidOperationException(), but can't do anything with it because it's not saved to a variable or thrown. If you saved it to a variable, you could throw it later:
var excep = new InvalidOperationException();
throw excep;

How many times is DOSTUFF() called? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
try
{
for (int i = 0; i <10; i++)
{
DoStuff();
if (i>3 && 1== i% 2)
{
throw new Exception();
}
}
}
catch (Exception ex)
{
DoOtherStuff();
}
DOSTUFF() is called 0 times because C# method names are case-sensitive.
If you really meant DoStuff(), then assuming it is not an unimplemented partial method and the method itself is not marked with a conditional attribute specifying an undefined symbol, then it will be called 6 times as in #MikeCito's answer.
6 times. When i hits 5 the remainder will be 1 and it will be greater than 3.

Categories