Using DataTable to evaluate string equation [duplicate] - c#

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Closed 3 years ago.
I have this TextBox where you can type in something like "1 + 2 + 3". I want the program to evaluate the answer (6 in this case) and assign it to a var. Then output the answer to a TextBlock. Tried the following but I'm getting a red underline under DataTable. It says 'DataTable' does not contain a constructor that takes 0 arguments
using System.Data;
private void ButtonCalculate_Click(object sender, RoutedEventArgs e)
{
string equation = textBoxEquation.Text;
var answer = new DataTable().Compute(equation, "");
textBlockAnswer.Text = answer;
}

I solved this by:
Right click project name in Solution Explorer
Choose Properties
Target the Min version to at least Fall Creators Update

Related

can someone explain to me why there is a zero inside after the compareTo expression.. thank you [duplicate]

This question already has answers here:
C# CompareTo method confusion
(3 answers)
Closed 1 year ago.
what is the zero in the expression for?
public void Insert(TItem newItem)
{
TItem currentNodeValue = this.NodeData;
if (currentNodeValue.CompareTo(newItem) > 0)
{
// insert new item into the left subtree
}
}
The CompareTo method is probably returning an int value and you are checking to see if that value is greater than 0.
If so, based on what you have, you would insert new item to the left subtree.

Getting the last numbers from a string [duplicate]

This question already has answers here:
Select last element quickly after a .Split()
(6 answers)
Extract version number from string(eg : "ver.1.9.0")
(3 answers)
Closed 2 years ago.
Hello I need to get last number from 1.0.0.0 but the number will change so eventually it can be 1.0.0.111
so I have stripped the number from "."
var amount = "1.0.0.23";
var pureAmount = amount.Replace(#".", "");
Console.WriteLine(pureAmount);
and then I have this extension method that returns the number
public static class StringExtension
{
public static string GetLast(this string source, int tail_length)
{
if(tail_length >= source.Length)
return source;
return source.Substring(source.Length - tail_length);
}
}
used like this
Console.WriteLine(amount.GetLast(1));
But what if I don't know how many digits will the last number have? I just need the number behind the last "." is there a way to do the?
You can achieve this using single statement. Try string functions Split and Last
var result = amount.Split('.').Last();

How do I make permanent changes to a variable within for loops so that the variable doesn't reset every iteration? [duplicate]

This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 3 years ago.
I want to replace characters in a string iteratively using the string.Replace() function in a for loop however every iteration the string itself resets and no changes made in the previous loop stay. Ending up in the string undergoing no changes overall.
I've tried making the string a class member but that didn't work,
static void Main(string[] args)
{
int i;
string strOne = "abcdefg";
for (i=0; i < (strOne.Length - 1); i++)
{
string c = Convert.ToString(strOne[i]);
strOne.Replace(c, "1");
}
}
I expect the output of 1111111 but instead i get abcdefg.
When you call string.Replace(), it does not modify the string itself. Instead, it returns the string with the replaced characters. Therefore, you need to do this: strOne = strOne.Replace(c, "1");

How to add space between two words in c# enum list [duplicate]

This question already has answers here:
How to set space on Enum
(19 answers)
Closed 5 years ago.
I want to space in enum in c#.Like : FabricRollManagement to Fabric Roll Management
I need enum list not a single value.where i pass a enmu list and return a list that contain multiple value
You can't, C# doesn't allow whitespace in variable, class or any declaration.
Simple, you can not do it to code, but you can display it.
String save;
foreach (x in yourenum.toString().toCharArray()) {
if Char.isUpper(x) save += " " + x;
else save += x;
}

Using the '?', '$' and ':' sign in C# [duplicate]

This question already has answers here:
question mark inside C# syntax [duplicate]
(7 answers)
Closed 5 years ago.
I recently came across this bit of code:
public void AppendTextColour(string text, Color color, bool AddNewLine = false)
{
rtbDisplay.SuspendLayout();
rtbDisplay.SelectionColor = color;
rtbDisplay.AppendText(AddNewLine
? $"{text}{Environment.NewLine}"
: text);
rtbDisplay.ScrollToCaret();
rtbDisplay.ResumeLayout();
}
What fascinates me about it is the restructuring of AppendText(); bit here:
rtbDisplay.AppendText(AddNewLine? $"{text}{Environment.NewLine}": text);
I can only guess that the question mark is used to instantiate the boolean value, however the dollar sign and the double dots sign here are absolutely obscure. Can anyone dissect this bit and explain it to me?
I apologise if I am being a bit ambiguous but I was unable to find any relevant information anywhere. :/
This line:
rtbDisplay.AppendText(AddNewLine? $"{text}{Environment.NewLine}": text);
can be written as:
if (AddNewLine){
rtbDisplay.AppendText(string.Format("{0}{1}",text, Environment.NewLine)); // or just (text + Environment.NewLine)
} else {
rtbDisplay.AppendText(text);
}
It uses the ternary operator and string interpolation (introduced in C# 6)

Categories

Resources