Question:
Math Problems in Visual Basic?
James
2011-07-27 13:42:51 UTC
I'm trying to do large Formulas in visual basic but seem to have forgotten how to do math since class got out?


Heres the Error:
System.InvalidOperationException was unhandled
Message=An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
Source=CombatCalc
StackTrace:
at CombatCalc.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at CombatCalc.My.MyProject.MyForms.get_Form1()
at CombatCalc.My.MyApplication.OnCreateMainForm() in C:\Users\James\AppData\Local\Temporary Projects\CombatCalc\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at CombatCalc.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=CombatCalc
StackTrace:
at CombatCalc.Form1..ctor() in C:\Users\James\AppData\Local\Temporary Projects\CombatCalc\Form1.vb:line 5
InnerException:

Here's the code:

Public Class Form1
Dim Quarter As Decimal = ".25"
Dim Half As Decimal = ".5"
Dim ThirteenDIVTen As Decimal = "1.3"
Dim HalfPrayer As Decimal = TextBox5.Text * Half
Dim HalfSummoning As Decimal = TextBox6.Text * Half
Dim Attack As Decimal = TextBox1.Text
Dim Strength As Decimal = TextBox2.Text
Dim Defense As Decimal = TextBox3.Text
Dim Constitution As Decimal = TextBox4.Text
Dim AttackPLUSStrength As Decimal = Attack + Strength
Dim AttackStrengthTIMESThirteenDIVTen = AttackPLUSStrength * ThirteenDIVTen
Dim NeedsQuarter As Decimal = AttackStrengthTIMESThirteenDIVTen + Defense + Constitution + HalfPrayer + HalfSummoning
Dim AddQuarter As Decimal = NeedsQuarter * Quarter

Private Sub BtnCalcMelee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalcMelee.Click
Label7.Text = AddQuarter
End Sub
End Class
Three answers:
Daniel B
2011-07-27 14:18:55 UTC
The problem is that you are trying to access you textboxes at the class level, this will not work. For example the line:



Dim HalfPrayer As Decimal = TextBox5.Text * Half



will be executed as soon as the Form1 class has been instantiated and at that point the text boxes don't actually exist yet, that's why you are getting an "Object reference not set to an instance of an object" error.



You probably should do all your math in the BtnCalcMelee_Click function.
?
2016-10-16 04:25:56 UTC
first of all, you would be able to desire to choose an distinctive form of characters. in case you're making a password generator, then set it to to what you desire to have it to be precisely. Secondly, based on your specifications, your minimum is 9 characters now no longer 6. What you would be able to desire to do is create 3 counters one for all the three standards, then while the character suits the standards you upload one to the counter. as quickly as the finished characters were randomly created, you certainly confirm that each and each counter is larger than 2.
AJ
2011-07-27 14:16:44 UTC
your code is all screwed up.



You need to create a class and put all these attributes into that class



As you have it right now, every variable you declared at the form level that looks at a textbox is always going to be null. Also do not use decimal, use double.



Here's a start on the class



Public Class Class1

Private _Prayer As Double

Private _Summoning As Double

Private _Attack As Double

Private _Strength As Double

Private _Defense As Double

Private _Constitution As Double



Public class1()



Public Property Prayer As Double

Get

Return _Prayer

End Get

Set(value As Double)

_Prayer = value

End Set

End Property



Public ReadOnly Property HalfPrayer As Double

Get

Return _Prayer * 0.5

End Get

End Property



Public Property Summoning As Double

Get

Return _Summoning

End Get

Set(value As Double)

value = _Summoning

End Set

End Property



Public ReadOnly Property HalfSummoning As Double

Get

Return _Summoning * 0.5

End Get

End Property



Public Property Attack As Double

Get

Return _Attack

End Get

Set(value As Double)

value = _Attack

End Set

End Property



Public Property Strength As Double

Get

Return _Strength

End Get

Set(value As Double)

value = _Strength

End Set

End Property



Public ReadOnly Property AtkStrength As Double

Get

Return _Attack + _Strength

End Get

End Property



Public ReadOnly Property AtkStrengthplus30 As Double

Get

Return AtkStrength * 1.3

End Get

End Property



Public Property Defense As Double

Get

Return _Defense

End Get

Set(value As Double)

value = _Defense

End Set

End Property



Public Property Constitution As Double

Get

Return _Constitution

End Get

Set(value As Double)

value = _Constitution

End Set

End Property



Public ReadOnly Property NeedsQuarter As Double

Get

Return AtkStrengthplus30 + _Defense + _Constitution + HalfPrayer + HalfSummoning

End Get

End Property

End Class



Then when you click the calc button, you create a new class, assign values from the textboxes to the appropriate field in the class


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...