473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jul 17 '05 #1
7 2853
"John MacDonald" <jm****@sympatico.ca> wrote in message
news:4A*******************@news20.bellglobal.com.. .
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


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

Ken
un*****@hotmail.nospam.com
Jul 17 '05 #2
"John MacDonald" <jm****@sympatico.ca> wrote in message
news:oH*******************@news20.bellglobal.com.. .
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 message 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
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.
'At this point i would like to do the math that is in
'Text1.Text
z = Val(y)
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.
'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 should 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


Ken
un*****@hotmail.nospam.com
Jul 17 '05 #3
"Trousle Undrhil" <un*****@hotmail.nospam.com> wrote
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.

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! =-----
Jul 17 '05 #4
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" <Ab***@SpamBusters.com> wrote in message
news:40********@corp.newsgroups.com...
"Trousle Undrhil" <un*****@hotmail.nospam.com> wrote
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.

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! =-----

Jul 17 '05 #5
Larry Serflaten
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?


The ScriptControl is not in the VB toolbox by default. You have to
add it in from the Project>Components 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! =-----
Jul 17 '05 #6

"Trousle Undrhil" <un*****@hotmail.nospam.com> wrote in message
news:HZ*****************@bignews5.bellsouth.net...
"John MacDonald" <jm****@sympatico.ca> wrote in message
news:oH*******************@news20.bellglobal.com.. .
<< SNIP >>
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.
OK ... with the ScriptControl, this part is still the same.
'At this point i would like to do the math that is in
'Text1.Text
z = Val(y)


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.
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.
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.


This is working now thanks to Larry Serflaten and the idea of using the MS
Script Control! :) :) :)
'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

should
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


Ken
un*****@hotmail.nospam.com
Jul 17 '05 #7
Thank you very much my program is working great now. Thank you Larry and
Ken.

-Julien

"Larry Serflaten" <Ab***@SpamBusters.com> wrote in message
news:40********@corp.newsgroups.com...
Larry Serflaten
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?


The ScriptControl is not in the VB toolbox by default. You have to
add it in from the Project>Components 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! =-----

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
5
by: stephen | last post by:
Hi, I have been working using VB .NET and I wanted to convert a code to C# in VB .Net I use modules so that I can declare variables, STP holders so that I can replace them easily when I move b/w...
1
by: coolindienc | last post by:
I converted this program from using global variables to local variables. When I did that my while loop stopped working in my main function(module). Anyone, any idea why? I underlined the area where...
0
by: rpjd | last post by:
Apache2 over XP Home, PHP5, PostgreSQL8.2 If this is not the correct forum for this, it can be reposted accordingly. I have php variables/arrays that I want to display in a php webpage. What I am...
1
by: dishal | last post by:
Can anyone help me please? How do I convert these codes to launch from a JFrame instead of a Java Applet? A simple program where the user can sketch curves and shapes in a variety of...
12
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef ...
3
by: sparks | last post by:
After changing all the variables in a table from long to double. you get 9.1====becomes==== 9.19999980926514 ok I checked microsoft and they had a workaround. export the values to excel...then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.