Questions tagged [casting]

Casting is a process where an object type is explicitly converted into another type if the conversion is allowed. This process might lead to a change in value.

Some algorithms have significant performance differences for different data-types, for example, calculating the N-dimensional median is much is much faster for integers than float type data. Therefore, the use of casting can be important for code performance.

19752 questions
169
votes
1 answer

Why does "The C Programming Language" book say I must cast malloc?

Today I reached page 167 of The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) and found that the author says I must cast malloc. Here is the part from the book: 7.8.5 Storage Management The functions malloc and…
Michi
  • 5,175
  • 7
  • 33
  • 58
167
votes
14 answers

How do I cast a string to integer and have 0 in case of error in the cast with PostgreSQL?

In PostgreSQL I have a table with a varchar column. The data is supposed to be integers and I need it in integer type in a query. Some values are empty strings. The following: SELECT myfield::integer FROM mytable yields ERROR: invalid input syntax…
silviot
  • 4,615
  • 5
  • 38
  • 51
167
votes
6 answers

Convert base class to derived class

Is it possible in C# to explicitly convert a base class object to one of it's derived classes? Currently thinking I have to create a constructor for my derived classes that accept a base class object as a parameter and copy over the property values.…
ARW
  • 3,306
  • 7
  • 32
  • 41
166
votes
10 answers

C# "as" cast vs classic cast

Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR I recently learned about a different way to cast. Rather than using SomeClass someObject = (SomeClass) obj; one can use this syntax: SomeClass someObject = obj as SomeClass; which…
Chris
  • 9,209
  • 16
  • 58
  • 74
166
votes
11 answers

How to cast Object to its actual type?

If I have: void MyMethod(Object obj) { ... } How can I cast obj to what its actual type is?
Paul Lassiter
  • 2,641
  • 5
  • 22
  • 25
165
votes
1 answer

How do I convert between numeric types safely and idiomatically?

Editor's note: This question is from a version of Rust prior to 1.0 and references some items that are not present in Rust 1.0. The answers still contain valuable information. What's the idiomatic way to convert from (say) a usize to a u32? For…
Caspar
  • 7,039
  • 4
  • 29
  • 41
161
votes
7 answers

How should I cast in VB.NET?

Are all of these equal? Under what circumstances should I choose each over the others? var.ToString() CStr(var) CType(var, String) DirectCast(var, String) EDIT: Suggestion from NotMyself… TryCast(var, String)
Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
160
votes
9 answers

What is the difference between up-casting and down-casting with respect to class variable

What is the difference between up-casting and down-casting with respect to class variable? For example in the following program class Animal contains only one method but Dog class contains two methods, then how we cast the Dog variable…
Dhivakar
  • 2,081
  • 5
  • 15
  • 18
159
votes
10 answers

Most efficient way to cast List to List

I have a List that I want to treat as a List. It seems like it shouldn't be a problem since casting a SubClass to a BaseClass is a snap, but my compiler complains that the cast is impossible. So, what's the best way to get a…
Riley Lark
  • 20,660
  • 15
  • 80
  • 128
156
votes
6 answers

No need to cast the result of findViewById?

Recently I found that AndroidStudio reminds me to remove some class cast. I remember that in the old time, we have to cast the result of findViewById, but now it's not necessary. The result of findViewById is still View, so i want to know why we…
Eric Zhao
  • 1,704
  • 2
  • 12
  • 14
154
votes
11 answers

TypeScript: Index signature is missing in type

I want MyInterface.dic to be like a dictionary name: value, and I define it as follows: interface MyInterface { dic: { [name: string]: number } } Now I create a function which waits for my type: function foo(a: MyInterface) { ... } And the…
Manu Artero
  • 9,238
  • 6
  • 58
  • 73
152
votes
8 answers

Cast int to varchar

I have below query and need to cast id to varchar Schema create table t9 (id int, name varchar (55)); insert into t9( id, name)values(2, 'bob'); What I tried select CAST(id as VARCHAR(50)) as col1 from t9; select CONVERT(VARCHAR(50),id) as colI1…
Mario
  • 2,303
  • 5
  • 20
  • 21
151
votes
9 answers

PHP String to Float

I am not familiar with PHP at all and had a quick question. I have 2 variables pricePerUnit and InvoicedUnits. Here's the code that is setting these to values: $InvoicedUnits = ((string) $InvoiceLineItem->InvoicedUnits); $pricePerUnit = ((string)…
John
147
votes
10 answers

Cast List to List

public interface IDic { int Id { get; set; } string Name { get; set; } } public class Client : IDic { } How can I cast List to List?
Andrew Kalashnikov
  • 3,073
  • 5
  • 34
  • 57
147
votes
5 answers

Why does an NSInteger variable have to be cast to long when used as a format argument?

NSInteger myInt = 1804809223; NSLog(@"%i", myInt); <==== The code above produces an error: Values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead The corrected NSLog message is actually…
Daniel Lee
  • 1,501
  • 2
  • 10
  • 5