C# - String.Format() [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm new to C#. Can anybody explain the following lines:
string value = "";
string tempValue = "=Fields!{0}.Value";
value = RemoveSpace(ReportDataTable.Columns[i].ColumnName);
value = String.Format(tempValue, value);

You need to read about string.Format which replaces each format item in a specified string with the text equivalent of a corresponding object's value.
RemoveSpace would be some method like Trim() to remove the space around the string.

you are formatting the value according to tempValue format, where {0} is place holder
for more info on string format see this

I assume that you want this line to be explained:
value = String.Format(tempValue, value);
String.Format creates strings from a pattern and values. It is a static method in the C# language. It receives a format string that specifies where the following arguments should inserted. The format string uses substitution markers.
So string.Format replaces the "{0}" in this string "=Fields!{0}.Value" with your value.
Side-note: you can (should) always consult MSDN first. Just type the method into google and the first link is probably the documentation.

Related

how to find an specific character in a string [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
In c#, how can I find a specefic character in a string. this "2222+2222" is my string and I want to find the character "+" in it? I want a function that returns a bool if it finds it.
string.Contains("+") returns a bool.
yourString.IndexOf("+") will return 0 or a positive number if the character is found
Since you prefer something that returns bool, you can use Contains instead but notice that Contains does not provide an overload to perform a case-insensitive search. In scenarios where this is important (finding a string without caring for case), it's best to use IndexOf(stringToSearch,StringComparison.InvariantCultureIgnoreCase) to determine whether the string is found or not.
String s = "2222+2222";
if (s.Contains("+")) {
// dosomething...
}

Which one is better? Conversion and Type Cast [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I would like to know which way is better and faster for the following scenarios.
string dateStart = ((DateTime)dtRow["StartDate"]).ToShortDateString();
or
string dateStart = DateTime.Parse(dtRow["StartDate"].ToString()).ToString("dd/MM/yyyy")
If the type of value stored in the StartDate column of data-table is already DateTime, the first one is faster than the second. Otherwise we can't compare them, because the first one crashes.
Cast is doubtfully better because is only appropriate way if underlying data is type of DateTime or compatible.
Second way converts DateTime to String and then back to DateTime what is pointless.

Error converting data type varchar to numeric? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am updating records. But It gives error
Error converting data type varchar to numeric
Here is the query:
UPDATE tbl_Contract_QutationDetails SET DCont_Discount_Var=23
WHERE DCont_Qty_Code_Var='BLDG/CNQT/11-12/101' AND DCont_Contractor_Code_Var='CNTT1001'
Please give me solution.
I would suggest you first check the column definitions against the data youre passing them and them.
What the error is saying (assuming its triggered by that piece of sql) is that there is some number column youre working with but youre passing the data as a string and that string doesnt represent a number. So,
DCont_Discount_Var=23
Are you sure DCont_Discount is a number? (Seems the answer is yes)
DCont_Qty_Code_Var='BLDG/CNQT/11-12/101'
Are you sure DCont_Qty_Code_Var is a string?
DCont_Contractor_Code_Var='CNTT1001'
Are you sure DCont_Contractor is defined as string?
One of these columns is defines as something numeric, i'd wager
My guess is DCont_Discount_Var=23 should be DCont_Discount_Var='23'

Why Does DateTimeConverter.ConvertFrom Work with Empty Strings? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
When doing DateTimeConverter.ConvertFromString(" ") it gives back a DateTime.MinValue. You can have as many blank spaces as you want and it still works. If you give it an empty string, it will return null, which I would expect.
This behavior is different than all the other TypeConverters that will either return null or throw an exception.
This is also different than the behavior of DateTime.TryParse(" ") which returns false meaning it didn't work.
I would expect the ConvertFrom method to behave the same as the TryParse method where it should fail if there is an empty or whitespace string.
Why does it do this? Is this a bug?
I'm doing some type conversion and this is a situation where I'm getting unexpected/inconsistent behavior. Is there a way to reliably do type conversion that works for all types? Or do I need to put a special clause in for the DateTime case?
if( typeConverter is DateTimeConverter )
{
// Check if the value is a white space string.
}

Need a formula interpreter for .Net [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm looking for a formula interpreter that I can use in a C# application. It needs to be able to interpret a string like this:
max(1+2, 4) * x
I found Writing a fast formula interpreter (codeproject.com) which almost does what I need but it doesn't allow for functions with multiple parameters. I could probably add that functionality to it but I was just wondering if something like this already exists.
Thanks
A couple I've used in the past with no problems:
NCalc
Fast Lightweight Expression Evaluator
You can actually build a very effective interpreter by parsing and replacing certain functional keywords such as max with Math.Max and then dynamically building and executing the formula as a C# class and method. So actually you would be parsing and wrapping the formula and allowing the C# compiler to interpret and execute it.
So, taking your sample formula of max(1+2, 4) * x would turn into:
public class MyFormula
{
public double calc(double x)
{
return Math.Max(1+2, 4) * x;
}
}
Which you would compile on the fly and then execute per the linked article. You still have to parse for and pass the x value of course.
A long time ago in one project i had to create some booking with formulas, and i used VsaEngine. To use this engine you need to add reference to Microsoft.JScript. Here is example:
Code for usage is very simple, just replace formula parameters like:
string formula = "x+y";
formula= formula.Replace("x","100").Replace("y","200");
string result = CalculateFormula(formula);
And here is core method, CalculateFormula:
public string CalculateFormula(string evaluationString)
{
VsaEngine en = VsaEngine.CreateEngine();
Object result = Eval.JScriptEvaluate(evaluationString, en);
return result.ToString();
}
With this you can create your custom formula interpreter engine.

Categories

Resources