Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access constants of a static class #6

Closed
PavelBal opened this issue Feb 19, 2019 · 7 comments
Closed

Access constants of a static class #6

PavelBal opened this issue Feb 19, 2019 · 7 comments

Comments

@PavelBal
Copy link

Is there a way to access constants of a static class? The code would look like

namespace MyNamespace
{
public static class MyConstants
{
public const int CONST_1 = 38;
public const int CONST_2 = 20;
public const int CONST_3 = 100;
...

@tr8dr
Copy link
Owner

tr8dr commented Feb 19, 2019

The code does not handle this currently. If you are handy with low level reflection code, could add code to scan for fields and consider these to be properties more or less.

Perhaps easier would be to expose some of these constants as properties, in which case the bridge will see these.

In R would get the property as:

obj$Get("CONST_1")

In python:

obj.CONST_1

@PavelBal
Copy link
Author

@tr8dr Thank you for the reply! I will try the second option.

@PavelBal
Copy link
Author

Could you also give me a hint how to create a .NET array of a certain type in R?

I tried
myArray= .cstatic("System.Array", "CreateInstance", myType, 0);

but I'm getting

Error in internal_ccall_static(classname, methodname, argv) : 
  closing connection 

@tr8dr
Copy link
Owner

tr8dr commented Feb 21, 2019

If the target array is one of the types recognized: int, double, float, string. You can just create the vector on the R side and then pass to a function.

For example if you have a function that takes a double[]:

class Foo {
     public double Sum(double[] vector) {
        ...
     }
     public double SumVector(DenseVector<double> vector) {
        ...
     }
}

You can call this function directly with an R vector.

obj <- .cnew("Foo")
v <- rnorm(100)
obj$Sum (v)

Or if you use MathNet.Numerics, can use functions with signatures for matrices and vectors:

obj <- .cnew("Foo")
v <- rnorm(100)
obj$SumVector (v)

If you are trying to create a type that is not something R would understand, not sure why you would bother to create from R. If you provide more context in terms of what you want to do can suggest a solution.

@PavelBal
Copy link
Author

I need to call a function that requires an array of a custom type as one of the arguments:

public ParameterValue[] CreateIndividual(OriginData matlabOriginData, IEnumerable<MoleculeOntogeny> moleculeOntogenies){
I can create an object of type OriginData from within R, and the output type 'ParameterValue' offers getter/setter for retrieving numerical and string data.

So far I'm trying to figure out how much I can do without writing a wrapper in C#.

@tr8dr
Copy link
Owner

tr8dr commented Feb 22, 2019

You should be able to see the ParameterValue[] objects. I suspect where it is confused is with the IEnumerable<...>.

I think the problem with the array you were creating above is that the array would initially be empty. R can view and index into an array of objects, but in creating the array initially does not have objects in it (unless it is a struct type). The Bridge will attempt to return a R list of these objects, but then the array cells are uninitialized, presenting a problem.

Even if you created the array and the R bridge was OK with uninitialized cells, there is no functionality to set a value in the array. I'd have to check, but you might be able to create a R list of objects and then this will be converted in the call.

If we are considering an array of MoleculeOntogeny objects, could do as follows:

ontogeny <- list()
ontogeny[[1]] <- .cnew ("...")
ontogeny[[2]] <- .cnew("...")

obj$CreateIndividual (..., ontogeny)

if you call your function, this will be presented as a List<> of these objects.

@PavelBal
Copy link
Author

Great, thank you, I'll give it a try!

@PavelBal PavelBal closed this as completed Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants