none
Comparing Strings with on another String. VB.Net RRS feed

  • Question

  •  

    Hello, Im working on a .ini file that needs to be compared

     

     

    myIniFile = new iniFile(filepath)

    dim ex as string = myIniFile.getTerm()

     

     

    if ex = "MYTERMINAL" then

    foo()

    end if

     

    Funcion getTerm() as string

     

    TERMINAL_TYPE = New String(" ", 6)

    result = GetPrivateProfileString("GENERAL", "TERMINAL_TYPE", " ", TERMINAL_TYPE, + _ 10,  iniFilePath)

     

    return item.tostring()

     

    end function

     

     

    now for some reason this does not work..... On my debug the "ex" variable does equal "MYPROGRAM" string but in the comparison "if" statement it will not follow.  I have tried all of the string cmparison statements.... They all say true but in the "if" statement the true is ignored
    Monday, August 27, 2007 8:10 PM

Answers

  • If you look real close in the debugger's Locals window, you'll see that "MYPROGRAM is missing the end-quote.  That's not a typo, there's an invisible character there.  The If statement sees it.  You must use a StringBuffer.  Using a ByRef String violates the immutability requirement of the String type.  Add a new module to your project and paste this code:

    Imports System.Text
    Module Module1
      Private Declare Auto Function GetPrivateProfileString Lib "kernel32.dll" (ByVal section As String, _
        ByVal key As String, ByVal defval As String, ByVal retval As StringBuilder, _
        ByVal bufsize As Integer, ByVal path As String) As Integer

      Public Function GetIniString(ByVal path As String, ByVal section As String, ByVal key As String) As String
        Dim buf As New StringBuilder(1024)
        Dim len As Integer = GetPrivateProfileString(section, key, "", buf, buf.Capacity - 1, path)
        If len <= 0 Then Return ""
        Return buf.ToString(0, len)
      End Function
    End Module

    Tuesday, August 28, 2007 3:58 AM
    Moderator

All replies

  • Not only is it a gross mistake to pass a String instead of a StringBuffer, lying about it and saying the string is 10 characters long when it is actually only 6 long buys you a well deserved ticket to never-never land.  Visit www.pinvoke.net for the correct Declare statement and usage.
    Monday, August 27, 2007 11:28 PM
    Moderator
  • that was a typo..... of course it would be 10 length string..... Still the when the string is set to the correctly... The if statement is not recognizing the comparison even if true......

     

    Tuesday, August 28, 2007 1:58 AM
  • If you look real close in the debugger's Locals window, you'll see that "MYPROGRAM is missing the end-quote.  That's not a typo, there's an invisible character there.  The If statement sees it.  You must use a StringBuffer.  Using a ByRef String violates the immutability requirement of the String type.  Add a new module to your project and paste this code:

    Imports System.Text
    Module Module1
      Private Declare Auto Function GetPrivateProfileString Lib "kernel32.dll" (ByVal section As String, _
        ByVal key As String, ByVal defval As String, ByVal retval As StringBuilder, _
        ByVal bufsize As Integer, ByVal path As String) As Integer

      Public Function GetIniString(ByVal path As String, ByVal section As String, ByVal key As String) As String
        Dim buf As New StringBuilder(1024)
        Dim len As Integer = GetPrivateProfileString(section, key, "", buf, buf.Capacity - 1, path)
        If len <= 0 Then Return ""
        Return buf.ToString(0, len)
      End Function
    End Module

    Tuesday, August 28, 2007 3:58 AM
    Moderator
  • Thanks alot!!!! I tried my hardest to stay away from using another datatype but once I recoded with stringbuilder everthing works like a charm...... Your the man!

    Tuesday, August 28, 2007 6:40 PM