logic gates in VB.NET 2005

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I just recently got VB.NET 2005 Express Edition. How do I represent logic
gates (AND, OR, NOT, etc.) in VB? I considered a function because functions
return a value. For example, a 2-input AND gate function would have 2 input
parameters & 1 output. But suppose that I want to build a "circuit" using
different gates. I can't have a bunch of functions representing an AND gate.
I don't want to have to create a separate function every time I need another
AND gate. I considered a logic gate object. I can have more than 1 instance
of an AND object running at the same time, can't I? Can objects be linked
together? In other words, can the output of 1 AND object be used as the input
to another AND object? Can these logic gate objects be dynamic? In other
words, can I use the logic gate objects to create different "circuits"
instead of being "hardwired"? Thank you.
 
You don't need to have multiple objects or functions to accomplish what you
want, I don't think.

For example:

Function AndGate (in1 as object, in2 as object) as object
AndGate = in1 ANd in2
end function

Function OrGate (in1 as object, in2 as object) as object
OrGate = in1 OR in2
end function

Dim Ans as object = AndGate(OrGate(AndGate(In1, In2), OrGate(In3, In4)),
OrGate(In5, AndGate(In6, In7)))


Tom
 
pcnerd said:
In other words, can I use the logic gate objects to create different
"circuits" instead of being "hardwired"?

Yes, but you need to think about how you're going to be able to "plug"
different things (i.e. Gates and Values) together.

For example, an AND Gate takes two inputs, but these might be two simple
Values or two other Gates or a mixture. So you have to be able to treat
a Value and a Gate in "the same" way.

Here's one suggestion:

Interface IBool
' Any IBool object has a Value
Function Value as Boolean
End Interface

Class BooleanValue
Implements IBool

Public Sub New(ByVal bValue as Boolean)
m_bValue = bValue
End Sub

' The "Value" is simply the value we created it with
Public Function Value() As Boolean _
Implements IBool.Value

Return m_bValue
End Property

Private m_bValue As Boolean = False

End Class


Class ANDGate
Implements IBool

Public Sub New(
ByVal oValue1 as IBool _
, ByVal oValue2 as IBool _
)
m_oValue1 = oValue1
m_oValue2 = oValue2
End Sub

' The "Value" is calculated from the Value functions
' on each object (operand)
Public Function Value() As Boolean _
Implements IBool.Value

Return m_oValue1.Value And m_oValue2.Value
End Property

Private m_oValue1 As IBool = Nothing
Private m_oValue2 As IBool = Nothing

End Class

So then you can set up "circuits" like this:

Dim oYes as New BooleanValue(True)
Dim oMaybe as BooleanValue _
= FunctionThatReturnsABooleanValue()

Dim oAND1 as New ANDGate(oYes, oNo)

Console.Writeline(oAND1.Value())

HTH,
Phill W.
 
HUH???
Thanks for the info, but that's all Greek to me. I'm a VB.NET beginner. My
philosophy is KISS - Keep it simple, stupid. What is IBool? I hope that I can
come up with a simpler solution, like maybe a truth table array of type
Boolean. Since we're on the subject of objects - can there be more than one
object of the same name? If so, how does VB keep track of them & distinguish
one from the other? Thank you.
 
pcnerd said:
HUH???
Thanks for the info, but that's all Greek to me. I'm a VB.NET beginner. My
philosophy is KISS - Keep it simple, stupid.

And trying to get to grips with .Net? You /do/ like a challenge, don't
you ;-)
What is IBool?

IBool is an Interface that I've defined. You'll hear people talk about
an Interface being a "contract" between two separate bits of code.
In this case I've defined an Interface that, no matter what Object I
find it in, will always be able to give me a "Value" of type Boolean.
I hope that I can come up with a simpler solution, like maybe a truth table
array of type Boolean.

That /really/ depends what you're trying to achieve.

OK, so you set up a Truth Table, but then how do you use it?
As I recall, you asked for "logic gate /objects/" from which you could
construct "different circuits". Paste the code into a console
application and try it; take a little time to read around the code (in
MSDN) and see if(?) it makes any more sense after that.
Since we're on the subject of objects - can there be more than one
object of the same name?

Can you have two classes with the same name (in the same Namespace)?
No.

Public Class AndGate
End Class
Public Class AndGate
End Class

Can you have more than one instance of an object that has a particular
name?
Oh Yes.

Dim x As New BooleanValue( True )
Dim y As New BooleanValue( False )

x and y are both BooleanValue objects, but are totally separate from one
another.
If so, how does VB keep track of them & distinguish one from the other?

Beats me - it just does. ;-)
Seriously, I could go off rattling on about Pointers (oops!) and memory
allocation and Garbage Collection and all sorts of other wierd stuff;
but not today - it can and it does.

HTH,
Phill W.



Thank you.
 
One of my questions was :

Your reply was:
Can you have more than one instance of an object that has a particular
name?
Oh Yes.

Dim x As New BooleanValue( True )
Dim y As New BooleanValue( False )

x and y are both BooleanValue objects, but are totally separate from one
another.

Variables x & y don't have the same name. I thought about it after I sent
the e-mail. So, I answered my own question. It's not possible to have 2
variables of the same name. VB would be confused. As far as I know, there
cannot be 2 of anything with the same name - variable, object, etc. VB would
be confused & not be able to distinguish one from the other.

Maybe, an array of Structures. Each Structure would be a different gate -
AND, OR, NOT, NAND, XOR. I don't know how to implement logic gates in
VB.NET. But your suggestion is confusing to me. I hope that there is a simple
solution. I'll have to keep on looking. Maybe, I'll find the answer on the
'net. Thank you.
 
Back
Top