I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast is more efficient than using dynamic cast.
Example:
#include <iostream>
#include <typeinfo>
using namespace std;
class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};
int ...
Example code:
#include <cstdlib>
#include <iostream>
using namespace std;
class A {
public:
A(int x, int y) : x(x), y(y) {}
int x, y;
};
class B {
public:
operator A() {
return A(x,y);
}
float x, y;
};
void func1(A a) {
cout << "(" << a.x << "," << a.y << ")" << endl;
}
void func2(A *a, int len) {
...
Consider the following sample code:
class SampleClass
{
public long SomeProperty { get; set; }
}
public void SetValue(SampleClass instance, decimal value)
{
// value is of type decimal, but is in reality a natural number => cast
instance.SomeProperty = (long)value;
}
Now I need to do something similar through reflection:
...
I'm probably missing something very basic but I cannot figure out why I get a compilation error with a certain code and I don't get it in an almost identical code.
So I do get an error here:
//parent.GetChildren() returns a IEnumerable<IBase>
F1<T>(T parent, Func<string, T, string> func) where T: IBase
{
F1(parent.GetChildren(), ...
I get an InvalidCastException converting a linq entity list to a businessobject list using the .Cast<> operator.
"Unable to cast object of type 'Ticketing.ticket' to type
'Ticketing.ModelTicket'." (namespace name was changed because underscore was causing unneeded formatting)
here's my business object class
public sealed class ModelT...
Is there a way in C# to cast an object based on a string?
Example,
String typeToCast = control.GetType().Name;
Button b = (typeToCast)control;
...
I've been trying to do this for a good few hours now and this is as far as I have got
var castItems = typeof(Enumerable).GetMethod("Cast")
.MakeGenericMethod(new Type[] { targetType })
.Invoke(null, new object[] { items });
This returns me
System.Linq.Enumerable+d__aa`1[MyObjectType]
whereas ...
I have the following classes defined to do validation:
public class DefValidator : IValidate<IDef>
{
}
public interface IDef : IAttribute
{
}
Then, I have a list of validators defined as so:
IList<IValidate<IAttribute>> ValidationObjects;
When I try the following, it doesn't compile saying it can't convert types.
DefValidator def...
How do I provide support for casting my class to other types? For example, if I have my own implementation of managing a byte[], and I want to let people cast my class to a byte[], which will just return the private member, how would I do this?
Is it common practice to let them also cast this to a string, or should I just override ToStr...
There was recently a question asking whether it was a good idea in Java to assign the results of calling a getter to a local variable to avoid multiple calls to the same accessor. I can't find the original post but the consensus seemed to be that this generally isn't necessary as Hotspot will optimise away the method call overhead anywa...
// The Structure of the Container and the items
public interface IContainer <TItem> where TItem : IItem
{
}
public class AContainer : IContainer<ItemA>
{
}
public interface IItem
{
}
public class ItemA : IItem
{
}
// Client app
[Test]
public void Test ()
{
IContainer<IItem> container = new AContainer();
}
Question : In test t...
Hello!
I got a few stored procedures that uses a TRY/CATCH statement, so i execute the main program and if it generates any errors i catch them. Now my problem is in the catch statement, i have this piece of code:
BEGIN TRY
INSERT INTO ContentTypes (ContentName, ContentPath) VALUES (@ContentName, @ContentPath)
SET @QRe...
i am converting some Int16's to float and then back again.
and some int32 's to float and back again
im just using a straight cast, but doing this quite a few times per second. (44100 any guesses what its for? :) )
is a cast efficient, can it be done any faster?
ps compile for thumb is turned off
...
Hello,
I am trying to compare two lists of string in python. Some of the strings are numbers however I don't want to use it as number, only for string comparison.
I read the string from a file and put them on a list like this:
def main():
inputFileName = 'BateCarteira.csv'
inputFile = open(inputFileName, "r")
bankNumbers ...
Using V1.8 z/OS XL C compiler, with warnings jacked-up using INFO(ALL), I get the following warning on line 4 of the code below:
WARNING CCN3196 Initialization between types "const int** const" and "int**"
is not allowed.
1 int foo = 0;
2 int *ptr = &foo;
3 const int * const fixed_readonly_ptr = ptr;
4 const int...
My question has to do with casting classes inside a generic type. While I realize that casting objects like List<string> to List<object> requires support for covariance to prevent adding object objects to a list that contains strings, I am wondering why a cast like below is not accepted by the compiler and whether it is solvable employin...
EDIT!!! - The casting of the value to a string seems to work fine when I create a new object, but when I try to edit an existing object, it does not allow it.
So I have a decimal field in one of my models of Decimal(3,2)
When I query up all these objects and try to set this field
fieldName = 0.85
OR
fieldName = .85
It will throw a ...
I was taken aback earlier today when debugging some code to find that something like the following does not throw a compile-time exception:
public Test () {
HashMap map = (HashMap) getList();
}
private List getList(){
return new ArrayList();
}
As you can imagine, a ClassCastException is thrown at runtime, but can someone ...
Hello friendly Flashers...
I have a movieClip with a button that I created inside of my display class Thumbnail.as and I have a button function that interacts with it inside of my ui class ThumbnailController.as.
The current problem I'm having is that; in my ui class I can't target the movieClip playGlow which was created inside of m...
I created the following property, which threw an InvalidCastException if the getter was accessed when ViewState[TOTAL_RECORD_COUNT] was null.
public long TotalRecordCount
{
get { return (long)(ViewState[TOTAL_RECORD_COUNT] ?? -1); }
set { ViewState[TOTAL_RECORD_COUNT] = value; }
}
My thought is that it incorrectly attempted to...