Strange debugging behaviour using class library - vb.net

I have a VB.NET class library, ConsoleControls, which has a few class objects designed to imitate Forms objects in Console mode. For example, there is a ProgressBar class which draws an ASCII progress bar set at a certain value. I've been using this library in my main project, TheGame.
The code is functional, but when I use the class objects and methods in my main project, I've been getting strange behaviour when debugging. For example, when I call the ProgressBar class's Draw method, the yellow arrow showing which line is about to be executed appears at the End Sub for that method. Then of all things it starts running through a completely different class, TextBox, which has nothing to do with ProgressBar. It repeats certain lines multiple times. It's as if the debugger is running a completely different portion of code to what it shows me it's running.
Here's the ProgressBar class:
Public Class ProgressBar
Public InactiveColour As ConsoleColor
Public ActiveColour As ConsoleColor
Public Position As Point
Public Length As Integer
Public Value As Integer
Public MaxValue As Integer
Public Sub New(_Position As Point, _Length As Integer, _InactiveColour As ConsoleColor, _ActiveColour As ConsoleColor)
Position = _Position
Length = _Length
InactiveColour = _InactiveColour
ActiveColour = _ActiveColour
End Sub
Public Sub SetValue(_Value As Integer, _MaxValue As Integer)
Value = _Value
MaxValue = _MaxValue
End Sub
Public Sub IncrementValue()
If Value + 1 <= MaxValue Then Value += 1
End Sub
Public Sub DecrementValue()
If Value - 1 >= 0 Then Value -= 1
End Sub
Public Sub Draw()
Dim ActiveBars, InactiveBars As Integer
Dim Divisor As Double
Divisor = MaxValue / Length
ActiveBars = CInt(Value / Divisor)
InactiveBars = Length - ActiveBars
Console.SetCursorPosition(Position.X, Position.Y)
Console.ForegroundColor = ActiveColour
For i = 1 To ActiveBars
Console.Write(Block)
Next
Console.ForegroundColor = InactiveColour
For i = 1 To InactiveBars
Console.Write(Block)
Next
Console.ResetColor()
End Sub
End Class
And here's the code that does weird things in my main project:
Dim PlayerHealthBar As ProgressBar
PlayerHealthBar = New ProgressBar(New Point(30, 4), 32, ConsoleColor.DarkGray, ConsoleColor.White)
PlayerHealthBar.SetValue(1, 10)
PlayerHealthBar.Draw()
Any suggestions as to what may be going on?

Related

How do I declare a constant variable with a value of a function call

In a VBA module I have the following declaration of constants:
Private Const const_abc = 3000
Private Const const_def = 900
Private Const const_etc = 42
' and so on and so forth
Now, I have to initialize these values with a one time function call, ideally something like so
Private Const const_abc = someFunc(18)
Private Const const_def = someFunc( 7)
Private Const const_etc = someFunc( 5)
' and so on and so forth
Of course, this won't work in VBA. So, is there a common pattern on how to deal with such a requirement?
I probably could go like so
Private const_abc As Double
Private const_def As Double
Private const_etc As Double
sub initConsts()
const_abc = someFunc(18)
const_def = someFunc( 7)
const_etc = someFunc( 5)
end sub
But then I'd have to make sure that initConsts is called which I'd rather not do.
Edit As per the question of S O, I am using MS-Access.
Create a class that reads the cell and presents a Get-only interface to the value.
Here's a class called ItsMyValueClass
Option Explicit
Private pMyVal As Integer
Public Property Get MyValue() As Integer
MyValue = pMyVal
End Property
Private Sub class_initialize()
'pMyVal = Sheet.Range("somewhere)
pMyVal = 17
End Sub
And here's the code in your module:
Option Explicit
Sub IsItReadOnly()
Dim valu As ItsMyValueClass
Dim x As Integer
Set valu = New ItsMyValueClass
x = valu.MyValue
'valu.MyValue = 23 'compile error "Can't assign to read-only property"
End Sub
Public Function White() as Long
White = RGB(255,255,255)
End function
Private Sub TestIt()
Debug.Print "White is " & White
White = 123 ' <-- compile error
End Sub
in a one-liner that works with modules and classes alike for pure constant-like access:
Public Property Get myConst() As Integer: myConst = 3: End Property
you would use it like this:
Sub test()
Debug.Print "myConst: " & myConst 'would print: "myConst: 3"
End Sub
and if it has to be initialized with a custom value once, one could do it with a static property and one or many private variables:
Private ci As Boolean 'constants initialized
Private myConst1_ As Integer
Private myConst2_ As Integer
Static Property Get myConst1() As Integer
If Not ci Then init
myConst1 = myConst1_
End Property
Static Property Get myConst2() As Integer
If Not ci Then init
myConst2 = myConst2_
End Property
Private Sub init()
'these can come from anywhere:
myConst1_ = 3
myConst2_ = 5
ci = True
End Sub
they are initialized on the first access of the first "constant" property
if you have to initialize them earlier one could just call the init function earlier (and optionally remove the ci variable and all related lines if it is ensured that the properties are not accessed earlier)

VBA Object module must Implement ~?

I have created two classes, one being an interface for the other. Each time I try to instantiate Transition_Model I get:
Compile error: Object Module needs to implement '~' for interface'~'
To my understanding Implementing class is supposed to have a copy of all public subs, function, & properties. So I don't understant what is the problem here?
Have seen similar questions come up but either they refer to actual Sub or they include other complications making answer too complicated for me to understand.
Also note I tried changing Subs of Transition_Model to Private and add 'IModel_' in front of sub names(Just like top answer in second question I linked) but I still receive the same error.
IModel
Option Explicit
Public Enum Model_Types
Transition
Dummy
End Enum
Property Get M_Type() As Model_Types
End Property
Sub Run(Collat As Collateral)
End Sub
Sub Set_Params(key As String, value As Variant)
End Sub
Transition_Model
Option Explicit
Implements IModel
Private Transitions As Collection
Private Loan_States As Integer
Private Sub Class_Initialize()
Set Transitions = New Collection
End Sub
Public Property Get M_Type() As Model_Types
M_Type = Transition
End Property
Public Sub Run(Collat As Collateral)
Dim A_Transition As Transition
Dim New_Balance() As Double
Dim Row As Integer
For Row = 1 To UBound(Collat.Curr_Balance)
For Each A_Transition In Transitions
If A_Transition.Begining = i Then
New_Balance = New_Balance + Collat.Curr_Balance(Row) * A_Transition.Probability
End If
Next A_Transition
Next
End Sub
Public Sub Set_Params(key As String, value As Double)
Dim Split_key(1 To 2) As String
Dim New_Transition As Transition
Split_key = Split(key, "->")
Set New_Transition = New Transition
With New_Transition
.Begining = Split_key(1)
.Ending = Split_key(2)
.Probability = value
End With
Transitions.Add New_Transition, key
End Sub
Lastly the Sub I am using to test my class
Sub Transition_Model()
Dim Tested_Class As New Transition_Model
Dim Collat As New Collateral
'Test is the model type is correct
Debug.Assert Tested_Class.M_Type = Transition
'Test if Model without transition indeed does not affect balances of its collateral
Collat.Curr_Balance(1) = 0.5
Collat.Curr_Balance(2) = 0.5
Tested_Class.Run (Collat)
Debug.Assert ( _
Collat.Curr_Balance(1) = 0.5 And _
Collat.Curr_Balance(2) = 0.5)
End Sub
Actaully Per the second question I linked has the correct answer which I missed.
All subs need to start with 'IModel_' and rest ot the name has to match the name in IModel.
AND
This is the part i missed, you cannot use underscore in the Sub name.

Vb conceptual understanding of creating objects within a class

I was wondering if you could help me understand how to create a Card object (with a value and suit) and use 52 of those cards to make an object called deck.
I have created my card class how do I initialize every card inside the deck class? Should I do it one by one? How do I link all those cards to one deck.
Thanks
As it happen I did read your previous question earlier today.
First, create a suit enum.
Public Enum Suit As Integer
Hearts = 1
Diamonds = 2
Clovers = 3
Spades = 4
End Enum
Then create the card class. Notice that the properties are read only as a card never changes its value. (Maybe not true if you're a magician)
Public Class Card
Public Sub New(suit As Suit, value As Integer)
Me.m_suit = suit
Me.m_value = value
End Sub
Public ReadOnly Property Suit() As Suit
Get
Return Me.m_suit
End Get
End Property
Public ReadOnly Property Value() As Integer
Get
Return Me.m_value
End Get
End Property
Private m_suit As Suit
Private m_value As Integer
End Class
Finally, create the deck class and populate 52 cards.
Public Class Deck
Public Sub New()
Dim cards = New Card(52 - 1) {}
Dim num As Integer = 0
For s As Integer = 1 To 4
For v As Integer = 1 To 13
cards(num) = New Card(CType(s, Suit), v)
num += 1
Next
Next
Me.m_cards = New Collections.ObjectModel.ReadOnlyCollection(Of Card)(cards)
End Sub
Public ReadOnly Property Cards() As Collections.ObjectModel.ReadOnlyCollection(Of Card)
Get
Return Me.m_cards
End Get
End Property
Private ReadOnly m_cards As Collections.ObjectModel.ReadOnlyCollection(Of Card)
End Class
You need two Enumerations and two Classes:
Enumerations
CardFaceValue - with values ranging from Ace-10 (inclusive), Jack, Queen, King.
CardFaceType - with values Hearts, Spades, Clubs, Diamonds
Classes
Deck - Has one property to contain the collection of all cards
Cards - of Type Array of Cards, sized 52.
Card - Has two properties
CardFaceValue
CardFaceType
In the constructor of the Deck class run a loop within a loop. The outer loop will run for 4 times for each of the CardFaceType enumeration, and the inner loop will run for 13 times for cards 1-10, J, Q, K.
With these loops iterate through the enumeration values and add cards to your Deck.
This is just a quick draft of what I envision
You'll need the card class first.
Public Class Card
Private cSuit As String
Private cValue As Integer
Public Property suit() As String
Get
Return cSuit
End Get
Set(ByVal value As String)
cSuit = value
End Set
End Property
Public Property value() As Integer
Get
Return cValue
End Get
Set(ByVal value As Integer)
value = cValue
End Set
End Property
Public Sub New(ByVal TheSuit As String, ByVal TheValue As Integer)
cSuit = TheSuit
cValue = TheValue
End Sub
Then you can make a new object for each card and add it to the deck collection.
Dim Deck As New List(Of Card)
Dim Suit As String = "Spade"
Dim Value As Integer = 11
Dim AceOfSpades As New Card(Suit, Value)
Deck.Add(AceOfSpades)

VB.NET For Each Loop exits after one iteration

I have a For Each loop that is only iterating 1 element. It begins with the 6th element out of 32 in my test, and happens to be one where the comp.includeMe evaluates to True. After the outer if statement is executed, it begins the 2nd iteration but exits the loop and returns immediately after the comp.includeMe evaluated to false. No errors or warnings are present, and I have verified that there are elements in the components object. Can anyone explain what I am doing wrong, and why this syntax doesn't work?
Public Class BOM
Public Property components as New List(Of Component)
Public Function TotalArea(ByVal adjusted As Boolean) As Double
Dim total As Double = 0
For Each comp As Component In components
If comp.includeMe = True Then
If adjusted Then
total += comp.GetAdjustedSize() * comp.quantity
Else
total += comp.area * comp.quantity
End If
End If
Next
Return total
End Function
public sub Add(byval comp as Component)
components.add(comp)
end sub
End Class
Public Class Component
Public Property quantity as Integer
Public Property area as Double
Public Property includeMe as Boolean
...
End Class
' object construction
Dim bomlist as New BOM
bomlist.add(comp)
After digging a little deeper, it seems that the foreach statement is recognizing the first if statement and pulls values only if it is true. I realized I only had only had one component with the includeMe Boolean set to true. After I set other components to true as well, I observed that the For Each iterates exactly the number of times as the number of components with includeMe = True
I'd suggest adding some debug statements to assist with debugging:
Public Class BOM
Public Property components as New List(Of Component)
Public Function TotalArea(ByVal adjusted As Boolean) As Double
Dim total As Double = 0
Debug.Print(components.Count)
For Each comp As Component In components
Debug.Print(comp.includeMe)
If comp.includeMe = True Then
If adjusted Then
total += comp.GetAdjustedSize() * comp.quantity
Else
total += comp.area * comp.quantity
End If
End If
Next
Return total
End Function
public sub Add(byval comp as Component)
components.add(comp)
end sub
End Class

vb Class Random number

I am trying to make a poker game of sorts. I'm trying to learn vb classes. Here is my class code
Friend Class CalcCards
Dim random As New Random()
Private mH11 As Integer = 0
Private mH12 As Integer = 0
Public ReadOnly Property H11() As Integer
Get
H11 = mH11
End Get
End Property
Public ReadOnly Property H12() As Integer
Get
H12 = mH12
End Get
End Property
Public Sub Calc()
Dim count As Integer = 52
Dim intArr(51) As Integer
Dim intshuffle(51) As Integer
For i = 0 To 51
intArr(i) = i
Next
mH11 = intArr(Random.Next(0, 51))
mH12 = intArr(Random.Next(0, 51))
End Sub
This would be the form code.
picH11.Image = ImageList1.Images(calc.H11)
picH12.Image = ImageList1.Images(calc.H12)
My question is why does calc() always return 0 for H11 and H12? Can i not make an instance of random in a class?
You never called the Calc() method.