Author Topic: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)  (Read 1178 times)

0 Members and 1 Guest are viewing this topic.

Offline NY2KWTopic starter

  • Regular Contributor
  • *
  • Posts: 149
  • Country: us
VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
« 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?
 

Offline TomS_

  • Frequent Contributor
  • **
  • Posts: 834
  • Country: gb
Re: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
« Reply #1 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) + "?*"
 

Offline gbaddeley

  • Regular Contributor
  • *
  • Posts: 205
  • Country: au
Re: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
« Reply #2 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.
Glenn
 

Offline Syntax Error

  • Frequent Contributor
  • **
  • Posts: 584
  • Country: gb
Re: VB Net 2019 - Using: string.{TrimEnd(charsToTrim)
« Reply #3 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
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf