casting

Creating generic arrays in Java

折月煮酒 提交于 2020-02-11 08:55:07
问题 public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will throw out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can create an array with a generic type? Thanks. 回答1: You can't: you must pass the class as an argument: public <K> K[] toArray(Class<K> clazz) { K[] result = (K[])Array.newInstance(clazz,this.size

Creating generic arrays in Java

半城伤御伤魂 提交于 2020-02-11 08:54:32
问题 public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will throw out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can create an array with a generic type? Thanks. 回答1: You can't: you must pass the class as an argument: public <K> K[] toArray(Class<K> clazz) { K[] result = (K[])Array.newInstance(clazz,this.size

Creating generic arrays in Java

余生长醉 提交于 2020-02-11 08:53:25
问题 public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will throw out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can create an array with a generic type? Thanks. 回答1: You can't: you must pass the class as an argument: public <K> K[] toArray(Class<K> clazz) { K[] result = (K[])Array.newInstance(clazz,this.size

Convert fractional string to decimal

拟墨画扇 提交于 2020-02-06 09:52:28
问题 I've got a few columns that have values either in fractional strings (i.e. 6 11/32) or as decimals (1.5). Is there a CAST or CONVERT call that can convert these to consistently be decimals? The error: Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar to numeric. Can I avoid doing any kind of parsing? Thanks! P.S. I'm working in SQL Server Management Studio 2012 . 回答1: CREATE FUNCTION ufn_ConvertToNumber(@STR VARCHAR(50)) RETURNS decimal(18,10) AS BEGIN DECLARE @L VARCHAR

Displaying a CAST datetime in RFC822 Format

久未见 提交于 2020-02-05 07:01:08
问题 I would like to display a CAST datetime in an SQL table using the RFC822 format (ex: Fri, 19 Nov 2010 13:43:39 ) from the following SELECT (part of a larger statement found here) *_snip_* ,(Select Cast(Cast(FieldValue as nvarchar(max)) as DateTime) from dbo.UserDefinedData where UserDefinedFieldId = 298 and UserDefinedRowId = item.UserDefinedRowId) as [pubDate] The bit of SQL below will retrieve the current date in the desired format. How can I integrate it into the above statement (or is

Understanding cast from bytea to oid

妖精的绣舞 提交于 2020-02-05 04:36:47
问题 I'm using PostgreSQL 9.2 . This blog entry by Grace Batumbya provides a cast from bytea to oid . create or replace function blob_write(lbytea bytea) returns oid volatile language plpgsql as $f$ declare loid oid; lfd integer; lsize integer; begin if(lbytea is null) then return null; end if; loid := lo_create(0); lfd := lo_open(loid,131072); lsize := lowrite(lfd,lbytea); perform lo_close(lfd); return loid; end; $f$; CREATE CAST (bytea AS oid) WITH FUNCTION blob_write(bytea) AS ASSIGNMENT;

How to emulate .net Int64 in VB6?

旧巷老猫 提交于 2020-02-03 16:50:42
问题 How can store an Int64 number in VB6, to work with Win32 functions? Is there a way to define a type like Int64 in .net? And simply evaluate the number. 回答1: I think many of VB6 programmers need something like this, Because some of the Win32 API's use _int64 as their parameters. I wrote a function to cast a currency into an API compatible structure. Put these codes in a module file. Private Declare Sub CopyMemory lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal

How to emulate .net Int64 in VB6?

大憨熊 提交于 2020-02-03 16:46:57
问题 How can store an Int64 number in VB6, to work with Win32 functions? Is there a way to define a type like Int64 in .net? And simply evaluate the number. 回答1: I think many of VB6 programmers need something like this, Because some of the Win32 API's use _int64 as their parameters. I wrote a function to cast a currency into an API compatible structure. Put these codes in a module file. Private Declare Sub CopyMemory lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal

How to emulate .net Int64 in VB6?

房东的猫 提交于 2020-02-03 16:46:28
问题 How can store an Int64 number in VB6, to work with Win32 functions? Is there a way to define a type like Int64 in .net? And simply evaluate the number. 回答1: I think many of VB6 programmers need something like this, Because some of the Win32 API's use _int64 as their parameters. I wrote a function to cast a currency into an API compatible structure. Put these codes in a module file. Private Declare Sub CopyMemory lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal

How to do a static assert that a pointer cast is trivial?

拜拜、爱过 提交于 2020-02-03 04:32:26
问题 Let's say I have these types: struct A { int a; }; struct B { int b; }; struct C : public A, public B { int c; }; A C* pointer can be cast to A* pointer without adjusting the actual address at all. But when C* is cast to B* , the value must change. I'd like to ensure that two related types I have can be cast to each other without a change in address (i.e. that there is no multiple inheritance, or that the base class is the first base of the derived class). This could be checked at run-time, e