This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am writing a helper method for conveniently setting the Name of a Thread:
public static bool TrySetName(this Thread thread, string name)
{
try
{
if (thread.Name == null)
{
thread.Name = name;
return true;
}
return false;
}
catch (InvalidOperationException)
{
return false;
}
}
It's working as intended. ReSharper, however, claims that the condition is always false and the corresponding code is heuristically unreachable. That's wrong. A Thread.Name is always null until a string is assigned.
So, why does ReSharper think it is? And is there some way to tell ReSharper it isn't (other than // ReSharper disable ...)?
I'm using ReSharper 5.1.3.
This was fixed in 6+ of RS I think. See here.
It appears to be a bug in R#, fixed in v6.
see: http://devnet.jetbrains.net/message/5366898
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I need to know how I check the character in first position in a string on C# code. For example , if the first character is the character "&" or other.
Thanks.
As you can see from the answers, there are many ways to accomplish this. You should be careful to avoid exceptions that will be thrown if you attempt to call methods on a string that is null or use indexers on a string that is null or empty.
if(!String.IsNullOrEmpty(input) && input[0] == '&')
{
// yes
}
or…
if(input != null && input.StartsWith("&"))
{
// yes
}
The easiest way without needing multiple checks is to use the String.CompareOrdinal overload.
string test = "&string";
if (String.CompareOrdinal(test, 0, "&", 0, 1) == 0) {
// String test started with &
}
This has the added benefit of not needing to check for null or empty as the static method handles those automatically.
string test = "&myString";
if(!string.IsNullOrEmpty(test) && test[0] == '&')
{
// first character is &
}
Try using the String.StartsWith method.
if (MyString.StartsWith("&")) {
// do something.
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a RadioButtonList and I would like to select a radio button in this list based on a value read from a database. Once I read the value from the database, how do I then make the radio button in the list become selected?
You can simply assign the value via RadioButtonList.SelectedValue = reader.value; as long as you know that the value is in the list (if it's not in the list, then you will get an exception when that line executes).
Since it sounds like you do not know for sure that reader.value will be one of the options in the RadioButtonList, so you will need to check that first.
if(RadioButtonList.Items.FindByValue(reader.value) != null) {
RadioButtonList.SelectedValue = reader.value;
}
Alternatively, you could handle the exception via a try/catch.
string sSortname = row["GoodsSortName"].ToString().Trim();
foreach (ListItem s in this.rdbSort.Items)
{
if (s.Text == sSortname)
{
s.Selected = true;
break;
}
}
I Used this way ,had sovle this problem
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Currently, not even the simplest examples of using the 'ExpandoObject' work on my machine.
Both
dynamic obj = new ExpandoObject();
obj.Value = 10;
var action = new Action<string>((line) => Console.WriteLine(line));
obj.WriteNow = action;
obj.WriteNow(obj.Value.ToString());
(from this website) and
dynamic sampleObject = new ExpandoObject();
sampleObject.test = "Dynamic Property";
Console.WriteLine(sampleObject.test);
(from the MSDN examples) fail with a RuntimeBinderException. I presume I've misconfigured something, but I am at a loss about what it might be.
I am using .NET v4.0.30319 and Visual Studio 2010 SP1 Premium. Please ask for anything else you might need to know. =)
Deleting the hidden "SolutionName.suo" file in the solution directory fixed this problem for me.
I still have no clue why it occured, though.
Edit:
Andras Zoltan, who deleted his answer, guessed correctly. I have had "Break on all Exceptions" enabled and was being stupid. =)
The problem is simply that Console.WriteLine has too many overloads and so the dynamic part cannot be figured out correctly.
Put the output into a typed variable before or just cast it.
e.g.
dynamic sampleObject = new ExpandoObject();
sampleObject.test = "Dynamic Property";
Console.WriteLine((string)sampleObject.test);
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
When chaining constructors, in C#, how can I easily tell whether or not the constructor was called directly, or was called by another constructor using this?
public Test() : this(string.Empty, string.Empty) {}
public Test(string helloworld) : this(helloworld, string.Empty){}
public Test(string helloworld, string goodbyeworld)
{
//do work
}
If for some reason you really NEED to do this (and you pretty much never need to) this can be accomplished by making your "Master" constructor private or protected and adding another argument that indicates which other constructor was used.
I realize this is sort of a ridiculous answer but the problem is kind of ridiculous as well.
It isn't perfectly accurate, but you can check the call stack. See this question for more info
How can I find the method that called the current method?
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Can anyone help me in casting generic collection in c# 4.
Here is the code snippet.
GridView1.DataSource = dataServiceColl.Select(t => t.product_desc="EdibileItem")
It is throwing up runtime error at the below line,
Gridview1.Databind();
Saying it is a HTTP Exception.
I think it should be a simple type cast.
Thanks,
Kris.
Use
t => t.product_desc=="EdibileItem"
HTTP Exception? That has nothing to do with casting.
More importantly, why are you assigning "EdibleItem" to t.product_desc here?
Select(t => t.product_desc="EdibileItem")
Did you meant == instead of =? If so, would a Where be more appropriate than a Select?
I think it all boils down to: what are you trying to achieve, exactly?