none
String Substitution in VB.Net RRS feed

  • Question

  • Is there an operator in VB.Net equivalent to the "&" (not the concatenate usage) operator in FoxPro where "&VariableName" is the string content of the variable, "VariableName" and can be used the same as the string content itself?

    DAllenCohen

    Wednesday, February 17, 2016 6:43 PM

Answers

  • A simplified equivalent would be: Foln = "FOL" + str(n)/For n=1 to 10 / Q(n) = 3*n+2 / &Foln = Q(n) / EndFor     This would yield:  FOL1 = 5, FOL2 = 8, ..., FOL10 = 32

    The formula is not the issue - the real issue is why do you need it to be in a string variable?

    For instance, in VFP the usual reason is that the function is a data item that is part of the data record.  That is, the processing that is specific to that particular item is part of the data record for that item, and that processing could be anything at all.

    If you have a limited number of possible processing requirements, then you can code each in your application as a named function, store the name for the required function with each record, and use 'call by name' as mentioned above.

    Or, there might be a value in the data item that allows selection amongst a limited number of processing option.  Select Case would be useful for that.

    But if the macro code really is arbitrary then is not much available.  If you know that the expression is always a formula with a specific format, you can use some SQL facilities. There is an example here:
    https://technet.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.variable.evaluateasexpression(v=sql.105).aspx

      

    • Marked as answer by DAllenCohen Thursday, February 18, 2016 12:51 AM
    Thursday, February 18, 2016 12:15 AM
  • For this kind of substitution, you had to use the old trick of vb6 with collecction, but in .net it equivelant is HashTable

            Dim oHT As New Hashtable
            Dim Key = ""
    
            For I = 1 to 10
                Key = "FOL" + I.ToString
                oHT.Add(Key,3*I+2 )
            Next
            MsgBox(oHT("FOL1")) ' 5
            MsgBox(oHT("FOL5")) ' 8
            MsgBox(oHT("FOL10")) ' 32
    As I said before, I am 10 years VFP programmer, there is no direct equivalent in .net, each "&" substitution problem has a different way of solving it.


    .Net Blog VFP Blog

    • Marked as answer by DAllenCohen Thursday, February 18, 2016 12:50 AM
    Thursday, February 18, 2016 12:35 AM

All replies

  • Is there an operator in VB.Net equivalent to the "&" (not the concatenate usage) operator in FoxPro where "&VariableName" is the string content of the variable, "VariableName" and can be used the same as the string content itself?

    DAllenCohen

    Not being familiar with FoxPro, I have no idea what the "&" does. However, all .Net objects have a ToString method that will return some kind of string representation of the object. What that string representation is depends on the type of object, but in most cases it is useful.

    For example with numeric types (such as Integer, Double, Decimal), ToString returns a string representation of the number. For DateTime types, ToString returns a string representation of the date and time. In many cases (including the examples I just gave), ToString has an optional format string argument that specifies the desired format (see Standard Numeric Format Strings and Standard Date and Time Format Strings).

    Wednesday, February 17, 2016 6:50 PM
  • It is always difficult if persons ask "Is there a shop likewise in my village in New York".

    However, after searching on Google I saw that this is a kind of replacement for macro names

    If you study a little bit OOP, then you would see that it is in a better way replaced by OOP

    You create a class and simply do.

    Dim x as New MyClass, MyClass can be used endless times with endless times. One of differences is, that the instances are then pieces of code which are only in memory if they are needed (when it is well done). With scripting way of handling this it are mostly endless pieced of codes which are compiled at runtime. 

      


    Success
    Cor


    • Edited by Cor Ligthert Wednesday, February 17, 2016 6:56 PM
    Wednesday, February 17, 2016 6:55 PM
  • Is there an operator in VB.Net equivalent to the "&" (not the concatenate usage) operator in FoxPro where "&VariableName" is the string content of the variable, "VariableName" and can be used the same as the string content itself?

    DAllenCohen

    What you're asking might be interpreted several ways, but the way I'm taking it, you just use the name of the variable itself.

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
    
            Const aString As String = "Something"
    
            Dim anotherString As String = aString & " Else"
    
            MessageBox.Show(anotherString)
    
            Stop
    
        End Sub
    End Class

    Is that what you mean?


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Wednesday, February 17, 2016 7:25 PM
  • Is there an operator in VB.Net equivalent to the "&" (not the concatenate usage) operator in FoxPro where "&VariableName" is the string content of the variable, "VariableName" and can be used the same as the string content itself?+

    The nearest equivalent is Calling a Property or Method Using a String Name.  There is also a facility  to compile and execute code contained in a variable, but that is significantly more functionality than the Foxpro macro expansion, and a lot more complex to implement.

    Wednesday, February 17, 2016 8:32 PM
  • Is there an operator in VB.Net equivalent to the "&" (not the concatenate usage) operator in FoxPro where "&VariableName" is the string content of the variable, "VariableName" and can be used the same as the string content itself?+

    The nearest equivalent is Calling a Property or Method Using a String Name.  There is also a facility  to compile and execute code contained in a variable, but that is significantly more functionality than the Foxpro macro expansion, and a lot more complex to implement.


    I guess that the right answer should be NO ... There are no textual macro substitution in VB.

    Luc

    Wednesday, February 17, 2016 8:46 PM
  • Thanks, Frank.  I appreciate your answer but it doesn't quite do what I'm looking for.  Let me give a hypothetical VB.Net approach:  Assume 'Substitute' as a valid dimension.  Dim &Fol AS Substitute / For n = 1 to 10 / &Fol = "Fol" + str(n) / Dim &Fol As String / next n.   Is there an equivalent way of doing this kind of substitution in VB.Net?


    DAllenCohen

    Wednesday, February 17, 2016 8:57 PM
  • 
    Also thanks to Blackwood and Cor Lighthert

    DAllenCohen

    Wednesday, February 17, 2016 8:59 PM
  • Thanks, Frank.  I appreciate your answer but it doesn't quite do what I'm looking for.  Let me give a hypothetical VB.Net approach:  Assume 'Substitute' as a valid dimension.  Dim &Fol AS Substitute / For n = 1 to 10 / &Fol = "Fol" + str(n) / Dim &Fol As String / next n.   Is there an equivalent way of doing this kind of substitution in VB.Net?


    DAllenCohen

    The "As" keyword means that whatever "Substitute" is, it's a type (a class).

    Now I'm completely lost in what you're trying to accomplish. ;-)


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Wednesday, February 17, 2016 9:02 PM
  • ...content of the variable...

    The more I read the words, the more I think you're talking about a class (or structure). Am I wrong here?


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Wednesday, February 17, 2016 9:15 PM
  • There is no direct equivelant to that in .Net

    Post what you want to do in VFP, and I will try to tell you how to do it in vb.net



    .Net Blog VFP Blog

    Wednesday, February 17, 2016 9:58 PM
  • Thanks, Frank.  I appreciate your answer but it doesn't quite do what I'm looking for.  Let me give a hypothetical VB.Net approach:  Assume 'Substitute' as a valid dimension.  Dim &Fol AS Substitute / For n = 1 to 10 / &Fol = "Fol" + str(n) / Dim &Fol As String / next n.   Is there an equivalent way of doing this kind of substitution in VB.Net?


    DAllenCohen

    The "As" keyword means that whatever "Substitute" is, it's a type (a class).

    Now I'm completely lost in what you're trying to accomplish. ;-)


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    @ frank

    What he want the  equivalent in VB is

    In fox pro, you can create a string like that that contains some code

             vary = "IIf( VarX = 1, 10, 20)"

      vary is a string that contains some code, it is a "Marco"

    Then later in your code you can use the macro like this

           VarX = 5

           VarZ = &vary + 10

     Then VarZ will be 30 


    Luc


    • Edited by Crazypennie Wednesday, February 17, 2016 10:20 PM
    • Proposed as answer by Frank L. Smith Wednesday, February 17, 2016 10:23 PM
    Wednesday, February 17, 2016 10:19 PM
  • Crazy,

    It sounds like it might could be done with a lambda expression in LINQ then?


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Wednesday, February 17, 2016 10:25 PM
  • Crazy,

    It sounds like it might could be done with a lambda expression in LINQ then?


    Knowledge rests not upon truth alone, but upon error also. Carl Jung

    Or with strings that contains Java script or vbScript

    I had posted how to use VB and Java script from . net a coupe years ago. (Can't find the thread right now ... I am on my iPhone)


    Luc

    Wednesday, February 17, 2016 11:17 PM
  • Thanks again to Acamar, Crazypennie, Frank and Samir Ibrahim.  Crazypennie is correct in what I am looking for.  I am not familiar with LINQ or whether it will work with Visual Basic.  Samir suggested that I send  what I want in VFP.  A simplified equivalent would be: Foln = "FOL" + str(n)/For n=1 to 10 / Q(n) = 3*n+2 / &Foln = Q(n) / EndFor     This would yield:  FOL1 = 5, FOL2 = 8, ..., FOL10 = 32

    DAllenCohen

    Wednesday, February 17, 2016 11:46 PM
  • Luc,

    But what is that essential different from (beside that a name in VB has to start with an upper or lower chase letter.

    Module Module1
        Sub Main()
            Console.WriteLine(Y("And "))
            Console.ReadKey()
        End Sub
        Public Function Y(Seed As String) As String
            Return Seed & "Whatever"
        End Function
    End Module


    Success
    Cor

    Wednesday, February 17, 2016 11:55 PM
  • Frank, specifically regarding my hypothetical 'Substitute', as a valid AS, I believe would have to be its own  class for it to work the way it does in VFP.

    DAllenCohen

    Thursday, February 18, 2016 12:03 AM
  • A simplified equivalent would be: Foln = "FOL" + str(n)/For n=1 to 10 / Q(n) = 3*n+2 / &Foln = Q(n) / EndFor     This would yield:  FOL1 = 5, FOL2 = 8, ..., FOL10 = 32

    The formula is not the issue - the real issue is why do you need it to be in a string variable?

    For instance, in VFP the usual reason is that the function is a data item that is part of the data record.  That is, the processing that is specific to that particular item is part of the data record for that item, and that processing could be anything at all.

    If you have a limited number of possible processing requirements, then you can code each in your application as a named function, store the name for the required function with each record, and use 'call by name' as mentioned above.

    Or, there might be a value in the data item that allows selection amongst a limited number of processing option.  Select Case would be useful for that.

    But if the macro code really is arbitrary then is not much available.  If you know that the expression is always a formula with a specific format, you can use some SQL facilities. There is an example here:
    https://technet.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.variable.evaluateasexpression(v=sql.105).aspx

      

    • Marked as answer by DAllenCohen Thursday, February 18, 2016 12:51 AM
    Thursday, February 18, 2016 12:15 AM
  • I don't quite understand your question/suggestion.  The basic capability I am after is to be able systematically to change a variable label by changing a number and have that become a different variable, but refer to said variables generically as a single variable in an expression within a For, Next structure that sequences the number.

    DAllenCohen

    Thursday, February 18, 2016 12:26 AM
  • For this kind of substitution, you had to use the old trick of vb6 with collecction, but in .net it equivelant is HashTable

            Dim oHT As New Hashtable
            Dim Key = ""
    
            For I = 1 to 10
                Key = "FOL" + I.ToString
                oHT.Add(Key,3*I+2 )
            Next
            MsgBox(oHT("FOL1")) ' 5
            MsgBox(oHT("FOL5")) ' 8
            MsgBox(oHT("FOL10")) ' 32
    As I said before, I am 10 years VFP programmer, there is no direct equivalent in .net, each "&" substitution problem has a different way of solving it.


    .Net Blog VFP Blog

    • Marked as answer by DAllenCohen Thursday, February 18, 2016 12:50 AM
    Thursday, February 18, 2016 12:35 AM
  • I don't quite understand your question/suggestion.  The basic capability I am after is to be able systematically to change a variable label by changing a number and have that become a different variable, but refer to said variables generically as a single variable in an expression within a For, Next structure that sequences the number.

    If that's the task, it's easy - put that code into a method and call the method.  It's easy because you know at design time what you want to do.  Therefore you can code it.

    But if the definition of the task is only available at run time, and has to be extracted from that variables as a string and then converted into code, that's much more difficult.  It can be done.   But if it's not needed, then do the coding at design time, call the function, and the job is done.

    That's why the formula is not the issue - the issue is why the formula is only available as that variable.

    If you confirm that all you are trying to do is turn that expression into code you can use within your VB app, then it's possible to consider exactly what the code will look like.


    • Edited by Acamar Thursday, February 18, 2016 12:44 AM sp
    Thursday, February 18, 2016 12:39 AM
  • I want to thank all of you.  I am enough of  a neophyte  to admit that I am slow at understanding your suggestions but I think I understand enough.  Thanks again

    DAllenCohen

    Thursday, February 18, 2016 12:53 AM