0

Please help, I need to pass string to function with few optional parameters which needs to be replaced within string. Can someone help how can this be done, as im not to vb.net

String - "The <field1> is mandatory"
Variable - Employee Id
Output should be - "The Employee Id is mandatory"

Regards

2 Answers 2

2

You would be looking at the String.Replace method. something like this. Since strings are immutable it returns a new string with your corrected value you would then need to assign it to the old string.

Module Module1

    Sub Main()
        Dim test As String = "The <field1> is mandatory"
        Dim variable As String = "Employee Id"
        test = test.Replace("<field1>", variable)
        Console.WriteLine(test)
        Console.ReadLine()
    End Sub

End Module

You could also use the String.Format Method which uses composite formatting which will allow you to embed fields in your string then replace them with variables. something like this.

Dim test As String = "The {0} is a mandatory {1}"
Dim variable As String = "Employee Id"
Dim variable2 As String = "Field"

test = String.Format(test, variable, variable2)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this

Public Function cal(Byref id)
Dim str as String
str= "The "+id+" is mandatory"
return str
End Function

and call this function by

s=cal(id) //id is the value to be passed and s is the result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.