DirectCast (or alternative) with a value from Array Of Type as second argument? - arrays

I am writing a Class to error report explicitly to users who sent copied & pasted data via ajax.
I am trying to properly cast Types for INSERTion into sql server.
The user's data is stored in an Object(,). The Types are passed via ByVal ColumnTypes() As Type and stored inline:
{
GetType(Integer), GetType(String), GetType(Integer)
}
for example.
How can a value from the Array Of Type be used as the second argument of DirectCast (or an alternative to DirectCast)?
psuedo
DirectCast(thisVariable, ColumnTypes(0))
for an Integer

Unfortunately there is no way to achieve this. The DirectCast expression can only work with named types. Essentially values that are resolvable at compile time. The expression ColumnTypes(0) is only resolvable at runtime and hence can't be used with DirectCast in this manner.
Instead of DirectCast try using TypeConverter
TypeConverter.ConvertTo(thisVariable, ColumnTypes(0))

Related

Only user-defined types defined in public modules can be coerced to or from a variant passed to late-bound functions

I have the following code, whereby I have declared an array or records as Member, the Member type consists of 3 entries, forename, surname and distance. I've tried many variations on my 'call' function but I keep getting the same error:
Here is a shortened format of my 'whole' code, hoping someone can point out whatever silly mistake I'm making here, I can only assume something within the parameter passing or declaration of the record structure?
Hope you can help with my school project.
Just like the message says. You're using a private type, and so it can't be coerced to Variant.
You probably want your Read_In_File Sub to instead declare the type of the parameter, rather than using the default Variant type.
Private Sub Read_In_File(ByRef Members As Member())
Though it's very odd to both have a global variable in your class and a parameter of the same name; so I'm not quite sure what exactly you're trying to accomplish.
The error message is telling you exactly how to solve your issue:
"Only user-defined types defined in public modules can be coerced to or from a variant passed to late-bound functions"
Add a Module to your project called MUserDefinedTypes or modUserDefinedTypes (or whatever naming convention you use) and declare your public user-defined type there.
I also agree with Peter in that you have some funny naming conventions in you code. Do not name your variables the same as you modules, classes, forms, or types. It will only get you into trouble. I suggest a simple prefix approach.

Kotlin array types and class literals

I am trying to deserialize an array of JSON objects with GSON. So the simple call:
val arrayOfFoo = gson.fromJson(source, Array<Foo<*>>::class.java>)
should do the trick. But type erasure tells us, that Foo<*> does not exist at runtime, so the error "Only class literals are allowed on the left hand side of a class literal" pops up. Well, so the solution must be:
val arrayOfFoo = gson.fromJson<Array<Foo<*>>>(source, Array::class.java)
Unfortunatelly, now the Kotlin compiler magic - that turns arrays of Wrapper types into primitive arrays - can not be sure what to do and tells us:
"Array class literals require a class type. Please specify one in angle brackets".
But, wait: This is, what did not work a second ago. Using
Array<Foo>::class.java
does not work, too, since now the compiler tells us: "One type argument is expected for Foo".
I personally can not see a way to solve that. Is it impossible to give a class literal of a typed array, which's type also expects a type parameter?
You can get the array class from an array instance, for example either one of
arrayOf<Foo<*>>()::class.java
java.lang.reflect.Array.newInstance(Foo::class.java, 0)::class.java
The basic problem: You need to specify the type of your array. This is done using a TypeToken in Gson.
I hope this helps:
val listType = object : TypeToken<Array<String>>() {}.type
val json = """["1"]"""
val yourClassList :Array<String> = Gson().fromJson(json, listType)
print(yourClassList)
Note that for primitives, it is simpler: Gson().fromJson(json, IntArray::class.java)

Querying database in Grails

The values in database are saved as such
::{"rating1":"2","rating2":"4","rating3":"5","rating4":"0","rating5":"0"},
Now I need to acces the individual values like 2,4,5 etc.
I made a variable "rating" of the Domain Class type and tried accesing as object using (.) operator but it wont work and gives error:
:exception::groovy.lang.MissingPropertyException: No such property: rating1 for class: java.lang.String
, I tried casting to array and list (as Array, as ArrayList, as List) etc but that wont work either.
Casting to List gives exception:exception::org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{"rating1":"2","rating2":"4","rating3":"5","rating4":"0","rating5":"0"}' with class 'java.lang.String' to class 'java.util.List' .
Accessing like "rating[3]" gives answer "a". Should I use "rating[11]" to get value 2 or is there any way around.
What could be the possible solution. Please help.
You're storing a string, so you only have string operations available. You need to parse that string to get at the attributes individually (JsonSlurper perhaps?).
rating[character-position] is a bad idea IMO.
Maybe transients (check the GORM docs) would be useful.
class YourDomain {
String yourField
String getRating1() { new JsonSlurper().parseText(yourField).rating1 }
}
Maybe. Totally untested, just an idea.

D: Creating an array of templated objects

I'm trying to create an array of Regex objects, like so: Regex[] regexes;.
The compilation fails with main.d(46): Error: template std.regex.Regex(Char) is used as a type.
I find the documentation cryptic. All I understand is that templates generate code on compilation, but I don't see what's preventing me from creating an array of Regex.
There's an existing question on StackOverflow with the same problem, but it deals with C++, not D.
You cannot create a regex object without first instantiating the template with a type. this is because the actual type is generated at compile time based on the instantiation type you give. Regex itself is not an actual type, it is just a template function allowing you to generate a type when instantiated.
In this case you probably want to change:
Regex[] regexes;
into:
Regex!char[] regexes;
to tell the compiler that your regex contains chars as opposed to some derived type. This means specifically you are instantiating the Regex template with the type char.

VBA excel passing an array to sub fired by button

As in title I would like to pass an array to the sub which is executed by a button (That's why I don't know ho to pass it to the sub). Array is calculated in the Sheet before you can exectue sub with the button.
I've tried using:
Option Explicit On
Public My_array(1 to 10)
But I have received an error that I can not do that command with array, const etc.
What is the correct way to do it?
I think there is some terminology we can try to clear up: you're not actually passing the variable to the button's procedure. A publicly scoped variable is going to be available within that procedure and does not need to be "passed".
Note: I'm not sure what you mean by "Array is calculated in the Sheet". Unless you are using some event procedure to calculate an array in memory (probably you are not, because you don't have the array scoped or declared properly to do this...), this statement can't possibly be true.
Your declaration should be like:
Public myArray(1 To 10) As Variant 'or As String, As Double, etc.
You can omit the As ... part and it will be type Variant. if you need more strongly-typed array, then you will need to specify.
Also, On is not a legal keyword after the Option Explict statement. So, remove that. But keep Option Explicit in all of your modules.
The error message you receive hints at the solution: You can't put public declarations in object modules. An object module is a Worksheet, Workbook, UserForm, or Class module in the VBE.
You can only put these declarations in a normal code module.
Solution: Move the declaration of myArray to a normal code module.

Resources