Posts

Showing posts with the label Casting

Cast Object To Interface In TypeScript

Answer : There's no casting in javascript, so you cannot throw if "casting fails". Typescript supports casting but that's only for compilation time, and you can do it like this: const toDo = <IToDoDto> req.body; // or const toDo = req.body as IToDoDto; You can check at runtime if the value is valid and if not throw an error, i.e.: function isToDoDto(obj: any): obj is IToDoDto { return typeof obj.description === "string" && typeof obj.status === "boolean"; } @Post() addToDo(@Response() res, @Request() req) { if (!isToDoDto(req.body)) { throw new Error("invalid request"); } const toDo = req.body as IToDoDto; this.toDoService.addToDo(toDo); return res.status(HttpStatus.CREATED).end(); } Edit As @huyz pointed out, there's no need for the type assertion because isToDoDto is a type guard, so this should be enough: if (!isToDoDto(req.body)) { throw new Error("invalid...

Cast Datagrid.SelectedItems Collection To List

Answer : Make sure to use the System.Linq namespace then : You should be able to use : List<Foo> SelectedItemsList = DataGrid.SelectedItems.Cast<Foo>().ToList(); or if you're not quite sure what DataGrid.SelectedItems contains : List<Foo> SelectedItemsList = DataGrid.SelectedItems.OfType<Foo>().ToList() Try this: DataGrid.SelectedItems.OfType<Foo>().ToList()

C Function To Convert Float To Byte Array

Answer : Easiest is to make a union: #include <stdio.h> int main(void) { int ii; union { float a; unsigned char bytes[4]; } thing; thing.a = 1.234; for (ii=0; ii<4; ii++) printf ("byte %d is %02x\n", ii, thing.bytes[ii]); return 0; } Output: byte 0 is b6 byte 1 is f3 byte 2 is 9d byte 3 is 3f Note - there is no guarantee about the byte order… it depends on your machine architecture. To get your function to work, do this: void float2Bytes(byte bytes_temp[4],float float_variable){ union { float a; unsigned char bytes[4]; } thing; thing.a = float_variable; memcpy(bytes_temp, thing.bytes, 4); } Or to really hack it: void float2Bytes(byte bytes_temp[4],float float_variable){ memcpy(bytes_temp, (unsigned char*) (&float_variable), 4); } Note - in either case I make sure to copy the data to the location given as the input parameter. This is crucial, as local variables will not exist after you return (alt...

Casting Smallint To Boolean In PostgreSQL

Answer : CREATE OR REPLACE FUNCTION boolean1(i smallint) RETURNS boolean AS $$ BEGIN RETURN (i::smallint)::int::bool; END; $$ LANGUAGE plpgsql; CREATE CAST (smallint AS boolean) WITH FUNCTION boolean1(smallint) AS ASSIGNMENT;

Cast Int To Enum In Java

Answer : Try MyEnum.values()[x] where x must be 0 or 1 , i.e. a valid ordinal for that enum. Note that in Java enums actually are classes (and enum values thus are objects) and thus you can't cast an int or even Integer to an enum. MyEnum.values()[x] is an expensive operation. If the performance is a concern, you may want to do something like this: public enum MyEnum { EnumValue1, EnumValue2; public static MyEnum fromInteger(int x) { switch(x) { case 0: return EnumValue1; case 1: return EnumValue2; } return null; } } If you want to give your integer values, you can use a structure like below public enum A { B(0), C(10), None(11); int id; private A(int i){id = i;} public int GetID(){return id;} public boolean IsEmpty(){return this.equals(A.None);} public boolean Compare(int i){return id == i;} public static A GetVa...