I wonder about how I can store a part of code in a string field of an object and convert it at run-time in executable code.
Let's say I have my class:
Public Class Car
Public m_IDCar As String
Public m_Brand As String
Public m_Description As String
Public m_Condition As String ' => here I need to store an If, or an If condition as a String, that will be executed at run-time.
End Class
Then the code that happens when CommandButton1.Click()
:
Dim carList As List(Of Car)
Dim parameter as String
' here I create carList with data from a database, so for each car In the database I create its relative object, with its .m_IDCar, .m_Brand, .m_Description and .m_Condition, and add it to carList
parameter = TextBox1.Text ' => given in input by the user
For each car As Car in carList
If (car.m_Condition = True) then ' => here there must be something to do cause, as things are now, in car.m_Condition is stored a String, but I need to parse it in code that returns a boolean value and, if this value would be True, the code will enter in the If statement.
'do something
End If
Next
Example of car.m_Condition
could be:
car.m_Condition = "(car.m_Name=""BMW"" AND car.m_Description.Contains(parameter)) OR car.m_Brand=""AUDI"""
I need some tips on how to implement this approach, if someone would help me.
TY!
EDIT: Thanks David, I have seen that question and seems a lot similar. I've seen that the user asked how store the entire If in a string:
Dim code As String = "IIf(1 = 2, True, False)"
(the users that asked that question used IIf)
The best way for me would be to store the condition to evaluate in a string and, maybe, the value to with compare the result in another one. So, for example:
Dim condition As String = "(car.m_Name=""BMW"" AND car.m_Description.Contains(parameter)) OR car.m_Brand=""AUDI"""
Dim valueToCompareWith as String = "True"
and the following If
If (car.m_Name=""BMW"" AND car.m_Description.Contains(parameter)) OR car.m_Brand=""AUDI"" = True) Then
'do something
End If
will become:
If (condition = valueToCompareWith) Then '(conceptually, because in this form it's simply a String comparison that returns always False)
'do something
End If
EDIT2:
Thanks Plutonix, I've read about your hint on Getters and Setters, I've not specified it, but the context I'm working on is a lot more complex then this. I've made a very simple example just for focus the problem, but I'm working with a good amount of objects, with various fields to compare and for every comparison there are different conditions in the If, with different logics and different types of data to compare.