Author Topic: String Concat  (Read 1282 times)

Phrog30

  • Guest
String Concat
« 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.
Code: [Select]
label.Text = variable & " static"
This works
Code: [Select]
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

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: String Concat
« Reply #1 on: August 10, 2017, 06:28:44 PM »
it works for me
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: String Concat
« Reply #2 on: August 10, 2017, 06:45:37 PM »
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

Phrog30

  • Guest
Re: String Concat
« Reply #3 on: August 10, 2017, 06:48:09 PM »
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:

Code: [Select]
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.
« Last Edit: August 10, 2017, 06:50:43 PM by Phrog30 »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: String Concat
« Reply #4 on: August 10, 2017, 06:59:45 PM »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: String Concat
« Reply #5 on: August 10, 2017, 07:01:22 PM »
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

Phrog30

  • Guest
Re: String Concat
« Reply #6 on: August 10, 2017, 07:23:36 PM »
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?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: String Concat
« Reply #7 on: August 10, 2017, 07:47:12 PM »
ReadRaw doesn't work with subscriptions. It only an additional function similar to Read

Phrog30

  • Guest
Re: String Concat
« Reply #8 on: August 10, 2017, 07:51:44 PM »
That's what I thought, just making sure. I will try your suggestion later tonight. Thank you.

Phrog30

  • Guest
Re: String Concat
« Reply #9 on: August 10, 2017, 08:49:46 PM »
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

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: String Concat
« Reply #10 on: August 11, 2017, 12:02:53 AM »
It should make it work better since it is using the length as defined by the PLC tag.

Phrog30

  • Guest
Re: String Concat
« Reply #11 on: August 11, 2017, 09:18:41 AM »
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.

Code: [Select]
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

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: String Concat
« Reply #12 on: August 12, 2017, 11:05:18 AM »
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.

Phrog30

  • Guest
Re: String Concat
« Reply #13 on: August 12, 2017, 07:40:43 PM »
Thanks for the update Archie. I'm happy with the updated code you suggested.