Converting Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John MacDonald

    #1

    Converting Variables

    I need to convert a string to a integer. I tryed to use the Val() Function
    but the i need the string to be added. The string is "(x+3)^2" where the x
    is a part of the for/next statement. And i used a statement like y =
    replace(y, "x", x) to replace the x in the tring to a number. But when I use
    the Val() function it only grabs the first number. Is there a function or
    piece of code that i can use to change the sting to a integer and doing the
    math? It would be greatly appreciated.

    -Julien


  • Trousle Undrhil

    #2
    Re: Converting Variables

    "John MacDonald" <jmjadf@sympati co.ca> wrote in message
    news:4ACTb.5160 $bp1.198318@new s20.bellglobal. com...[color=blue]
    > I need to convert a string to a integer. I tryed to use the Val() Function
    > but the i need the string to be added. The string is "(x+3)^2" where the x
    > is a part of the for/next statement. And i used a statement like y =
    > replace(y, "x", x) to replace the x in the tring to a number. But when I[/color]
    use[color=blue]
    > the Val() function it only grabs the first number. Is there a function or
    > piece of code that i can use to change the sting to a integer and doing[/color]
    the[color=blue]
    > math? It would be greatly appreciated.
    >
    > -Julien
    >[/color]

    Can you post the exact code snippet for what you are trying to do? It might
    help us help you, you know? :)

    Ken
    undrhil@hotmail .nospam.com


    Comment

    • Trousle Undrhil

      #3
      Re: Converting Variables

      "John MacDonald" <jmjadf@sympati co.ca> wrote in message
      news:oHUTb.7082 $bp1.313639@new s20.bellglobal. com...[color=blue]
      > I understand that no one understands the problem that i am having do to
      > the explination so I created a small piece of
      > code that contains my problem. I have attached the program to this[/color]
      message[color=blue]
      > and i have wrote out the program
      > below if you dont trust downloading files do to the virus going around.
      >
      > -In Visual Basic 6.0 create a form.
      > -Add to this form a TextBox a CommandButton and a ListBox
      > -then add this peace of code to the program
      >
      > Private Sub Command1_Click( )
      >
      > y = Text1.Text
      > For x = 1 To 10
      > z = Replace(y, "x", x)
      > List1.AddItem y[/color]

      OK, this is part one. When you type the assignment statement
      'z=Replace(y,"x ",x) that is telling Visual Basic to search the string (y)
      for the value "x" and replace it with (x) and then place the resultant
      string into (z) ... so change z to z1 and replace the y in the next line
      (List1.AddItem y) to z1. That will at least have the Listbox showing the
      formula correctly.
      [color=blue]
      > 'At this point i would like to do the math that is in
      > 'Text1.Text
      > z = Val(y)[/color]

      All Val() does is return the numeric portions of a string minus any
      extrenuous characters. In this case, Val("(1+3)^2") sees the first
      parenthesis and returns the value of 0 since it didn't see any numbers
      before that parenthesis. If you remove the parenthesis, it will only return
      the value which is before the + symbol, since it doesn't recognize that as a
      number. Also, the (y) here should be replaced with (z1) as per the previous
      explanation.

      In order to do what you are trying to do, you would have to parse the string
      yourself and perform any math functions involved. I am not aware of any
      functions included with VB which will parse a string and perform the math
      for you. I am sure than someone else in this group could tell me if I am
      wrong about that ... so let them speak now or forever hold their peace.
      [color=blue]
      > 'Should state the answer to the above AddItem in List1
      > List1.AddItem z
      > Next x
      >
      > 'This is not the program that i am stuck on but this gives
      > 'an example where I run into my problem
      > 'Help Would be appresiated
      > 'Julien
      > End Sub
      >
      > -when you run the program input the equation "(x+3)^2"
      >
      > The problem that I am having is that the number under the equation[/color]
      should[color=blue]
      > be the answer but it keeps telling
      > me the answer is 0. If someone could help me it will be greatly
      > appresiated.
      >
      > Thank you,
      > Julien[/color]

      Ken
      undrhil@hotmail .nospam.com


      Comment

      • Larry Serflaten

        #4
        Re: Converting Variables

        "Trousle Undrhil" <undrhil@hotmai l.nospam.com> wrote
        [color=blue]
        > In order to do what you are trying to do, you would have to parse the string
        > yourself and perform any math functions involved. I am not aware of any
        > functions included with VB which will parse a string and perform the math
        > for you. I am sure than someone else in this group could tell me if I am
        > wrong about that ... so let them speak now or forever hold their peace.[/color]


        Dim Z

        With ScriptControl1
        .AddCode "X = 3"
        .AddCode "Y = 1"
        Z = .Eval("(X + Y) ^ 2")
        End With

        MsgBox Z ' 16


        The ScriptControl can parse the string and evaluate the expression.

        LFS




        -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
        http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
        -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

        Comment

        • John MacDonald

          #5
          Re: Converting Variables

          Every time i try to run your piece of code, i get an "Object Required"
          error. Is there a file that i need to download to let this piece of code
          work?

          Thank you so much everyone for your help.
          -Julien

          "Larry Serflaten" <Abuse@SpamBust ers.com> wrote in message
          news:402071d2_8 @corp.newsgroup s.com...[color=blue]
          > "Trousle Undrhil" <undrhil@hotmai l.nospam.com> wrote
          >[color=green]
          > > In order to do what you are trying to do, you would have to parse the[/color][/color]
          string[color=blue][color=green]
          > > yourself and perform any math functions involved. I am not aware of[/color][/color]
          any[color=blue][color=green]
          > > functions included with VB which will parse a string and perform the[/color][/color]
          math[color=blue][color=green]
          > > for you. I am sure than someone else in this group could tell me if I[/color][/color]
          am[color=blue][color=green]
          > > wrong about that ... so let them speak now or forever hold their[/color][/color]
          peace.[color=blue]
          >
          >
          > Dim Z
          >
          > With ScriptControl1
          > .AddCode "X = 3"
          > .AddCode "Y = 1"
          > Z = .Eval("(X + Y) ^ 2")
          > End With
          >
          > MsgBox Z ' 16
          >
          >
          > The ScriptControl can parse the string and evaluate the expression.
          >
          > LFS
          >
          >
          >
          >
          > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
          > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
          > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


          Comment

          • Larry Serflaten

            #6
            Re: Converting Variables

            Larry Serflaten
            [color=blue]
            > Every time i try to run your piece of code, i get an "Object Required"
            > error. Is there a file that i need to download to let this piece of code
            > work?[/color]

            The ScriptControl is not in the VB toolbox by default. You have to
            add it in from the Project>Compone nts menu (Ctrl-T) dialog. Once
            you add the control to the toolbox, you treat it like any other control,
            if you want to use it, you have to put one on the form.

            LFS




            -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
            http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
            -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

            Comment

            • Trousle Undrhil

              #7
              Re: Converting Variables


              "Trousle Undrhil" <undrhil@hotmai l.nospam.com> wrote in message
              news:HZYTb.2861 $Rl4.1941@bigne ws5.bellsouth.n et...[color=blue]
              > "John MacDonald" <jmjadf@sympati co.ca> wrote in message
              > news:oHUTb.7082 $bp1.313639@new s20.bellglobal. com...[/color]

              << SNIP >>
              [color=blue]
              > OK, this is part one. When you type the assignment statement
              > 'z=Replace(y,"x ",x) that is telling Visual Basic to search the string (y)
              > for the value "x" and replace it with (x) and then place the resultant
              > string into (z) ... so change z to z1 and replace the y in the next line
              > (List1.AddItem y) to z1. That will at least have the Listbox showing the
              > formula correctly.[/color]

              OK ... with the ScriptControl, this part is still the same.
              [color=blue][color=green]
              > > 'At this point i would like to do the math that is in
              > > 'Text1.Text
              > > z = Val(y)[/color]
              >
              > All Val() does is return the numeric portions of a string minus any
              > extrenuous characters. In this case, Val("(1+3)^2") sees the first
              > parenthesis and returns the value of 0 since it didn't see any numbers
              > before that parenthesis. If you remove the parenthesis, it will only[/color]
              return[color=blue]
              > the value which is before the + symbol, since it doesn't recognize that as[/color]
              a[color=blue]
              > number. Also, the (y) here should be replaced with (z1) as per the[/color]
              previous[color=blue]
              > explanation.[/color]

              Instead of using z = Val(z1), use z = ScriptControl1. Eval(z1). Then
              follow-up with the rest of the code to add z to the listbox.
              [color=blue]
              > In order to do what you are trying to do, you would have to parse the[/color]
              string[color=blue]
              > yourself and perform any math functions involved. I am not aware of any
              > functions included with VB which will parse a string and perform the math
              > for you. I am sure than someone else in this group could tell me if I am
              > wrong about that ... so let them speak now or forever hold their peace.[/color]

              This is working now thanks to Larry Serflaten and the idea of using the MS
              Script Control! :) :) :)
              [color=blue][color=green]
              > > 'Should state the answer to the above AddItem in List1
              > > List1.AddItem z
              > > Next x
              > >
              > > 'This is not the program that i am stuck on but this gives
              > > 'an example where I run into my problem
              > > 'Help Would be appresiated
              > > 'Julien
              > > End Sub
              > >
              > > -when you run the program input the equation "(x+3)^2"
              > >
              > > The problem that I am having is that the number under the equation[/color]
              > should[color=green]
              > > be the answer but it keeps telling
              > > me the answer is 0. If someone could help me it will be greatly
              > > appresiated.
              > >
              > > Thank you,
              > > Julien[/color][/color]

              Ken
              undrhil@hotmail .nospam.com


              Comment

              • John MacDonald

                #8
                Re: Converting Variables

                Thank you very much my program is working great now. Thank you Larry and
                Ken.

                -Julien

                "Larry Serflaten" <Abuse@SpamBust ers.com> wrote in message
                news:40207b8a_8 @corp.newsgroup s.com...[color=blue]
                > Larry Serflaten
                >[color=green]
                > > Every time i try to run your piece of code, i get an "Object[/color][/color]
                Required"[color=blue][color=green]
                > > error. Is there a file that i need to download to let this piece of[/color][/color]
                code[color=blue][color=green]
                > > work?[/color]
                >
                > The ScriptControl is not in the VB toolbox by default. You have to
                > add it in from the Project>Compone nts menu (Ctrl-T) dialog. Once
                > you add the control to the toolbox, you treat it like any other control,
                > if you want to use it, you have to put one on the form.
                >
                > LFS
                >
                >
                >
                >
                > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


                Comment

                Working...