casting

How are integers converted to strings under the hood?

I suppose the real question is how to convert base2/binary to base10. The most common application of this would probably be in creating strings for output: turning a chunk of binary numerical data into an array of characters. How exactly is this done? my guess: Seeing as there probably isn't a string predefined for each numerical value...

Help with InvalidCastException

I have a gridview and, when a record is double-clicked, I want it to open up a new detail-view form for that particular record. As an example, I have created a Customer class: using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.ComponentModel; using System.Data.SqlClient; using Sy...

Casting objects in C# (ASP.Net MVC)

I'm coming from a background in ColdFusion, and finally moving onto something modern, so please bear with me. I'm running into a problem casting objects. I have two database tables that I'm using as Models - Residential and Commercial. Both of them share the majority of their fields, though each has a few unique fields. I've create...

formatting and converting in java

I have few small basic problems : How to format : int i = 456; to give output : ""00000456" ? I've tried %08d but it's not working. Next thing is a problem with conversion and then formatting. I have side and height of triangle, let's say int's 4,7, and 7 is the height. From formula for field we know that F=1/2(a*h). So how to get...

C++ ulong to class method pointer and back

Hi guys, I'm using a hash table (source code by Google Inc) to store some method pointers defined as: typedef Object *(Executor::*expression_delegate_t)( vframe_t *, Node * ); Where obviously "Executor" is the class. The function prototype to insert some value to the hash table is: hash_item_t *ht_insert( hash_table_t *ht, ulong ke...

Typed DefaultListModel to avoid casting

Is there a way in java to have a ListModel that only accepts a certain type? What I'm looking for is something like DefaultListModel<String> oder TypedListModel<String>, because the DefaultListModel only implements addElement(Object obj) and get(int index) which returns Object of course. That way I always have to cast from Object to e....

Cast then check or check then cast?

Which method is regarded as best practice? Cast first? public string Describe(ICola cola) { var coke = cola as CocaCola; if (coke != null) { string result; // some unique coca-cola only code here. return result; } var pepsi = cola as Pepsi; if (pepsi != null) { string result; ...

Explanation of casting/conversion int/double in C#

I coded some calculation stuff (I copied below a really simplifed example of what I did) like CASE2 and got bad results. Refactored the code like CASE1 and worked fine. I know there is an implicit cast in CASE 2, but not sure of the full reason. Any one could explain me what´s exactly happening below? //CASE 1, result 5.5 double a...

Storing a type in C++

Is it possible to store a type name as a C++ variable? For example, like this: type my_type = int; // or string, or Foo, or any other type void* data = ...; my_type* a = (my_type*) data; I know that 99.9% of the time there's a better way to do what you want without resorting to casting void pointers, but I'm curious if C++ allows this...

Casting problems with Google Maps API

Hi there, I'm trying to run the following line: Directions.loadFromWaypoints((Waypoint[])waypoints.toArray(), opts); But I'm getting: 23:41:44.595 [ERROR] [carathome] Uncaught exception escaped java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.google.gwt.maps.client.geocode.Waypoint; at com.presasystems.g...

Java; casting base class to derived class

Why can't I cast a base class instance to a derived class? For example, if I have a class B which extends a class C, why can't I do this? B b=(B)(new C()); or this? C c=new C(); B b=(B)c; Alright let me be more specific as to what I'm trying to do. Here's what I have: public class Base(){ protected BaseNode n; public vo...

Does (size_t)((char *)0) ever not evaluate to 0?

According to the responses in "Why subtract null pointer in offsetof()?" (and my reading of K&R), the C standard doesn't require that (size_t)((char *)0) == 0. Still, I've never seen a situation where casting a null pointer to an integer type evaluates to anything else. If there is a compiler or scenario where (size_t)((char *)0) != 0, ...

How often do you create implicit conversions for your classes?

Hey there, I've been developing .NET applications for 4 years. So far, I did not need to create any implicit conversions for the classes I authored. Could you provide real-life situations when you could not do without creating implicit conversions? Thank you ...

Casting good practice

Hi, I've got 3 classes namespace ServerPart { public class Car { } public class SUV:Car { public string Name {get;set;} public string Color {get;set;) } } And namespace WebSericePart { public class Car { } } namespace WebSericePart.Car { public class SUV { ...

Creating a simple VS2008 visualizer inside autoexp.dat (problem with casting).

I have a large project of mixed C/C++. I've created a simple visualizer for the ICU UnicodeString class as follows... [inside autoexp.dat] icu_4_2::UnicodeString { preview ([$c.fUnion.fFields.fArray,su]) } ...and that works fine. Inside the debugger wherever I see the object I now see the text inside in the preview line. ...

How to return an IQueryable<Something> as an IQueryable<ISomething>

I have a class Something that implements ISomething. How can I convert/cast from an IQueryable<Something> to an IQueryable<ISomething>. When I try to cast, I am able to compile, but the result of the cast is always NULL. Background: The reason I am doing this is because my Something class is a CodeSmith-generated class (PLINQO templa...

C# cast Foo<Bar> to Foo<object>

Hi, does anyone know if it is possible to cast a generic type with a certain type parameter (e.g. Bar) to the same generic type with the type parameter being a base type of Bar (such as object in my case). And, if it is possible, how would it be done? What I want to do is have a collection of Foo<object> but be able to add Foos with mor...

Using a function with reference as a function with pointers?

Today I stumbled over a piece of code that looked horrifying to me. The pieces was chattered in different files, I have tried write the gist of it in a simple test case below. The code base is routinely scanned with FlexeLint on a daily basis, but this construct has been laying in the code since 2004. The thing is that a function implem...

C++ Class Inheritance architecture - preventing casting

I have a structure of base class and a couple of inherited classed. Base class should be pure virtual class, it should prevent instantiation. Inherited classes can be instantiated. Code example below: class BaseClass { public: BaseClass(void); virtual ~BaseClass(void) = 0; }; class InheritedClass : public BaseClass { public: ...

Question about member function pointers in a heirarchy

I'm using a library that defines an interface: template<class desttype> void connect(desttype* pclass, void (desttype::*pmemfun)()); and I have a small heirarchy class base { void foo(); }; class derived: public base { ... }; In a member function of derived, I want to call connect(this, &derived::foo); but it seems that &der...