How to format [decimal?] when it's null? [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.
In my class partData the FW_Step attribute isfrom the type double?
When I try to format it like that
partData.FW_Step.Value.ToString("F3")
It's fail when the value is null
How can I use the format when the value is null?

You can't format when it's null; hopefully the reasons why are obvious. You need to check for the value first:
string formattedValue;
if (partData.FW_Step.HasValue)
formattedValue = partData.FW_Step.Value.ToString("F3");
else
formattedValue = "default value for null";
You can make this code shorter using a ternary expression:
string formattedValue = partData.FW_Step.HasValue ? partData.FW_Step.Value.ToString("F3") : "default value for null";

Related

generic variable Names [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.
I have a string variable which has the value that the user taps. I need to make another variable which its name will be the string's value.
How do / can I do that?
No, you cannot do that: the closest you can get is a Dictionary<string,object> (you can replace the object with some other type). Using this dictionary you would be able to create associations between strings (known as "keys") and values stored in the dictionary.
IDictionary<string,object> variables = new Dictionary<string,object>();
string varName = "hello";
variables[varName] = "world";
Console.WriteLine("Name: {0} Value: {1}", varName, variables[varName]);

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
}

How to write the regex expression? [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.
The input string: A+SUM(A)C+AB-C+SUM(A)+1
I want to replace A with 0, the result like this:0+SUM(A)C+AB-C+SUM(A)+1
or replace SUM(A) with 0, the result like this:A+SUM(A)C+AB-C+0+1
Thanks
Without Regex (because Regex is overkill for this):
var s = "A+SUM(A)+B-C";
var replaceBeginningA = s.Replace("A+", "0+");
var replaceSumA = s.Replace("SUM(A)", "0");
Console.WriteLine(replaceBeginningA); // 0+SUM(A)+B-C
Console.WriteLine(replaceSumA); // A+0+B-C
As pointed out in the comments though, you need to provide more detail if this input is expected to have a different format.
maybe:
replace ^A with 0
replace SUM\(A\) with 0
Try thus:
string replaced = Regex.Replace(input, #"\b(\w+)\+SUM\(\1\)", "0+0");
This will match anything of the form
foo+SUM(foo)
and replace it with 0+0

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.

Error at tryparse decimal conversion from textbox [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.
When i read from the XML file, i read it like this and it works,
decimal checkkkk = Config.Location.test.Longitude;
textBox3.Text = checkkkk.ToString(CultureInfo.InvariantCulture);
I want to write it back to the same XML file, I'm getting a error at this point..!
decimal value;
Configs.Location.test.Longitude =
decimal.TryParse(textBox3.Text, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out value);
What is the mistake?
The method Decimal.TryParse returns the boolean data-type and not decimal.
Decimal.TryParse, Converts the string representation of a number to its Decimal equivalent using the specified style and culture-specific format. A return value indicates whether the conversion succeeded or failed.
Try to do it this way instead:
decimal value;
if (decimal.TryParse(textBox3.Text, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out value))
{
rseConfigs.RseLocation.GpsCoordinates.Longitude = value;
}

Categories