casting

Can I cast std::vector<Animal*> to std::vector<Dog*> without looking at each element?

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 does "as T" get an error but casting with (T) not get an error?

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...

How do I use a common class between winforms client and web service?

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...

Change properties of an unknown object in VB.NET

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...

When querying with LINQ-to-XML, is it better/more efficient to leave element values as strings or convert them to the correct type?

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 ...

Implementing comparison using a generic interface in C++

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 ...

c# casting to type gotten from typename as string

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) { ...

Why can't you use GetType when casting?

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...

Should I use (ObjectType) or 'as ObjectType' when casting in c#?

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 ...

Which one to use when static_cast and reinterpret_cast have the same effect?

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...

Should WCF services return plain old objects, or the actual class that you're working with?

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...

How can I do a cast in a SqlDataSource FilterExpression?

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...

How to cast object to type described by Type class?

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...

How to get this value as a CGFloat?

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? ...

What's it called when you cast an array of objects to an array of strings?

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. ...

reinterpret casting to and from unsigned char* and char*

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...

C# ChangeType and ToString?

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...

Defined behavior of cast from unsigned char * to char * in Objective-C

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...

static_cast wchar_t* to int* or short* - why is it illegal?

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...

Casting objects in Java

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...