EEVblog Electronics Community Forum

Computing => Programming => Topic started by: NY2KW on April 18, 2020, 06:13:12 pm

Title: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
Post by: NY2KW on April 18, 2020, 06:13:12 pm
I am learning VB Net 2019.   My program searches (with NI-VISA) for all available instrument types as selected by user.  Based on checkboxes selected I build a resourceString in a NI-VISA format where the abbreviations that NI-VISA expects  are ASRL, GPIB, TCPIP, USB.  The resourceString expects these types to be separated by the ' | ' character and I need to strip off the last ' | ' and then terminate with a string of "?*"

I have Option Strict On as I was told to learn VB NET this way

Code: [Select]
Dim resourceString as String = "ASRL|GPIB|TCPIP|USB|"     'for debugging i use a string with all resources as they would be added
Dim charsToTrim() As Char = {"|"}

Dim temp As String = resourceString.TrimEnd(charsToTrim)
 resourceString = temp + "?*"

I get an error : on the 3rd line with the ' | '  :   Error   BC30512   Option Strict On disallows implicit conversions from 'String' to 'Char'.   

Is my only option to turn off Option Strict?
Title: Re: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
Post by: TomS_ on April 19, 2020, 06:50:53 am
Presumably it still thinks your character is a string.

Looking at the documentation for TrimEnd (https://docs.microsoft.com/en-us/dotnet/api/system.string.trimend?view=netframework-4.8), perhaps try this:

Code: [Select]
Dim charsToTrim() As Char = {"|"c}

Or:

Code: [Select]
Dim temp As String = resourceString.TrimEnd("|"c)

Or maybe even:

Code: [Select]
resourceString = resourceString.TrimEnd("|"c) + "?*"
Title: Re: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
Post by: gbaddeley on May 07, 2020, 01:35:12 pm
How about changing the way “|” are used in the string, putting them before each value rather that after. The trim end is no longer required. Just take the SubString from offset 1 onwards, and append the trailing sequence.
Title: Re: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
Post by: Syntax Error on May 08, 2020, 07:28:58 pm
If you must have a trailing "|", another method is to declare resourceString as an array containing the strings "ASRL" etc, and then use the string.join class to create your output. So...

output = string.Join( "|" , resourceString ) + "?*"
ASRL|GPIB|TCPIP|USB?*

You can change the seperator to any string value.
https://docs.microsoft.com/en-us/dotnet/api/system.string.join?view=netcore-3.1

Or use the regular expression RegEx.replace class to strip the trailing character - if present. But regular expressions are a little overkill for your task. https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions