implicit-cast

Implicit casting in VB.NET

孤人 提交于 2019-12-12 21:05:12
问题 The question is intended for lazy VB programmers. Please. In vb I can do and I won't get any errors. Example 1 Dim x As String = 5 Dim y As Integer = "5" Dim b As Boolean = "True" Example 2 Dim a As EnumType = 4 Dim v As Integer = EnumType.EnumValue Example 3 Private Sub ButtonClick(sender As Object, e As EventArgs) Dim btn As Button = sender End Sub Example 4 Private Sub ButtonClick(sender As Button, e As EventArgs) Dim data As Contact = sender.Tag End Sub If I surely know the expected

Trigger cast operator on use of the dot operator

我怕爱的太早我们不能终老 提交于 2019-12-11 04:56:03
问题 I have a class something like this: template<typename T> class wrapper { public: operator const T & () const { return value; } private: T value; }; I then use it with a struct like this: struct point { float x; float y; }; //... wrapper<point> myPoint; std::cout << myPoint.x;// error: no member x or whatever. I'm wondering if there's a way to allow this without having to do ((point)myPoint).x. I know that I can overload the -> operator but I'd prefer not to since its supposed to "pretend" to

Strange casting behavior in mysql

两盒软妹~` 提交于 2019-12-10 20:37:17
问题 Here is the code mysql> SELECT id FROM tbl WHERE id = '1h'; +----+ | id | +----+ | 1 | +----+ 1 row in set There is indeed a field with id 1 (but not '1h'). Here is an extraction from MySQL docs: http://dev.mysql.com/doc/refman/5.1/en/type-conversion.html mysql> SELECT 1 > '6x'; -> 0 mysql> SELECT 7 > '6x'; -> 1 So this bug is documented, so to say. The question is what's the reason for such behavior and how to correct it to make this not cast strings with char symbols? I can cast all field

Short hand assignment operator, +=, True Meaning?

眉间皱痕 提交于 2019-12-10 14:04:24
问题 I learnt that i+=2 is the short-hand of i=i+2 . But now am doubting it. For the following code, the above knowledge holds no good: byte b=0; b=b+2; //Error:Required byte, Found int The above code is justifiable, as 2 is int type and the expression returns int value. But, the following code runs fine: byte b=0; b+=2; //b stores 2 after += operation This is forcing me to doubt that the += short-hand operator is somewhat more than I know. Please enlighten me. 回答1: When in doubt, you can always

Implicit casting Integer calculation to float in C++

给你一囗甜甜゛ 提交于 2019-12-09 21:20:01
问题 Is there any compiler that has a directive or a parameter to cast integer calculation to float implicitly. For example: float f = (1/3)*5; cout << f; the "f" is "0", because calculation's constants(1, 3, 10) are integer. I want to convert integer calculation with a compiler directive or parameter. I mean, I won't use explicit casting or ".f" prefix like that: float f = ((float)1/3)*5; or float f = (1.0f/3.0f)*5.0f; Do you know any c/c++ compiler which has any parameter to do this process

implicit operator

北慕城南 提交于 2019-12-09 14:11:19
问题 I just saw it was using in one of the recent answers: public static implicit operator bool(Savepoint sp) { return sp != null; } Why do we need word implicit here, and what does it mean? 回答1: Implicit means that the conversion doesn't require a cast in your code. You can now do this: Savepoint point = new Savepoint(); if(point) // becomes a bool using your operator { } instead of having to do this: Savepoint point = new Savepoint(); if((bool)point) // an "explicit" conversion { } One example

Is there a way to disable implicit casts from UInt32 to char?

旧巷老猫 提交于 2019-12-08 20:43:05
问题 I am working on code that takes as input a ton of ascii text defined by specific protocol. The original author interpreted "string(1)" datatypes in the original protocol as chars in the code. There have been a lot subtle bugs in corner cases where you have code such as: char theChar = whatever(); if(theChar == 7) {...} where was was really meant was: if(theChar == '7') {...} In order to attempt to catch all of these at once, is there a way to disable the implicit casting to 'char'? If not,

Auto-(un)boxing fail for compound assignment

让人想犯罪 __ 提交于 2019-12-08 16:37:19
问题 Thanks to the implicit casting in compound assignments and increment/decrement operators, the following compiles: byte b = 0; ++b; b++; --b; b--; b += b -= b *= b /= b %= b; b <<= b >>= b >>>= b; b |= b &= b ^= b; And thanks to auto-boxing and auto-unboxing, the following also compiles: Integer ii = 0; ++ii; ii++; --ii; ii--; ii += ii -= ii *= ii /= ii %= ii; ii <<= ii >>= ii >>>= ii; ii |= ii &= ii ^= ii; And yet, the last line in the following snippet gives compile-time error: Byte bb = 0;

How do i cast A to object to class A when B can typcast to A?

孤者浪人 提交于 2019-12-08 04:35:55
问题 Basically i want to do this. aa causes a bad cast exception. NOTE: o can be ANYTHING. It may not be B, it can be C, D, E, F etc. But this should work as long as o is a class that can typecast into A (B is such a class. It uses an implicit operator overload) var b = (B)"sz"; var a = (A)b; object o = b; var aa = (A)o; 回答1: Have you tried doing the following? [...] var ee = (A)(B)o; The reason this will work and your code doesn't is that such explicit casts are statically compiled. In other

Implicit cast operator and the equality operator

雨燕双飞 提交于 2019-12-07 14:47:29
问题 Say I have a simple object which supports implicit casting to System.String public sealed class CompanyCode { public CompanyCode(String value) { // Regex validation on value format _value = value; } private readonly String _value; public override String ToString() { return _value; } static public implicit operator String(CompanyCode code) { if(code == null) return null; return code.ToString(); } } Now lets say in another part of my program I perform a comparison with a string: var companyCode