Subtracting two DateTime objects in c++/cli - datetime

System::DateTime ^now = System::DateTime::Now;
System::DateTime ^now2 = System::DateTime::Now;
System::TimeSpan ^span = now->Subtract(now2);
The above code gets the compiler error:
test.cpp(104) : error C2664: 'System::TimeSpan System::DateTime::Subtract(System::DateTime)' : cannot convert parameter 1 from 'System::DateTime ^' to 'System::DateTime'
1> No user-defined-conversion operator available, or
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
now and now2 are c++/cli handles, but the method expects the actual object. What is the correct way of invoking the method from c++/cli?

DateTime and TimeSpan are value types.
System::DateTime now = System::DateTime::Now;
System::DateTime now2 = System::DateTime::Now;
System::TimeSpan span = now.Subtract(now2);

System::Diagnostics::Stopwatch^ stopwatch = gcnew System::Diagnostics::Stopwatch();
stopwatch->Start();
// code
stopwatch->Stop();
// stopwatch->Elapsed

Related

Kotlin reflection on inbuilt classes

I am trying to read the "value" field of a String in Kotlin. I am not very familiar with reflection in Kotlin, so I can't get it to work. This is what I have:
var str: String = "Some string"
val field = String::class.java.getDeclaredField("value")
field.isAccessible = true
println(field) // This prints "private final char[] java.lang.String.value"
println(field.get(str)) // This prints [C#66d3c617
When trying to cast char[] to Array, I get this exception:
java.lang.ClassCastException: [C cannot be cast to [Ljava.lang.Character;
What am I doing wrong?
I am not sure what you are trying to achieve but you can try this.
val value = (field.get(str) as ByteArray).toString(Charset.defaultCharset())
println(value)
In my env, the field is a ByteArray so I casted it to a ByteArray and get a printable version. In your case, a simple CharArray should suffice.

JScript.NET: Enumerating WMI collections

In JScript.NET the following snippet:
wmi.js
------
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2"),
col =null, prc=null;
col=wmi.ExecQuery("SELECT * From Win32_Process", "WQL", 32);
//col=wmi.InstancesOf("Win32_Process");
var e = new Enumerator(col);
for (; !e.atEnd(); e.moveNext()){
prc = e.item();
print(prc.CommandLine);
}
compiles with:
%windir%\Microsoft.NET\Framework64\v4.0.30319\jsc.exe /platform:x64 wmi.js
and executes, but changing the WMI call with:
col=wmi.ExecQuery("SELECT * From Win32_Process", "WQL", 32);
compilation still works, while the execution gives the:
Unhandled Exception: System.InvalidCastException:
Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{496B0ABE-CDEE-11D3-88E8-00902754C43A}' failed due to the following error:
'No such interface supported (Exception from HRESULT: 0x80004002
I don't understand why, since for both
InstancesOf
and
ExecQuery documentation says:
If successful, the method returns an SWbemObjectSet
Also, WSH JScript can enumerate both InstancesOf collection and ExecQuery.
First things first, remove the flag for wbemFlagForwardOnly and the ExecQuery returns an object that works as expected.
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2")
, col =null, prc=null;
col=wmi.ExecQuery("SELECT * From Win32_Process");
//col=wmi.InstancesOf("Win32_Process");
var e = new Enumerator(col);
for (; !e.atEnd(); e.moveNext()){
prc = e.item();
print(prc.CommandLine);
}
For the explanation, here's a shot in the dark (I don't work with Jscript.NET every day nor am I an expert).
from https://msdn.microsoft.com/en-us/library/ms974547.aspx:
"A forward-only enumerator performs much faster than the default enumerator, because WMI doesn't maintain references to objects in the SWbemObjectSet"
from the error:
"Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable."
It seems that converting collection to enumerator requires a reference to the object being casted. With wbemFlagForwardOnly flag, there is no reference passed so cast fails.
That is how I read this. Take it for what it's worth.
An interesting thing I found when researching: there is no error with this enumerator using wscript/cscript versus executing exe from jsc/csc.
Also, it seems VBScript has no problem enumerating with these flags; check out the examples and compare - https://msdn.microsoft.com/en-us/library/ms525775(v=vs.90).aspx.

Julia : Make method take array as parameter

I'm currently programming in Julia and I want a method (The constructor of class ProbData) to take an array of a defined type as its parameter. What I'm currently doing is:
function ProbData(variables::AbstractArray{BaseVariable, 1})
instance = new()
instance.numPara = size(variables, 1)
instance.numRand = 0
for x = variables
if x.variableType != "Det"
instance.numRand += 1
end
end
instance.getNumPara = function()
return instance.numPara
end
instance.getNumRand = function()
return instance.numRand
end
return instance
end
BaseVariable is an abstract that has three subtypes. When I try to:
x = DetVariable("test", 0.15)
P = ProbData([x])
I get this error message:
ERROR: LoadError: MethodError: 'convert' has no method matching convert(::Type{ProbData}, ::Array{DetVariable.jl,1})
How can I fix this? I've just learned this language for a few days, so if you can point out what I shouldn't have done, I would appreciate it as well.
Edit: I've found out that if I defined the parameter array of the constructor to be an array of a certain type rather than an array of an abstract, the code would work properly. However, I need the parameter to be an array, whose elements could be of any subtype of BaseVariable.
Just change the function declaration to:
function ProbData{T<:BaseVariable}(variables::AbstractArray{T, 1})

cannot convert 'QScopedPointer<T>' to 'QStandardItem *'

I use this code without any error
QStandardItem *newRow;
newRow = new QStandardItem(hostname);
model2->setItem(index, 2, newRow);
I want to change the above code to the below:
QScopedPointer<QStandardItem> newRow(new QStandardItem);
model2->setItem(index, 2, newRow);
But I get this error:
C:\...\mainwindow.cpp:352: error: C2664: 'void QStandardItemModel::setItem(int,int,QStandardItem *)' : cannot convert parameter 3 from 'QScopedPointer<T>' to 'QStandardItem *'
with
[
T=QStandardItem
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
How can I solve the problem?
Try this, use take() method to get the pointer.
On my computer
QStandardItem *item2 = new QStandardItem("foo");
model->setItem(4,0,item2);//works
QScopedPointer<QStandardItem> newRow(new QStandardItem("foo"));
model->setItem(4,0,newRow.take());//works too
Instead of QScopedPointer<T>::take() which releases the stored pointer of the scoped pointer container i suggest to use QScopedPointer<T>::data() which returns a the pointer but does not reset the scoped pointer
But on the other hand, why would you like to use QScopedPointer to store a pointer to the QStandardItem when the model will take ownership of it and will handle its lifetime?

Why does casting a Date to a Date in ActionScript fail?

In ActionScript, I've discovered, casting a Date to a Date and assigning it to a Date-typed variable throws a TypeError:
var date : Date = Date(new Date(2012, 01, 01));
Error #1034: Type Coercion failed: cannot convert "Wed Aug 22 17:06:54 GMT+1000 2012" to Date.
This is obviously wrong, but I'd like to know why it happens. My theory is that the Date cast, like the Number cast, has been overridden to attempt to convert the given type rather than just cast it.
Interestingly, casting anything else to a Date and assigning it to a Date also fails:
var date : Date = Date("1/2/3");
var date : Date = Date(123);
// (Both fail)
But assigning it to an Object succeeds:
var object : Object = Date(new Date(2012, 01, 01));
var object : Object = Date("1/2/3");
var object : Object = Date(123);
// (All succeed)
AS3 can be very confusing and inconsistent at times.
Basically you're not casting anything in that code sample.
AS3 has some global camelCased functions that will take precedence over casting operators.
Vector also has a similar global function.
When you do Date(bla) without the new operator, it apparently creates a string representation of that date... Try to cast with the as operator instead.
Typically you should get a compiler warning about this behaviour, if the compiler argument
<!-- Invalid Date cast operation. -->
<warn-bad-date-cast>true</warn-bad-date-cast>
exists in your flex-config.xml.

Resources