Reputation: 321
I've been tasked with converting an Excel VBA script into a VB.net standalone Windows application. I've never used VB.net before and find myself struggling with fixing this particular crash. I apologize in advance if my code is a disaster as I'm learning how VB.net works as I go.
MainForm.vb
Public Partial Class MainForm
Dim Sessions As Object
Dim System As Object
Dim Sess0 As Object
Dim oldSystemTimeout&
Dim g_HostSettleTime As Double
Public Sub New()
Me.InitializeComponent()
End Sub
Public Sub costs_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles costs_box.TextChanged, other_box.TextChanged, poc_box.TextChanged
Dim total As Double
Dim costs As Double
Dim other As Double
Dim poc As Double
costs = Val(costs_box.Text)
other = Val(other_box.Text)
poc = Val(poc_box.Text)
total = costs + other + poc
totalCosts_box.Text = total.ToString("C2")
End Sub
Public Sub seller_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sellerLump_box.TextChanged, sellerSpecific_box.TextChanged
Dim total As Double
Dim lump As Double
Dim specific As Double
lump = Val(sellerLump_box.Text)
specific = Val(sellerSpecific_box.Text)
total = lump + specific
totalSeller_box.Text = total.ToString("C2")
End Sub
Public Sub credits_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sellerLump_box.TextChanged, sellerSpecific_box.TextChanged, dbCredit_box.TextChanged, lenderCredit_box.TextChanged
Dim total As Double
Dim lump As Double
Dim specific As Double
Dim db As Double
Dim lender As Double
lump = Val(sellerLump_box.Text)
specific = Val(sellerSpecific_box.Text)
db = Val(dbCredit_box.Text)
lender = Val(lenderCredit_box.Text)
total = lump + specific + db + lender
totalCredits_box.Text = total.ToString("C2")
End Sub
Public Sub Clear_buttonClick(sender As Object, e As EventArgs)
loanNumber_box.Clear
costs_box.Clear
other_box.Clear
poc_box.Clear
sellerLump_box.Clear
sellerSpecific_box.Clear
dbCredit_box.Clear
lenderCredit_box.Clear
loanType_box.Text = ""
loanFamily_box.Text = ""
propertyType_box.Text = ""
salesPrice_box.Text = ""
appValue_box.Text = ""
cltv_box.Text = ""
contribLimit_box.Text = ""
decisionUpper_box.Text = ""
decisionLower_box.Text = ""
salesPrice_box.ForeColor = SystemColors.ControlText
appValue_box.ForeColor = SystemColors.ControlText
decisionUpper_box.ForeColor = SystemColors.ControlText
decisionLower_box.ForeColor = SystemColors.ControlText
End Sub
Public Sub Run_buttonClick(sender As Object, e As EventArgs)
LPS()
End Sub
Public Sub LPS()
'--------------------------------------------------------------------------------
' Get the main system object
System = CreateObject("EXTRA.System") ' Gets the system object
If (System Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
Sessions = System.Sessions
If (Sessions Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
'--------------------------------------------------------------------------------
' Set the default wait timeout value
g_HostSettleTime = 50 ' Milliseconds Change As Needed. If retailChoice = 0 Then g_HostSettleTime = 150
oldSystemTimeout& = System.TimeoutValue
If (g_HostSettleTime > oldSystemTimeout) Then
System.TimeoutValue = g_HostSettleTime
End If
' Get the necessary Session Object
Sess0 = System.ActiveSession
If (Sess0 Is Nothing) Then
MsgBox("Please open an NTS session")
Exit Sub
End If
If Not Sess0.Visible Then Sess0.Visible = True
Sess0.screen.WaitHostQuiet (g_HostSettleTime)
' Do not edit code above this line.
' Place your code here:
Dim loanNumber As String
Dim loanType As String
Dim loanFamily As String
Dim salesPrice As Double
Dim appraisedValue As Double
Dim propertyType As String
Dim cltv As String
Dim adjustedCLTV As Double
Dim limit As Double
Dim familyCounter As Integer
Dim propertyCounter As Integer
Dim priceCounter As Integer
Dim contribLimit As Double
Dim totalCredits As Double
Dim totalCosts As Double
loanNumber = loanNumber_box.Text
If loanNumber = "" Then
MsgBox("Please enter valid loan number", vbOKOnly)
loanNumber = ""
Exit Sub
Else
'LPS
If Mid(loanNumber, 1, 2) = "02" Or Mid(loanNumber, 1, 2) = "04" Then
loanType_box.Text = ""
loanFamily_box.Text = ""
propertyType_box.Text = ""
salesPrice_box.Text = ""
appValue_box.Text = ""
cltv_box.Text = ""
contribLimit_box.Text = ""
salesPrice_box.BackColor = SystemColors.Control
appValue_box.BackColor = SystemColors.Control
Sess0.screen.SendKeys ("<Reset><Clear>sign<Enter>61<Enter>")
Sess0.screen.WaitHostQuiet (g_HostSettleTime)
Sess0.screen.PutString ("POINTS", 1, 3)
Sess0.screen.PutString (loanNumber, 1, 12)
Sess0.screen.SendKeys ("<ENTER>")
Sess0.screen.WaitHostQuiet (g_HostSettleTime)
loanType = Sess0.screen.getstring(6, 48, 1)
loanFamily = Sess0.screen.getstring(9, 23, 1)
salesPrice = Val(Sess0.screen.getstring(8, 11, 10))
appraisedValue = Val(Sess0.screen.getstring(8, 29, 10))
propertyType = Sess0.screen.getstring(7, 6, 4)
cltv = Sess0.screen.getstring(9, 73, 6)
adjustedCLTV = cltv / 100
totalCredits = totalCredits_box.Text
totalCosts = totalCosts_box.Text
If loanType = "C" Then
loanType_box.Text = "Conventional"
If loanFamily = "C" Then
loanFamily_box.Text = "Conforming"
familyCounter = 1
ElseIf loanFamily = "N" Then
loanFamily_box.Text = "Non-conforming"
familyCounter = 2
End If
If propertyType = "PRIM" Then
propertyType_box.Text = "Primary"
propertyCounter = 1
ElseIf propertyType = "SECN" Then
propertyType_box.Text = "Secondary"
propertyCounter = 2
ElseIf propertyType = "INVR" Then
propertyType_box.Text = "Investment"
propertyCounter = 3
End If
salesPrice_box.Text = salesPrice.ToString("C2")
appValue_box.Text = appraisedValue.ToString("C2")
If salesPrice < appraisedValue Then
priceCounter = 1
salesPrice_box.ForeColor = Color.ForestGreen
ElseIf salesPrice > appraisedValue Then
priceCounter = 2
appValue_box.ForeColor = Color.ForestGreen
ElseIf salesPrice = appraisedValue Then
priceCounter = 1
salesPrice_box.ForeColor = Color.ForestGreen
appValue_box.ForeColor = Color.ForestGreen
End If
cltv_box.Text = adjustedCLTV.ToString("P2")
ElseIf loanType <> "C" Then
MsgBox("Loan " & loanNumber & " is not a conventional loan", vbOKOnly)
Exit Sub
End If
If familyCounter = 1 Then
If propertyCounter = 1 Or propertyCounter = 2 Then
If cltv > 90# Then
limit = 0.03
ElseIf cltv > 75# And cltv <= 90# Then
limit = 0.06
ElseIf cltv <= 75# Then
limit = 0.09
End If
ElseIf propertyCounter = 3 Then
limit = 0.02
End If
ElseIf familyCounter = 2 Then
If propertyCounter = 1 Or propertyCounter = 2 Then
If cltv > 80# Then
limit = 0.03
ElseIf cltv <= 80# Then
limit = 0.06
End If
ElseIf propertyCounter = 3 Then
limit = 0
End If
End If
If priceCounter = 1 Then
contribLimit = salesPrice * limit
ElseIf priceCounter = 2 Then
contribLimit = appraisedValue * limit
End If
contribLimit_box.Text = contribLimit.ToString("C2")
If totalCredits > contribLimit And Not totalCredits > totalCosts Then
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Regular)
decisionUpper_box.Text = "Total credits exceed contribution limit"
ElseIf totalCredits > totalCosts And Not totalCredits > contribLimit Then
decisionUpper_box.Text = "TOTAL CREDITS EXCEED TOTAL COSTS"
decisionUpper_box.ForeColor = Color.Red
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Bold)
ElseIf totalCredits > totalCosts And totalCredits > contribLimit Then
decisionUpper_box.ForeColor = Color.Red
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Bold)
decisionUpper_box.Text = "TOTAL CREDITS EXCEED TOTAL COSTS"
decisionLower_box.Text = "Total credits exceed contribution limit"
decisionLower_box.Font = New Font(decisionUpper_box.Font, FontStyle.Regular)
ElseIf totalCredits < contribLimit Or totalCredits = contribLimit Then
decisionUpper_box.Text = "PASS"
decisionUpper_box.ForeColor = Color.Green
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Bold)
End If
End If
End If
' Do not edit code below this line.
System.TimeoutValue = oldSystemTimeout
Sessions = Nothing
System = Nothing
Sess0 = Nothing
End Sub
End Class
Program.vb
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
' This file controls the behaviour of the application.
Partial Class MyApplication
Public Sub New()
MyBase.New(AuthenticationMode.Windows)
Me.IsSingleInstance = False
Me.EnableVisualStyles = True
Me.SaveMySettingsOnExit = False
Me.ShutDownStyle = ShutdownMode.AfterMainFormCloses
End Sub
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = My.Forms.MainForm
End Sub
End Class
End Namespace
What I'm trying to do is create variables at a class level that are available for all my functions/subs. When I compile the code below and run it, I get a "System.NullReferenceException: Object variable or With block variable not set." error. The debugger highlights the "Me.MainForm = My.Forms.MainForm" in Program.vb as the source of the error but I'm lost as to how to fix it. Can anyone give me a few hints in the right direction? Thanks!
Upvotes: 1
Views: 6747
Reputation: 38895
I am not sure if this is all that is wrong, but:
Dim System As Object
This will cause all sorts of problems. .NET already has a System Namespace and it is a rather important one, as you can see just a few lines later:
Public Sub costs_TextChanged(ByVal sender As System.Object, ...
Rename it:
Dim XSys as Object
way down below:
' Get the main system object
XSys = CreateObject("EXTRA.System") ' Gets the system object
If (XSys Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
Sessions = XSys.Sessions
If (Sessions Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
' at the very end:
XSys.TimeoutValue = oldSystemTimeout
Sessions = Nothing
XSys = Nothing
It still might not work, but for certain, it CANT work the way it was
Upvotes: 1