casting

Use of IsAssignableFrom and "is" keyword in C#

While trying to learn Unity, I keep seeing the following code for overriding GetControllerInstance in MVC: if(!typeof(IController).IsAssignableFrom(controllerType)) { ... } this seems to me a pretty convoluted way of basically writing if(controllerType is IController) { ... } I appreciate there are subtle differences between is and...

Converting 'x' value of a CGPoint ivar to NSTimeInterval?

Hi I have a velocity ivar defined as a CGPoint. I need to somehow extract just the 'x' value of velocity, and then use this to call-send a message to the following method signature -(void) adjustTimer:(NSTimeInterval*)newInterval How do I obtain just the 'x' value of a CGPoint? Do I then need to convert or cast this result before ca...

casting BSTR as char* in a dll; different results depnding on VB/C# caller.

I have a dll function that takes BSTR parameters. These are casted as char* before being used for other things. When the dll is called from VB code this works fine. However, when it is called from C# code, only the first character is pointed to. Both of these are excel addIns for Pre-2007 and 2007+ versions of Office, which call into a...

Initialization discards qualifiers from pointer target type

I want to read a file, line by line and then assign to each line a variable. I have the following code: NSString *aFilePath= [[NSString alloc] initWithContentsOfFile:@"db.def"]; NSArray *lines = [aFilePath componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; char *server = [[lines objectAtIndex:0] UTF8Str...

Convert OracleParameter.Value to Int32

I have a stored procedure call that goes like this: using (OracleConnection con = new OracleConnection(ConfigurationManager.AppSettings["Database"])) using (OracleCommand cmd = new OracleCommand("Package.Procedure", con)) { Int32 existsCount; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("successCount", Ora...

How to get rid of void-pointers.

I inherited a big application that was originally written in C (but in the mean time a lot of C++ was also added to it). Because of historical reasons, the application contains a lot of void-pointers. Before you start to choke, let me explain why this was done. The application contains many different data structures, but they are stor...

What is the purpose of the const_cast<> on a volatile?

I saw it was possible to do it but I do not understand the interest. ...

Should I add try/catch around when casting on an attribute of JSP implicit object?

Hi all: Basically what I mean is like this: List<String[]> routes = (List<String[]>)application.getAttribute("routes"); For the above code, it tries to get an attribute named "routes" from the JSP implicit object - application. But as everyone knows, after this line of code, routes may very well contains a null - which means this app...

C#; On casting to the SAME class that came from another assembly

For complete separation/decoupling, I've implemented a DAL in an assebly that is simply being copied over via post-build event to the website BIN folder. The website then on Application Start loads that assembly via System.Reflection.Assembly.LoadFile. Again, using reflection, I construct a couple of instances from classes in that assemb...

Printing the address of a struct object

I have a struct like this typedef struct _somestruct { int a; int b; }SOMESTRUCT,*LPSOMESTRUCT; I am creating an object for the struct and trying to print it's address like this int main() { LPSOMESTRUCT val = (LPSOMESTRUCT)malloc(sizeof(SOMESTRUCT)); printf("0%x\n", val); return 0; } ..and I get this warning ...

ksoap2 casting getResponse()

Calling a .net SOAP1.1 web service from android using ksoap2 lib I met a problem of casting response to a custom object. For instance the code below is called correct after httpTransport.call(soapAction, soapEnvelope); and have data inside. But I cant't cast it to specific object neither to SoapObject or Vector as I saw in several exampl...

casting size_t to int to declare size of char array

I am trying to declare a size of a char array and I need to use the value of the variable that is declared as a size_t to declare that size. Is there anyway I can cast the size_t variable to an int so that I can do that? ...

Is C++ explicit conversion really that bad?

My knowledge of C++ at this point is more academic than anything else. In all my reading thus far, the use of explicit conversion with named casts (const_cast, static_cast, reinterpret_cast, dynamic_cast) has come with a big warning label (and it's easy to see why) that implies that explicit conversion is symptomatic of bad design and s...

Should I cast variables that use a typdef'd type?

If I have something like: typedef int MyType; is it good practice to cast the operands of an operation if I do something like this: int x = 5; int y = 6; MyType a = (MyType)(x + y); I know that I don't need to do that but wondering if it's better for intent/documentation/readability concerns. Or, should I just do: MyType a = x + ...

casting between sibling classes, AS3

I have two classes, derivedClassA and derivedClassB which both extend parentClass I'm declaring var o:parentClass and then, depending on what's going on, I want to cast o as either being of type derivedClassA or derivedClassB. Essentially, this: var o:parentClass ... if(shouldUseA) o = new derivedClassA(); else o ...

type casting in mysql

i have passportno(varchar) in database. i am entering values like this 001,002,003. and i want to display like sorting order. now i wrote query like this "select * from passport_registration where status=1 ORDER BY passportno" then displaying output like this......077,088,099,100,1000,1001,1009,101,1010 i want to diplay sort order. ...

Is there a way to avoid throwing/catching exceptions when validating Textboxes used for number entry?

Hey all, In the pursuit of elegant coding, I'd like to avoid having to catch an exception that I know well may be thrown when I try to validate that the Text field of a Textbox is an integer. I'm looking for something similar to the TryGetValue for Dictionary, but the Convert class doesn't seem to have anything to offer except exception...

Dynamic Casting based on Type information

I want to do an explicit cast using Type information from one array to another which is related by inheritance. My problem is that while casting using Type information the compiler throws error, but my requirement is to dynamically cast based on the Type information provided. Please Help class Program { static void Main(string[] ...

Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast

So if your converting from Void* to Type* or from Type* to Void* should you use: void func(void *p) { Params *params = static_cast<Params*>(p); } or void func(void *p) { Params *params = reinterpret_cast<Params*>(p); } To me static_cast seems the more correct but I've seen both used for the same purpose. Also, does the dir...

enable_if and conversion operator?

Any chance to use enable_if with a type conversion operator? Seems tricky, since both return type and parameters list are implicit. ...