473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to evaluate a string expression to a boolean...

2 New Member
This is kind of a weird one and hard to find answers online because of the format of the question.

Essentially I want to be able to have a string that looks like this "True AND True AND True" and evaluate that to a boolean which in this case would be True. Another example would be "(True or False) AND True" would be True. The reason for this is that I have a form where the user has to build some logic in a graphical interface. I can evaluate the logic and create a string but I don't know how to evaluate the string. The problem is that they can also choose items such as OR\AND\NOT which is hard to account for in code.

BTW I am working in VB .Net.


HELP!!
Jan 24 '08 #1
2 5082
yarborg
2 New Member
I think I have this figured out. I borrowed some C# code that is similar and converted to VB .Net. I still don't completely understand why it works but I have verified that it does.

Private Function Eval(ByVal expression As String) As Boolean
expression = expression.ToLo wer
expression = expression.Repl ace("false", "0")
expression = expression.Repl ace("true", "1")
expression = expression.Repl ace(" ", "")
Dim temp As String

Do
temp = expression
expression = expression.Repl ace("(0)", "0")
expression = expression.Repl ace("(1)", "1")
expression = expression.Repl ace("0and0", "0")
expression = expression.Repl ace("0and1", "0")
expression = expression.Repl ace("1and0", "0")
expression = expression.Repl ace("1and1", "1")
expression = expression.Repl ace("0or0", "0")
expression = expression.Repl ace("0or1", "1")
expression = expression.Repl ace("1or0", "1")
expression = expression.Repl ace("1or1", "1")
Loop While temp <> expression

If expression = "0" Then
Return False
End If
If expression = "1" Then
Return True
End If
Throw New ArgumentExcepti on("expression" )
End Function

Usage Example:
Eval("(True or True or False) and True")

Returns True as a boolean
Jan 24 '08 #2
Plater
7,872 Recognized Expert Expert
Sounds like you are creating a regular grammer for yourself.
Seems like a workable enough solution.
Jan 24 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1587
by: Bagger Vance | last post by:
The code below does not work -- but is there a way to do this? I want to both read a line and string sline; StreamReader m_streamReader = new StreamReader(fi); while(sline=m_streamReader.ReadLine()) Debug.WriteLine(sline);
1
3098
by: David Laub | last post by:
I have no problems running the following dynamic XPath evaluator form MSXSL: <msxsl:script implements-prefix="dyn" language="jscript"> evaluate(context, expression) { return context.nextNode().selectNodes(expression);
22
30023
by: Paminu | last post by:
As I remember if(1) evaluates to true and all other numbers including 0 evaluate to false. But where do I find out about this for sure?? I have looked through K&R, all the C for dummies books and various other C programming books but nowhere there is a mention on what a number in an if statement evaluates to. Is this some kind of big secret?
3
8024
by: Coco | last post by:
Hi! Does c# has any class method that will be able to evaluate a given string e.g ((True && False) || (True && True)) and return the result in boolean for this case is true Thanks!
13
33869
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
8
13129
by: No Such Luck | last post by:
Is there anyway to literally evaluate the contents of a string in an if statement? For example: int i = 0; char * str = "i == 0"; if(str) /* I know this doesn't do what I want */ {
3
1303
by: willempie | last post by:
hi all, in FoxPro we know a function EVALUATE(xxxx) . This function evaluates the contents of xxxx. for example EVALUATE("X>10") will give True if X is greater then 10. is there a such kind of function in VB.NET ?? i want to create a string which should be evaluated.
2
12343
kadghar
by: kadghar | last post by:
Many people asks if there is a way to write a mathematical expression, writen as a string in a text box, so they can do something like: sub something_click() textbox2.text=eval(textbox1.text) end sub Well, of course it's posible, and can be done with some characters mannaging. This way you can complicate it as much as you want. The way i usualy do it is in 5 simple steps: 1. Create 2 string arrays, and save numbers in one and...
1
6546
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private static Stack operators = new Stack(); private static Stack operands = new Stack();
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.