C# - Datetime convertion and GetDayOfYear usage fails - datetime

There's the code:
HebrewCalendar Heb = new HebrewCalendar();
DateTime tmp = new DateTime(1964,2,3);
MessageBox.Show(Heb.GetDayOfYear(tmp));
it's very basic and simple, but yet - i get an errors:
Error 1 The best overloaded method match for System.Windows.Forms.MessageBox.Show(string)' has some invalid arguments..
Error 2 Argument 1: cannot convert from 'int' to 'string'
what is the problem?

I'm not familiar with HebrewCalendar, but given the error message, I'd say that GetDayOfYear is returning an integer.
Try this:
MessageBox.Show(Heb.GetDayOfYear(tmp).ToString());
MessageBox.Show doesn't know how to deal with integers. If you convert it to a string first, it will show you the string representation.

Related

Create correct Criteria Operator

Trying to create a Criteria.Parse operator when I have to convert the string field to an Int.
Operation fails at the follwing:
Message=Parser error at line 0, character 15: syntax error;
("Convert.ToInt16{FAILED HERE}(awayML)>130")
Here is my code:
XPCollection collection = new XPCollection(session1, typeof(TodaysGame), CriteriaOperator.Parse("Convert.ToInt16(awayML)>130"));
int ct = collection.Count;
How do I form the Criteria using the Convert.ToInt16 function?
Criteria operators have their own syntax to convert string literals to int values. You need to use them instead of system Convert.ToInt function:
Function
Description
Example
ToInt(Value)
Converts Value to an equivalent 32-bit signed integer.
ToInt([Value])
ToLong(Value)
Converts Value to an equivalent 64-bit signed integer.
ToLong([Value])
You can check the full reference of DevExpress criteria syntax here
The correct way to build a Criteria like that would be:
CriteriaOperator.Parse("ToInt([awayML]) > 130");

Conversion from type 'TimeSpan' to type 'String' is not valid

' >
System.InvalidCastException: Conversion from type 'TimeSpan' to type 'String' is not valid
' >
I have a gridview , i want that the column"duréeCalculée" take like this value "text='<%# (TimeSpan.Parse(Eval("heure_retour") - Eval("heure_depart"))).ToString()%>'"
but when my page generate I got this problem(Conversion from type 'TimeSpan' to type 'String' is not valid)
Can U help Me?
I'm uncertain with this little information that anyone will be able to help you. It would seem that TimeSpan.Spare() is returning a structure of type TimeSpan. When you try to use .ToString(), your compiler isn't figuring out what to do because .ToString() isn't recognised as a conversion method for types TimeSpan.
Most likely what you need to do is get the element TimeSpan that you want and then parse it. Something along the lines of
double Element1 = TimeSpan.Element1;
char str[64];
sprintf(str,"%f",Element1);
Where Element1 is the name of the element you wish to retrieve.

Conversion from string "0.##" to type 'Integer' is not valid

i am doing a simple conversion from decimal to string and stripping trailing zeros like so:
argCat.ToString("0.##")
however, i keep getting the following error:
Conversion from string "0.##" to type 'Integer' is not valid.
am i missing something?
This would happen if argCat is of a type that does not have a ToString() overload that accepts a parameter.
In such a case, your code is parsed as ToString()("0.##"); the "0.##" becomes an argument to the indexer in the String returned by ToString().
You then get this misleading error because that indexer takes an int, not a string.
string str = String.Format("{0:C}", argCat);

Why does casting a Date to a Date in ActionScript fail?

In ActionScript, I've discovered, casting a Date to a Date and assigning it to a Date-typed variable throws a TypeError:
var date : Date = Date(new Date(2012, 01, 01));
Error #1034: Type Coercion failed: cannot convert "Wed Aug 22 17:06:54 GMT+1000 2012" to Date.
This is obviously wrong, but I'd like to know why it happens. My theory is that the Date cast, like the Number cast, has been overridden to attempt to convert the given type rather than just cast it.
Interestingly, casting anything else to a Date and assigning it to a Date also fails:
var date : Date = Date("1/2/3");
var date : Date = Date(123);
// (Both fail)
But assigning it to an Object succeeds:
var object : Object = Date(new Date(2012, 01, 01));
var object : Object = Date("1/2/3");
var object : Object = Date(123);
// (All succeed)
AS3 can be very confusing and inconsistent at times.
Basically you're not casting anything in that code sample.
AS3 has some global camelCased functions that will take precedence over casting operators.
Vector also has a similar global function.
When you do Date(bla) without the new operator, it apparently creates a string representation of that date... Try to cast with the as operator instead.
Typically you should get a compiler warning about this behaviour, if the compiler argument
<!-- Invalid Date cast operation. -->
<warn-bad-date-cast>true</warn-bad-date-cast>
exists in your flex-config.xml.

What's wrong with groovy math?

This seems quite bizarre to me and totally putting me on the side of people willing to use plain java. While writing a groovy based app I encountered such thing:
int filesDaily1 = (item.filesDaily ==~ /^[0-9]+$/) ?
Integer.parseInt(item.filesDaily) : item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
def filesDaily = (item.filesDaily ==~ /^[0-9]+$/) ?
Integer.parseInt(item.filesDaily) : item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
So, knowing that item.filesDaily is a String with value '1..*' how can it possibly be, that filesDaily1 is equal to 49 and filesDaily is equal to 1?
What's more is that when trying to do something like
int numOfExpectedEntries = filesDaily * item.daysToCheck
an exception is thrown saying that
Cannot cast object '111' with class 'java.lang.String' to class 'int'
pointing to that exact line of code with multiplication. How can that happen?
You're assigning this value to an int:
item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
I'm guessing that Groovy is converting the single-character string "1" into the char '1' and then taking the Unicode value in the normal char-to-int conversion... so you end up with the value 49.
If you want to parse a string as a decimal number, use Integer.parseInt instead of a built-in conversion.
The difference between filesDaily1 and filesDaily here is that you've told Groovy that filesDaily1 is meant to be an int, so it's applying a conversion to int. I suspect that filesDaily is actually the string "1" in your test case.
I suspect you really just want to change the code to something like:
String text = (item.filesDaily ==~ /^[0-9]+$/) ? items.filesDaily :
item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
Integer filesDaily = text.toInteger()
This is a bug in the groovy type conversion code.
int a = '1'
int b = '11'
return different results because different converters are used. In the example a will be 49 while b will be 11. Why?
The single-character-to-int conversion (using String.charAt(0)) has a higher precedence than the integer parser.
The bad news is that this happens for all single character strings. You can even do int a = 'A' which gives you 65.
As long as you have no way of knowing how long the string is, you must use Integer.parseInt() instead of relying on the automatic type conversion.

Resources