Variables within variables 
Author Message
 Variables within variables

You might have guessed what I'm trying to do right now from my previous
posts but here is another problem.
I need people to be able to enter in a text box something similar to: "X ^
2" the problem is I need to make the X a variable, square it then use it in
my code. If I have txtEqn.Text in my code in the place of X ^ 2 then it
gives me a type mismatch error. How I do I implement variables within
variables?

Thanks

Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Variables within variables
Alex,

I'm not sure I really understand your question.  In the TextBox will be
something like "12^2" and you want to produce the answer?

I would use the VB6 function Split() like this:

Private Sub Command2_Click()
    Dim s() As String
    Dim mymantissa As Integer
    Dim myexponent As Integer
    Dim myresult As Integer

    Dim i As Integer

    If InStr(1, txtEqn.Text, "^", vbTextCompare) <> 0 Then
        s() = Split(txtEqn.Text, "^", 2, vbTextCompare)
    Else
        Beep
        MsgBox "Illegal formula"
        Exit Sub
    End If

    mymantissa = CInt(s(0))
    myexponent = CInt(s(1))

    myresult = mymantissa

    For i = 1 To myexponent - 1
        myresult = myresult * mymantissa
    Next i

    Label2 = myresult
End Sub

This code doesn't guard against the user from entering "z^x" nor does it
check for overflows of the integers, but you get the idea...

I hope I got it right.

Michael


Quote:
> You might have guessed what I'm trying to do right now from my previous
> posts but here is another problem.
> I need people to be able to enter in a text box something similar to: "X ^
> 2" the problem is I need to make the X a variable, square it then use it
in
> my code. If I have txtEqn.Text in my code in the place of X ^ 2 then it
> gives me a type mismatch error. How I do I implement variables within
> variables?

> Thanks

> Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Variables within variables
Ok maybe I was a bit ambiguous.

here is My code without any use of the Text box:

Private Sub Command1_Click()

Dim X As Double

For X = ScaleLeft To ScaleWidth Step ScaleWidth / (ScaleWidth * 1825)
    PSet (X, (X ^ 3) * -1), QBColor(4)
Next X
End Sub

Notice the (X ^ 3) bit. You can Ignore the rest.
I would Like to replace that with (txtEqn.Text). Ok?
Now txtEqn would hold an equation such as X ^ 3 or X ^ 2 or X + 3 or just 3
X would be the only variable in the textbox if there is one at all.
taking the code above and replacing X ^ 3 with txtEqn.Text where txtEqn.Text
= "X ^ 3" doesn't work. How can I make it work?

Thanks again

Alex


Quote:
> Alex,

> I'm not sure I really understand your question.  In the TextBox will be
> something like "12^2" and you want to produce the answer?

> I would use the VB6 function Split() like this:

> Private Sub Command2_Click()
>     Dim s() As String
>     Dim mymantissa As Integer
>     Dim myexponent As Integer
>     Dim myresult As Integer

>     Dim i As Integer

>     If InStr(1, txtEqn.Text, "^", vbTextCompare) <> 0 Then
>         s() = Split(txtEqn.Text, "^", 2, vbTextCompare)
>     Else
>         Beep
>         MsgBox "Illegal formula"
>         Exit Sub
>     End If

>     mymantissa = CInt(s(0))
>     myexponent = CInt(s(1))

>     myresult = mymantissa

>     For i = 1 To myexponent - 1
>         myresult = myresult * mymantissa
>     Next i

>     Label2 = myresult
> End Sub

> This code doesn't guard against the user from entering "z^x" nor does it
> check for overflows of the integers, but you get the idea...

> I hope I got it right.

> Michael



> > You might have guessed what I'm trying to do right now from my previous
> > posts but here is another problem.
> > I need people to be able to enter in a text box something similar to: "X
^
> > 2" the problem is I need to make the X a variable, square it then use it
> in
> > my code. If I have txtEqn.Text in my code in the place of X ^ 2 then it
> > gives me a type mismatch error. How I do I implement variables within
> > variables?

> > Thanks

> > Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Expanding a variable or referencing a field within variable

2. print variable, variable, variable etc

3. Declaring variables within if statements

4. using data variables within SQL

5. How do I place a value of variable within a document

6. Bolding some words within a string assigned to a variable

7. simply setting of a variable within Sub fails

8. PLEASE HELP: Using Variables within SQL Queries

9. How to create a Timer variable (within a module)

10. single quotes within variables?

11. Easy one, but I can't find it - Searching within a variable for a string

12. Using a variable within WshShell object and Run method (newbie at work)

 

 
Powered by phpBB® Forum Software