Consider this static test class:
public static class Test
{
public static ushort sum(ushort value1, ushort value2)
{
return value1 + value2
}
}
This causes the following compile error, with value1 + value2 underlined in red:
Cannot implicitly convert type 'int'
to 'ushort'. An explicit conversion
exists (ar...
I am having trouble understanding what the rules are for passing delegates in C#3.0. The final .Max() is giving the compile error as shown below, but I can't understand what the significant difference is between that case and any of the others.
int NamedIdentity(int val) { return val; }
Func<int, int> Identity = x => x;
void Main() {
...
Hi,
I am having some trouble figuring out the syntax for typedef reinterpret_cast.
Can anyone help?
EDIT
What I am trying to do. I am always hesitant as people seem to get caught up in every thing else except what the problem actually is as you can see with my last post which lead to a whole bunch of nothing.
I am trying to come up w...
I have a situation where an end user can enter an XPath to access a value in some XML. I’m using a line of code similar to the one below:
IEnumerable e = (IEnumerable)importDocument.XPathEvaluate(theXPath);
As the Xpath could return an Attribute or an Element, what I need to know is how can I interpret ‘e’ in the above example to deci...
Hi, I have the following scenario:
public class A {
}
public class B extends A {
}
public class C extends B {
public void Foo();
}
I have a method that can return me class A, B or C and I want to cast safely to C but only if the class I get is of type C. I need to call Foo() but I don't want the ClassCastException.
...
If you have a void* pointer to Derived class that inherits from both BaseA and BaseB, how does the compiler cast the void* pointer to BaseA* (or BaseB*) without knowing that the void* pointer is of type Derived?
...
I have a generic method where I want to do something special for Strings.
I've found DirectCast(DirectCast(value, Object), String) to get the String value (when I've already confirmed GetType(T) Is GetType(String)) and DirectCast(DirectCast(newvalue, Object), T) as mentioned in a number of answers to similar questions works.
But is the...
I'm having a heap of trouble with Java coming from a PHP background. I've got a parent class Entity containing generic database methods, such as a static method getById(int id). My aim is to have children of this class, such as Person, so that I can call:
Person p = Person.getById(1);
At the moment this doesn't work, as getById(1) re...
I need a value to be calculated in it's getter method if it isn't set already. I prefer to use a nullable int construction, so I don't need another bool to check if it's already been calculated.
My first hunch was to go for a NSInteger *. I can check if it's NULL and otherwise set a value to it. But I don't know if it's possible, since ...
in java i can cast number to byte
for example
System.err.println((byte)13020);
the result will be
-36
how can i do the same in php ?
...
When trying to answer this question, I discovered the following:
string s = "test";
var result1 = s.Select(c => (ushort)c); // works fine
var result2 = s.Cast<ushort>(); // throws an invalid cast exception
Why does Cast<T>() fail here? Whats the difference?
...
I've got this nifty little piece of code here that self-propogates ad infinitum every time the relevant method is called:
if (this.ListBox_MyListBox.Items[e.Index] is MyObject)
{
MyObject epicObject= new MyObject();
epicObject= (MyObject)this.ListBox_MyListBox.Items[e.I...
Why do I get an InvalidCastException when trying to do this?
throw (ArgumentNullException)(new Exception("errormessage", null));
This is a simplified version of the following function.
public static void Require<T>(bool assertion, string message, Exception innerException) where T: Exception
{
if (!assertion)
{
...
I'trying to convert a float to an int value in php:
var_dump((int)(39.3 * 100.0)); //Returns 3929 but should be 3930!
var_dump((int)(39.2 * 100.0)); //Returns 3920
I can use ceil to make it work but can somebody explain this to me?
var_dump((int)ceil(39.3 * 100.0)); //Returns 3930
...
I'm using a LINQ query to translate data inside a DataTable object to be a simple IEnumerable of a custom POCO object.
My LINQ query is:
Dim dtMessages As DataTable
'...dtMessages is instantiated ByRef in a helper data access routine... '
Dim qry = From dr As DataRow In dtMessages.Rows
Select New OutboxMsg...
Hi, i'm here to get help about a strange behavior of my GLSL code when i cast a float to an int and i never seen such a bug since i started GLSL
Actually i'm trying to achieve mesh skinning on CPU with GLSL
I use an ATI Radeon HD 4850 (Gainward) and i work with OpenGL 2.1 on Windows XP
so on CPU side I gather bones indices and weights...
I have an array of type object which are strings.
I would like to convert them to strings
what would be the quickest way of doing so.
Eg.
I have this "object[]" and want to convert it so it is this "string[]"
UPDATE -
i think the problem is that some of the objects on the object[] are actually other objects like integers. I would ...
In C# you can implicite concat string and let's say, an integer:
string sth = "something" + 0;
My questions are:
Why, by assuming fact you can implicite concat string and int, C# disallow initializing string as:
string sth = 0; // Error: Cannot convert source type 'int' to target type 'string'
How C# casts 0 as string. Is it 0.ToSt...
To add const to a non-const object, which is the prefered method? const_cast<T> or static_cast<T>. In a recent question, someone mentioned that they prefer to use static_cast, but I would have thought that const_cast would make the intention of the code more clear. So what is the argument for using static_cast to make a variable const?
...
While browsing the MSDN documentations on Equals overrides, one point grabbed my attention.
On the examples of this specific page, some null checks are made, and the objects are casted to the System.Object type when doing the comparison :
public override bool Equals(System.Object obj)
{
// If parameter is null return false.
if ...