none
Get a Variable from String in Text File RRS feed

  • Question

  • I have created a forms app that generates Periodic Tables, I also made a Console version because it can generate a larger image before running out of memory. The Forms App has a nice fancy interface to set custom colors for each Group of Elements. I can easily create a Forms app to create a "config" file that the Console App can read to use those customized colors. Here is what the prototype config looks like:

    NobleGasesB=#ADD8E6
    NobleGasesF=#000000
    HalogensB=#FFFF00
    HalogensF=#000000
    AlkaliMetalsB=#FF0000
    AlkaliMetalsF=#000000
    AlkalineMetalsB=#FFC0CB
    AlkalineMetalsF=#000000
    TransitionMetalsB=#D3D3D3
    TransitionMetalsF=#000000
    PostTransitionMetalsB=#808080
    PostTransitionMetalsF=#000000
    LanthanidesB=#4169E1
    LanthanidesF=#000000
    ActinidesB=#FA8072
    ActinidesF=#000000
    NonMetalsB=#D2B48C
    NonMetalsF=#000000
    MetaloidsB=#40E0D0
    MetaloidsF=#000000
    UnknownB=#F5DEB3
    UnknownF=#000000
    

    On each line, the left side is the exact Variable name to be set, the right side is the HTML color code. My question is - Is there a way to get the correct variable name directly from the config file. I know I can split each line and do a loop checking each name but I'm hoping to just take the name from the config file and set its value accordingly. Hope this is clear...
    I want to read this line:
    NobleGasesB=#ADD8E6
    split it, yielding two strings - "NobleGasesB" and "#ADD8E6"
    then set the actual variable in the app "NobleGasesB" to the color using ColorTranslator.FromHtml("#ADD8E6")

    Wednesday, November 28, 2018 7:50 PM

Answers

  • I was looking for a way to translate the Variable Name to the Actual Variable, so the file can just be read line by line and the variables assigned in one "easy" step.

    Check an example of Console application:

    Imports System.Drawing
    
    Module Module1
    
        Dim NobleGasesB As Color
        Dim NobleGasesF As Color
    
    
        Sub Main()
    
            Dim name As String = "NobleGasesB"
            Dim color As Color = ColorTranslator.FromHtml("#ADD8E6")
    
            Dim f = GetType(Module1).GetField(name, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
    
            f.SetValue(Nothing, color)
    
        End Sub
    
    End Module
    


    It sets the NobleGasesB variable.

    • Edited by Viorel_MVP Thursday, November 29, 2018 6:17 AM
    • Marked as answer by Devon_Nullman Sunday, December 2, 2018 2:33 AM
    Thursday, November 29, 2018 6:15 AM

All replies

  • I suggest you to use *.ini file

    Then use API function to read *.ini file

    <System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto)>
    Public Shared Function GetPrivateProfileString(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    End Function


    Public Function GetData(ByRef Section As String, ByRef Key As String, ByRef [Default] As String) As String
      ' Returns a string from your INI file
      Dim IntCharCount As Integer
      Dim Result As New String(Nothing, BufferSizeValue)
      IntCharCount = GetPrivateProfileString(Section, Key, [Default], Result, BufferSizeValue, IniFileName)
      If IntCharCount > 0 Then Return Microsoft.VisualBasic.Left(Result.ToString, IntCharCount) Else Return String.Empty
    End Function

    Private BufferSizeValue As Integer = 256
    Private IniFileName As String = "C:\Config.ini"
    
    GetData("Colors", "NobleGasesB", "#ADD8E6")


    Hope this help

    Xan To

    • Proposed as answer by Xan To Wednesday, November 28, 2018 8:09 PM
    • Edited by Xan To Wednesday, November 28, 2018 8:47 PM
    • Unproposed as answer by KareninstructorMVP, Moderator Wednesday, November 28, 2018 9:58 PM
    Wednesday, November 28, 2018 8:05 PM
  • I have an XML file of the periodic table that has a spot for color.  Don't know if it helps.

    Get it here  PeriodicT


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Wednesday, November 28, 2018 9:48 PM
  • All I see there is:

    <color />

    Wednesday, November 28, 2018 11:28 PM
  • I was looking for a way to translate the Variable Name to the Actual Variable, so the file can just be read line by line and the variables assigned in one "easy" step.

    Wednesday, November 28, 2018 11:31 PM
  • I was looking for a way to translate the Variable Name to the Actual Variable, so the file can just be read line by line and the variables assigned in one "easy" step.

    Check an example of Console application:

    Imports System.Drawing
    
    Module Module1
    
        Dim NobleGasesB As Color
        Dim NobleGasesF As Color
    
    
        Sub Main()
    
            Dim name As String = "NobleGasesB"
            Dim color As Color = ColorTranslator.FromHtml("#ADD8E6")
    
            Dim f = GetType(Module1).GetField(name, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
    
            f.SetValue(Nothing, color)
    
        End Sub
    
    End Module
    


    It sets the NobleGasesB variable.

    • Edited by Viorel_MVP Thursday, November 29, 2018 6:17 AM
    • Marked as answer by Devon_Nullman Sunday, December 2, 2018 2:33 AM
    Thursday, November 29, 2018 6:15 AM
  • All I see there is:

    <color />

    Sorry, try this link  Periodic Table XML

    I have a program that uses it



    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.


    • Edited by dbasnett Thursday, November 29, 2018 5:07 PM
    Thursday, November 29, 2018 4:29 PM
  • I was looking for a way to translate the Variable Name to the Actual Variable, so the file can just be read line by line and the variables assigned in one "easy" step.

    Check an example of Console application:

    Imports System.Drawing
    
    Module Module1
    
        Dim NobleGasesB As Color
        Dim NobleGasesF As Color
    
    
        Sub Main()
    
            Dim name As String = "NobleGasesB"
            Dim color As Color = ColorTranslator.FromHtml("#ADD8E6")
    
            Dim f = GetType(Module1).GetField(name, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
    
            f.SetValue(Nothing, color)
    
        End Sub
    
    End Module
    


    It sets the NobleGasesB variable.

    Thanks, that was exactly what I was looking for.
    Sunday, December 2, 2018 2:55 AM