AdvancedHMI Software
General Category => Support Questions => Topic started by: Phrog30 on August 10, 2017, 12:29:33 PM
-
This is a general .NET question, I'm trying to concat two strings, one is a variable, the other is static. If I start with the static text it works, but whenever I start with the variable, then static, only the variable string is showing.
In this example, only the variable shows in the label text.
label.Text = variable & " static"
This works
label.Text = "static" & variable
I've also tried string.format, string.concat, etc. Can someone please help me on this? I would think it's easier than I'm making it. :)
James
-
it works for me
(https://s28.postimg.org/n6opk916l/Capture.png)
-
The 1 theory I can come up with is that your string has white spaces on the end of it. This theory will only hold up if your label's AutoSize property is set to False. What could happen is the white spaces push the appended string to the second line which makes it not visible if your label height can only accommodate one line.
To test this, try this:
label.Text = variable.length & "-" & variable
Then see if the length is more than the characters you see.
Another way to test it is to use variable.Trim
-
I've tried trim, I narrowed it down to when the dynamic string comes from a PLC string that isn't 82 characters and I run it through this code:
Private Function ExtractString(ByVal s As String) As String
Dim bytes((s.Length / 2) - 1) As Byte
For i = 0 To (s.Length / 2) - 1
bytes(i) = Byte.Parse(s.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber)
Next
Dim StringLength As Integer = BitConverter.ToInt32(bytes, 0)
Dim StringResult As String = System.Text.Encoding.Default.GetString(bytes, 4, bytes.Length - 4)
Return StringResult
End Function
If the dynamic string is full length it works fine.
Also, I'm testing with the console, so autosize isn't relevant. Also, I'm not using any AHMI components, when I see a datachange I'm writing my own code to do what I want.
-
Try this:
Dim StringResult As String = System.Text.Encoding.Default.GetString(bytes, 4, StringLength)
Otherwise you are appending the nulls to the end and .NET may be adding the nulls, but when it gets to a null during display, it may be assuming it is the end of the string. Similar to C programming.
-
Also if your using version 3.99x, you can save a few lines by using the new method of ReadRaw which returns the bytes instead of a string with the bytes encoded as hex
-
Also if your using version 3.99x, you can save a few lines by using the new method of ReadRaw which returns the bytes instead of a string with the bytes encoded as hex
I'm using the latest. So I can use the readraw method in a data subscriber data change event?
-
ReadRaw doesn't work with subscriptions. It only an additional function similar to Read
-
That's what I thought, just making sure. I will try your suggestion later tonight. Thank you.
-
Try this:
Dim StringResult As String = System.Text.Encoding.Default.GetString(bytes, 4, StringLength)
Otherwise you are appending the nulls to the end and .NET may be adding the nulls, but when it gets to a null during display, it may be assuming it is the end of the string. Similar to C programming.
Archie, this did the trick. Do you think this change will effect any of the existing stuff? Probably kind of hard to answer, but just curious what you think. If not, I will update my code and use this throughout my application(s).
James
-
It should make it work better since it is using the length as defined by the PLC tag.
-
Thanks Archie, by the way, if you send a "full string" (82 characters) through this code you will get an exception. I added a try catch, which everyone should consider as well.
Private Function ExtractString(ByVal s As String) As String
Dim bytes((s.Length / 2) - 1) As Byte
For i = 0 To (s.Length / 2) - 1
Try
bytes(i) = Byte.Parse(s.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber)
Catch
Return "String Error"
End Try
Next
Dim StringLength As Integer = BitConverter.ToInt32(bytes, 0)
Dim StringResult As String = System.Text.Encoding.Default.GetString(bytes, 4, StringLength)
Return StringResult
End Function
-
I think I stated wrong in saying ReadRaw doesn't work with subscriptions. Although the ReadRaw isn't used directly, a similar array of byte data is returned in the data received event. You can use e.RawData but you have to skip the first 4 (I think) bytes because they contain the data type.
-
Thanks for the update Archie. I'm happy with the updated code you suggested.