SignalR is sending my struct wrongly - signalr

So, after trying to understand whats happens with my messages in a big class... i've found out with a small test that:
public struct Test
{
public int X {get;set};
public int Y {get;set};
public Test(int x, int y)
{
X = x;
Y = y;
}
}
// hub
var sendMe = new Test(12,20);
Clients.All.Test(sendMe);
...and client gets Test = (0,0)!
Looks like a big bug.
Do i need to fill a bugreport?

I believe this is because JSon.NET (which is used by SignalR client to deserialize payload) does not handle structs by default. You can change your struct to a class.

Related

Recursive struct definitions in Solidity (Creating Linked Lists)

I'm trained mostly in Java so this has never really been an issue for me, but I'm trying to create a linked list in Solidity for a smart contract I am working on. In it, I am using a struct written as such:
struct Node {
address addr;
Node _next;
Node prev;
}
While I'm still working out the kinks on whether it's worth it to keep it as a single or doubly linked list, the main issue is that I have a recursive struct definition problem by referencing a Node struct inside my Node struct. I am a little unsure of what my alternatives are. Is this just a limitation of the language? I read this thread, but I couldn't really tell if the answer given was anything more than "it depends".
Thanks for any feedback; let me know if I can provide more information.
You could do it like this (double linked list):
pragma solidity ^0.4.24;
contract LinkedList {
struct Node {
string data;
uint pointPrevNode;
uint pointNextNode;
}
mapping(uint => Node) public nodes;
uint public nodeNumber = 0;
uint public count = 1;
function setData(string _data) public {
nodes[count].data = _data;
assignPointers(count);
}
function assignPointers(uint _count) internal {
nodes[nodeNumber].pointNextNode = _count;
nodes[_count].pointPrevNode = nodeNumber;
nodeNumber = _count;
count++;
}
}

Why isn't this Math.random method working?

Since Java doesn't allow to return two types in one method, I thought best way to do it is to use get methods.
Simply, I wanted computer to generate two random numbers, and if they were not the same I wanted it to print sum of them. If they were the same, I wanted it to roll once more and sum all of the rolls. Until here, it was okay, but then I wanted to see not only sum, but also the numbers that computer generated randomly before adding them up. Therefore, it had to be several return types.
But it prints 0 three times instead.
Can you help me with this? I want to learn what is wrong exactly with this code and if it can be done neater and cleaner? I know Java loves long ways..
Thank you.
class App {
public static int monopolyRoll(int side) {
double randomNumber = Math.random();
randomNumber = randomNumber * side;
randomNumber = randomNumber + 1;
int randomInt = (int) randomNumber;
return randomInt;
}
private int roll1 = monopolyRoll(6);
private int roll2 = monopolyRoll(6);
public int userRolls() {
if (roll1 != roll2) {
return roll1 + roll2;
} else {
int roll3 = monopolyRoll(6);
int roll4 = monopolyRoll(6);
return roll1 + roll2 + roll3 + roll4;
}
}
private static int first;
private static int second;
private static int third;
public App(int first, int second, int third) {
App.first = roll1;
App.second = roll2;
App.third = userRolls();
}
public static int getFirst() {
return first;
}
public static int getSecond() {
return second;
}
public static int getThird() {
return third;
}
public static void main(String[] args) {
int first = getFirst();
int second = getSecond();
int third = getThird();
System.out.println(first);
System.out.println(second);
System.out.println(third);
}
}
Math.random() works, but you never actually call it in your application. This is what your application does:
int first = getFirst();
int second = getSecond();
int third = getThird();
System.out.println(first);
System.out.println(second);
System.out.println(third);
That's it. Aside from the single return statements in those getter methods and the declared-but-never-assigned integers they return (so, zeroes), none of that other code ever executes.
I suspect this is coming from a bit of a misunderstanding on your part about the static keyword. By sprinkling around the static keyword until the code compiled, what you've done is create something that's syntactically correct but doesn't do anything :)
As a bit of a learning exercise, try moving all of the business logic out of the App class, leaving only the main() method as the application's entry point. And removing all static keywords from the new class you create. This should make the use of that class more clear.
Something like:
class Roller {
private int roll1;
private int roll2;
// other private variables
private int monopolyRoll(int side) {
// your code
}
// your other methods, also private and non-static
public Roller(int first, int second, int third) {
this.first = roll1;
this.second = roll2;
this.third = userRolls();
}
// and so on
}
The idea here is to make things instance-based (non-static) by default. Also make things private by default until explicitly needed to be accessed outside the class. Currently the only things your class needs to expose publicly are the constructor and the getters.
Then in the main() method you'll need to create an instance of your class to use it. Something like this:
Roller roller = new Roller(1, 2, 3);
int first = roller.getFirst();
int second = roller.getSecond();
int third = roller.getThird();
System.out.println(first);
System.out.println(second);
System.out.println(third);

Can Pex process the interface with the parameter of a class type

i have installed Academic version of pex and roles .
I wrote the following code in Visual Studio 2010.but pex just gave a null pointer as the input. doesn't the pex support the class type? please help me.
the test inferface is Test.
source code:
public class ClassForPex
{
public int a;
public int b;
ClassForPex(int x, int y)
{
a = x;
b = y;
}
};
public static class StringExtensions
{
public static int Test(ClassForPex cjh)
{
if (cjh.a > cjh.b)
return cjh.a;
else
{
return cjh.b;
}
}
}
You'll need to use a factory for supplying your ClassForPex instances to the tests. Look at this article to see how to do that.
Using of Factory in Pex - http://developers.de/blogs/damir_dobric/archive/2009/04/13/using-of-factory-in-pex.aspx

IComparer<> and class inheritance in C#

Is there any way to implement specialized IComparer for the base class type so a child class could still use it for sorting in speciliazed collections?
Example
public class A
{
public int X;
}
public class B:A
{
public int Y;
}
public AComparer:IComparer<A>
{
int Compare(A p1, A p2)
{
//...
}
}
so following code will work:
List<A> aList = new List<A>();
aList.Sort(new AComparer());
List<B> bList = new List<B>();
bList.Sort(new AComparer()); // <- this line fails due to type cast issues
How to approach this issue to have both - inheritance of sorting and specialized collections (and do not copy IComparer classes for each of children classes?
Thanks in advance!
Firstly, note that this is fixed in .NET 4 via generic contravariance - your code would simply work. EDIT: As noted in comments, generic variance was first supported in CLR v2, but various interfaces and delegates only became covariant or contravariant in .NET 4.
However, in .NET 2 it's still fairly easy to create a converter:
public class ComparerConverter<TBase, TChild> : IComparer<TChild>
where TChild : TBase
{
private readonly IComparer<TBase> comparer;
public ComparerConverter(IComparer<TBase> comparer)
{
this.comparer = comparer;
}
public int Compare(TChild x, TChild y)
{
return comparer.Compare(x, y);
}
}
You can then use:
List<B> bList = new List<B>();
IComparer<B> bComparer = new ComparerConverter<A, B>(new AComparer());
bList.Sort(bComparer);
EDIT: There's nothing you can do without changing the way of calling it at all. You could potentially make your AComparer generic though:
public class AComparer<T> : IComparer<T> where T : A
{
int Compare(T p1, T p2)
{
// You can still access members of A here
}
}
Then you could use:
bList.Sort(new AComparer<B>());
Of course, this means making all your comparer implementations generic, and it's somewhat ugly IMO.

How to cast a pointer to a c struct to jna structure

I would like some help in casting a pointer to a C struct to a jna strucuture. I am using jna to receive a callback function from a dll, the function has a parameter that is a pointer to a C struct, when a I try to cast the pointer to a jna structure I get wrong structure values.
That is the C struct:
typedef struct
{
int x;
int y;
}Point;
Point *gpt;
typedef struct
{
int x;
int y;
Point pt1;
}Point2;
Point2 *gpt2;
That is the callback function in C with a pointer (void *params) to Point2 sctruct:
void __stdcall PointCallback(void *params, int param_size)
So, I've made this code in java to receive the callback and get the original struct:
// Point.java
package Callback.UsePointLib;
import com.sun.jna.Structure;
public class Point extends Structure
{
public static class ByValue extends Point implements Structure.ByValue {}
public int x;
public int y;
}
//Point2.java
package Callback.UsePointLib;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
public class Point2 extends Structure {
public int x;
public int y;
Point pt1;
public Point2(Pointer p){
super(p);
}
}
Callback implementation:
//UsePointLib.java
public interface IFuncCallback extends StdCallCallback{
void callback(Pointer Params, int ParamSize);
}
public class FuncCallback implements IFuncCallback{
#Override
public void callback(Pointer Params, int ParamSize) {
Point2 pt2; // = new Point2();
pt2 = new Point2(Params);
System.out.println("pt2.x = "+pt2.x); **<- I get zero here instead of four**
System.out.println("pt2.y = "+pt2.y); **<- I get zero here instead of five**
System.out.println("pt2.pt1.x = "+pt2.pt1.x);**<- pt1 is null, throwing exception**
System.out.println("pt2.pt1.y = "+pt2.pt1.y);**<- same as pt1.**
}
}
I've made a C program to access the dll and receive the callback and it works ok, it receives the correct values. So, the problem is my java code. I've tried many alternatives with no success.
Please, I'd appreciate any help on that.
Thanks,
Fernando.
EDIT
I've modified the code and it works partially.
//UsePointLib.java
public interface IFuncCallback extends StdCallCallback{
void callback(Pointer Params, int ParamSize);
}
public class FuncCallback implements IFuncCallback{
#Override
public void callback(Pointer Params, int ParamSize) {
Point2 pt2; // = new Point2();
pt2 = new Point2(Params);
*pt2.read();* **<--Modification**
System.out.println("pt2.x = "+pt2.x); **<- I get the correct value (four)**
System.out.println("pt2.y = "+pt2.y); **<- I get the correct value (five)**
System.out.println("pt2.pt1.x = "+pt2.pt1.x);**<- pt1 is still null, throwing exception**
System.out.println("pt2.pt1.y = "+pt2.pt1.y);**<- same as pt1.**
}
}
The jna docs say that the constructor Structure(Pointer) allocates a structure onto a preallocated memory. It wont automatically assign values for you.I don't think thats what you want.
Change the constructors to
public Point2(){
super();
}
public Point2(Point1 p){
super();
pt1.x = p.x;
pt1.y = p.y;
this.x = something;
this.y = something;
}
Within the context of a callback, JNA will automatically call Structure.read on entry and Structure.write on exit for any parameters of type Structure.
If you declare your callback method signature to use a Structure type of the appropriate subclass ("Point2" in your example), the copying to/from native memory should be automatic.

Resources