Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
From Variant to Array
May 29, 2018
18:28, EEST
Avatar
otto.fitz
Member
Members
Forum Posts: 13
Member Since:
May 29, 2018
sp_UserOfflineSmall Offline

I subscribe an Array on a Server.

If i became onDataChange listener an Array. I can printout this, but how can i used it as Array in java to get .size or getIndex() and so on?

This is how i do:

if(machineData.getValue().isArray()){
System.out.println(machineData.getValue());
}

And the output:

[1380.06, 1581.48, 1781.52, 1982.06, 2184.38, 2385.93, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

My Datatyp of value ist Variant

thx đŸ™‚

May 30, 2018
14:08, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1032
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

This somewhat goes into general java programming category, which is generally outside of support. However as this can be sometimes a bit confusing:

Typically you know the actual type of the value that is encoded to the Variant and can just cast the value (Variant.getValue()) to the correct type, e.g. if you know the type is Double, then you can just

Variant variant = …
Double v = (Double) variant.getValue();

Similarly if you know it is singledimensional array of Doubles you can do

Double[] v = (Double[]) variant.getValue();

If it 1-dim array of not known type you can use Object[].

It should be noted that the value can be more than one-dimensional array. Please read the UA specification Part 6, section 5.2.2.16 “Variant” for more information.

May 30, 2018
15:32, EEST
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

The MultiDimensionArrayUtils in the stack (org.opcfoundation.ua.utils) can also provide some assistance on working with – especially multidimensional – arrays.

June 13, 2018
8:56, EEST
Avatar
otto.fitz
Member
Members
Forum Posts: 13
Member Since:
May 29, 2018
sp_UserOfflineSmall Offline

Thanks, it works

January 27, 2020
4:19, EET
Avatar
powerplant
New Member
Members
Forum Posts: 1
Member Since:
January 27, 2020
sp_UserOfflineSmall Offline

Bjarne Boström said
Hi,

This somewhat goes into general java programming category, which is generally outside of support. However as this can be sometimes a bit confusing:

Typically you know the actual type of the value that is encoded to the Variant and can just cast the value (Variant.getValue()) to the correct type, e.g. if you know the type is Double, then you can just

Variant variant = …
Double v = (Double) variant.getValue();

Similarly if you know it is singledimensional array of Doubles you can do

Double[] v = (Double[]) variant.getValue();

If it 1-dim array of not known type you can use Object[].

It should be noted that the value can be more than one-dimensional array. Please read the UA specification Part 6, section 5.2.2.16 “Variant” for more information.  

The conversion method for arrays outlined in this post doesn’t work for me. I’m forced to do an element-by element conversion. Are there any utility methods in the client SDK that I can use instead?

January 27, 2020
11:48, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1032
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

Please define “does not work”. What stacktraces and/or errors you are getting?

January 30, 2020
16:51, EET
Avatar
bvanseveren
Member
Members
Forum Posts: 3
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline

In our code we’re transforming the array into a Variant[]. This way, you can convert each item in the array the way you like.

public Variant[] asArray(Variant value) {
if (value.isArray()) {
Object[] array = (Object[]) value.getValue();
return Arrays.stream(array).map((o) -> Variant::new).toArray(Variant[]::new);
} else {
// Value is not an array, handle as you wish
}
}
Forum Timezone: Europe/Helsinki

Most Users Ever Online: 1919

Currently Online:
25 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Heikki Tahvanainen: 402

hbrackel: 144

rocket science: 88

pramanj: 86

Francesco Zambon: 83

Ibrahim: 78

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

Member Stats:

Guest Posters: 0

Members: 728

Moderators: 7

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1529

Posts: 6471

Newest Members:

roycedelargie91, kourtneyquisenbe, ellis87832073466, zkxwilliemae, gabriellabachus, Deakin, KTP25Zof, Wojciech Kubala, efrennowell431, wilfredostuart

Moderators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1032, Jimmy Ni: 26, Matti Siponen: 349, Lusetti: 0

Administrators: admin: 1