Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7865013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:57:31+00:00 2026-06-02T23:57:31+00:00

Consider following code: Dim S1 As String = a ‘this is the string in

  • 0

Consider following code:

    Dim S1 As String = "a"

    'this is the string in a file
    Dim StringFromFile As String = "S1=hello"

    Dim temp() As String = Split(StringFromFile, "=", -1, CompareMethod.Binary)
    'temp(0) = variable name
    'temp(1) = variable value

    'my question is: how to assign value to S1?

I have declared a string named S1. Now I want to assign new value to S1. The new string value is stored in a file using following format: [variable name][= as separator][string value]. How do I assign the value to S1 after retrieving the string variable name and value that stored in a file?

NOTE:

temp(0) = "S1"
temp(1) = "hello"

It should be noted that the string with the data comes from a file that may change from time to time! When the file changes, I want the variables to change as well.

Further clarification

I need a piece of code that when processing a string like this “S1=hello”, the code will first find a declared variable (i.e. S1), and then assign the S1 variable with “hello” string. The “=” just acted as separator for variable name and variable value.

UPDATE:

My attempt to use Mathias Lykkegaard Lorenzen’s EDIT 2 example but failed with “NullReferenceException” on this line "Field.SetValue(Me, VariableValue)". Please help me fix the problem. Following is my code based on Mathias Lykkegaard Lorenzen’s EDIT 2 example:

Public Sub Ask()
    Try
        Dim S1 As String = "a"

        Dim StringFromFile As String = "S1=hello"

        Dim temp() As String = Split(StringFromFile, "=", -1, CompareMethod.Binary)
        'temp(0) = variable name
        'temp(1) = variable value

        'my question is: how to assign value to S1?
        Dim TypeOfMe As Type = Me.GetType()

        'did this for readability.
        Dim VariableName As String = temp(0)
        Dim VariableValue As String = temp(1)

        'get the field in the class which is private, given the specific name (VariableName).
        Dim Field As FieldInfo = TypeOfMe.GetField(VariableName, BindingFlags.NonPublic Or BindingFlags.Instance)

        'set the value of that field, on the object "Me".
        Field.SetValue(Me, VariableValue) '<-- this line caused NullReferenceException
        MessageBox.Show(S1)
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:57:32+00:00Added an answer on June 2, 2026 at 11:57 pm

    You can use reflection to set S1 value:

    Imports System.Reflection
    
    Public Class Form1
        Public S1 As String = "a"
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim StringFromFile As String = "S1=hello"
    
            Dim temp() As String = Split(StringFromFile, "=", -1, CompareMethod.Binary)
            'temp(0) = variable name
            'temp(1) = variable value
    
            SetS1(Me, "S1", temp(1))
            MessageBox.Show(S1)
        End Sub
    
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="obj">the class that stores your S1 public field</param>
        ''' <param name="fieldName">that is your S1 field</param>
        ''' <param name="Value">S1 new value, that is hello</param>
        ''' <remarks></remarks>
        Public Sub SetS1(ByVal obj As Object, ByVal fieldName As String, ByVal Value As Object)
            Try
                Dim fi As FieldInfo = obj.GetType().GetField(fieldName, BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic)
                If Not fi Is Nothing Then
                    fi.SetValue(obj, Value)
                End If
    
            Catch ex As Exception
    
            End Try
        End Sub
    End Class
    

    Additional resource

    There is a book about reflection which is very good, take a look:

    Visual Basic .NET Reflection Handbook

    Reflection is a mechanism provided by .NET that enables developers to
    make their programs more flexible and dynamic. Reflection makes it
    possible for applications to be more modular, extensible, and
    configurable. Building on the basics of object-orientation and the
    .NET type system, reflection provides mechanisms for dynamically
    examining, modifying, and even creating objects at run time. .NET also
    adds the ability for programmers to add attributes to their types,
    which provide metadata about the type which can be examined and used
    through reflection at runtime.

    This book examines all the ways reflection can be used, and identifies
    practical applications and important programming techniques that rely
    upon reflection for their functionality. It covers the reflection API,
    the use of attributes in .NET, and also looks at the mechanisms .NET
    provides for dynamic generation of code – all techniques that allow
    developers to build more flexible, dynamic applications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code: Dim Working As Boolean = False Private Sub TreeView1_AfterCheck(ByVal sender
Consider the following code: Dim sql = SELECT * FROM MyTable WHERE value1 =
Consider following code for an android app; private String GetDistance(String src, String dest) {
Consider the following code: $('div').click(function(){ $(this).animate({height:100}, 500) $(this).css({opacity:1}); }); Versus: $('div').click(function(){ $(this).animate({height:100}, 500); })
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider the following code: Dim arr1 As New List(Of Double) Dim arr2 As New
Consider the following code in VB9: Dim text = Line1<br/>Line2 Dim markup = <span><%=
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider following code: ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(2134); aList.add(3423); aList.add(4234); aList.add(343); String tmpString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.