I have a base class with several classes extending it. I have some generic library utilities that creates a vector containing pointers to the base class so that any of the subclasses will work. How can I cast all of the elements of the vector to a specific child class?
// A method is called that assumes that a vector containing
// Dogs ...
Why can I do this:
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return (T)GetMainContentItem(moduleKey, itemKey);
}
but not this:
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return GetMainContentItem(moduleKey, itemKey) as T;
}
It complains that I haven't restricted the generic t...
I have inherited a large admin winforms application that shares a 'Common' library of classes for holding data with a web service. The problem I am having is that if I return a populated instance of a class from a web service call then it comes out on the client as a different type and I cannot use the other 'Common' project logic to man...
I have a sub that handles when 14 ComboBoxes have their Index changed. I am able to cast the sender of the event, and obtain properties from there. However, after that, I want to be able to change the properties of the actual sender, rather than the cast one. How would I do this?
Current code:
Private Sub ComboBoxIndexChange(ByVal send...
I'm constantly running up against this when writing queries with LINQ-to-XML: the Value property of an XElement is a string, but the data may actually be an integer, boolean, etc.
Let's say I have a "where" clause in my query that checks if an ID stored in an XElement matches a local (integer) variable called "id". There are two ways I ...
Let's say I'm working on a library that works on items of type Item. The main entry point is a class like:
class Worker
{
private:
SomeContainer _c;
public:
void add( const Item &i );
void doSomething();
};
The doSomething() method looks at the added items, compares them, etc. and does something with them. So the Item class ...
I want to work around the fact that my WCF servicelayer can not handle a generic method like this:
public void SaveOrUpdateDomainObject<T>(T domainObject)
{
domainRoot.SaveDomainObject<T>(domainObject);
}
so I built this workaround method instead
public void SaveOrUpdateDomainObject(object domainObject, string typeName)
{ ...
I asked a still unanswered question that will shed more light on this question.
Why can't I do this...
_wizardDialog.UIRoot.Controls.Clear()
_wizardDialog.UIRoot.Controls.Add(TryCast(wizardUserControl, wizardUserControl.GetType))
Why does using GetType in this way fail. The argument for try cast are object and type. Since wizardUse...
Possible Duplicate:
Casting: (NewType) vs. Object as NewType
Say for example I have a class called MyObjectType and I want to convert the sender parameter of an event to this type. I would usually go about it by simply doing this:
MyObjectType senderAsMyType = (MyObjectType) sender;
I've recently realised that it can also be ...
Often, especially in Win32 programming it is required to cast from one opaque type to another. For example:
HFONT font = cast_here<HFONT>( ::GetStockObject( SYSTEM_FONT ) );
Both static_cast and reinterpret_cast are applicable here and have exactly the same effect since HFONT is a pointer to a dummy struct specifically introduced for...
I am consuming a WCF service from another company, and it is returning an object of type object. Is there a reason not to return the actual class, and return an object that must be cast into the correct form?
For example, if the web service returns an object of type OrderStatus, why would you instead return a plain old object?
Correct...
I'm using a single textbox to search a report and filter out records. One of my fields is an int32 and the rest are varchar's. So when the filter tries to compare the string from the textbox with the int32 field, I get an error. Here's the SQLDataSouce and the search box:
<asp:TextBox ID="SearchBox" AutoPostBack="true" OnTextChanged="Se...
I have a object:
ExampleClass ex = new ExampleClass();
And:
Type TargetType
I would like to cast ex to type described by TargetType like this:
Object o = (TargetType) ex;
But when I do this I get:
The type or namespace name 't' could
not be found
So how to do this? Am I missing something obious here?
Update:
I would l...
I am asking for an Value like this:
[self.layer valueForKeyPath:@"transform.rotation.z"]
bit I need to pass it to a method which takes a CGFloat as parameter. What's the casting trick here?
...
There's a special name for it, but I can't remember what it is. There are two different terms, one for casting an array of a subclass to an array of its superclass, and the other way around.
...
I'm wondering if it is necessary to reinterpret_cast in the function below. ITER_T might be a char*, unsigned char*, std::vector<unsigned char> iterator, or something else like that. It doesn't seem to hurt so far, but does the casting ever affect how the bytes are copied at all?
template<class ITER_T>
char *copy_binary(
unsigned ch...
I want to Convert an int to a specific type and then return a string whose format depends on the type I converted it to.
I have a property that returns a Type object, and another that I want to return a string whose format depends on the Type.
Why doesn't the compiler like the code in HexString below?
Is there another equally brief way...
I understand the difference between unsigned char * and char * types. I also understand how to use reinterpret_cast to cast an unsigned char * to a char * in C++.
I'm using sqlite3 in Objective-C and am trying to get an NSString from a call to
sqlite3_column_text(...);
To do this, I'm basically doing:
char *cParam = (char *)sqlite...
In both Microsoft VC2005 and g++ compilers, the following results in an error:
On win32 VC2005: *sizeof(wchar_t) is 2*
wchar_t *foo = 0;
static_cast<unsigned short *>(foo);
Results in
error C2440: 'static_cast' : cannot convert from 'wchar_t *' to 'unsigned short *' ...
On Mac OS X or Linux g++: *sizeof(wchar_t) is 4*
wchar_t *fo...
I saw a few topics on SO about this but none really answered my question so here it is:
String s = "a string";
Object o = s;
s = String(o); // EDIT this line was wrong
s = (String)o; // EDIT Corrected line
Now this compiles fine but throws a ClassCastException. The only thing is that I thought there was some way to make this work. i...