I'm working within Visual Basic console application.
I have created a number of mathematical features such as quadratic equation and prime factorisation.
However, my next task is to create a mathematical feature that you're able to enter and select the number of decimal places you wish to get your answers for the other mathematical features.
For example let's call this function decimalPoint:
You enter decimal point - you're asked how many decimal places you'd like your answers to. You chose 2.
You go back to the main menu and select quadratic equation, after inputting the coefficients then the output of the quadratic equation has to output in 2.d.p.
I'm able to ask the user for an input and just convert that into whatever d.p I need, but I don't know how to link it to other subs/functions.
So Far I have something along the lines:
Sub decimalPoint
dim Input As Integer
Console.WriteLine("Welcome")
Console.WriteLine("Enter amount of decimal place you wish to save to 1-5", 1-6) 'max 5dp.
input = Console.ReadLine()
End Sub
''Now I could do something like Console.WriteLine(Round(Convert.ToDecimal(input), 2)), but that's only based on whatever they input and not related to other functions/subs that I need it for. Any idea how I can link this to other subs?
notes:
It's a program with a menu.
Update: I got something along these lines now, but it's still not working.
Module Module1
Public Property MyResult As ...
Public Property MyDecimalInput As ...
Sub QuadraticFunction()
....Calculations
Console.WriteLine("Chose a decimal input: 1-5")
If ... = ... Then
Call decimalPoint()
MyResult = (....)
End Sub
Sub decimalPoint()
If MyDecimalInput = ...
Math.Round(MyResult) ...
...
End Sub
End Module
I don't know if this is what you're looking for but this is how I interpreted your question, but does this answer your question? If not let me know and I'll do my best to help :)
'converts text in textbox to integer
Dim decimalPoint As Integer = Convert.ToInt64(addTB.Text)
'stores newly rounded function into a number of user specified decimal places
Dim answer As Double = Math.Round(13.17435, decimalPoint)
'displays rounded answer
MsgBox(answer)
Related
Requesting your help to understand the concept of widening better!
I came across the following statement w.r.t 'Widening Conversion' in VB.Net. From the msdn documentation on the topic I found the following: Widening conversions preserve the source value but can change its representation. This occurs if you convert from an integral type to Decimal, or from Char to String. The link to the page is found below:
https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/widening-and-narrowing-conversions
My Question is as follows: I wrote the following code to understand the meaning of the statement "...preserve the source value but can change its representation". But I see no difference in the output when I print the integer or the decimal. Then what does the phrase mean....what is the meaning of "...can change its representation"?
Module Module1
Sub Main()
Dim i As Integer = 5
Dim d As Decimal = i 'widening
Console.WriteLine(i)
Console.WriteLine(d)
'Both prints "5"...no difference in representation
Console.ReadLine()
End Sub
End Module
Can someone also please give an example to demonstrate how the representation changes when we convert a char value to a string?
It means the internal presentation of the number (in your case). Try to convert, say, Single to Double:
Sub Main(args As String())
Dim sng As Single = 1.23E+32
Dim dbl As Double = sng
Console.WriteLine($"Single: {sng}")
Console.WriteLine($"Double: {dbl}")
End Sub
' Output:
' Single: 1,23E+32
' Double: 1,2300000079302825E+32
I am trying to take the user input and assign it to a property defined in a class. When I run the program, it asks for user input as expected, but displays a different result. Can someone point out where my mistake is ?
I was trying to base my simple program on this tutorial
https://learn.microsoft.com/en-us/dotnet/core/tutorials/vb-with-visual-studio
but trying to extend it to classes.
I am using the latest version of Visual Studio and Visual Basic. It's a visual basic Console App
Module Module1
Sub Main()
Dim ClassInstance As New Class1()
Console.WriteLine("Input Property 1: ")
ClassInstance.Property1 = Console.Read()
Console.Write(ClassInstance.Property1)
Console.ReadKey(True)
End Sub
Public Class Class1
Public Property1 As Integer
Public Property2 As Integer
End Class
End Module
Expected output:
"Input Property 1:" |
User input 50 |
Output 50
Console.Read reads the next character from the input, and gives you that character's code. If, for instance, you typed 5 at the prompt1, Console.Read would return 53. Why? Because that's the ASCII/Unicode code for that character (in Unicode terms, it's U+0035, which is the same number represented in hexadecimal).
If you want to read multiple characters and interpret them as an integer, you should a) be using something other than Console.Read to take the input and b) use Int32.TryParse to try to turn it into a number (because users don't always give us the input we expect).
Something like:
Module Module1
Sub Main()
Dim ClassInstance As New Class1()
Console.WriteLine("Input Property 1: ")
Dim inp = Console.ReadLine()
Dim value as Int32
If Int32.TryParse(inp, value) Then
ClassInstance.Property1 = value
Console.Write(ClassInstance.Property1)
Console.ReadKey(True)
End If
End Sub
Public Class Class1
Public Property1 As Integer
Public Property2 As Integer
End Class
End Module
(With apologies if I've made syntax errors - my VB's quite rusty)
In reality, you'd probably want to write some form of loop that prompts for user input and doesn't terminate until it successfully parses. I think Do/While would fit there - but if you're going to prompt the user more than once, you probably would want to extract the "Loop until valid input received" code into a function that takes the prompt as a parameter.
More reading - ASCII/Unicode. For characters in the "7-bit ASCII" range, basic latin characters without accents, it doesn't make much difference which references you check
1And it doesn't matter if you carried on and typed any more characters, your program only asks for/gets one of them
Private Sub TextBox1_Change()
Slide2.TextBox2.Value = Slide2.TextBox1.Value * 0.0225
Slide2.TextBox3.Value = Slide2.TextBox1.Value * 0.1
End Sub
Everytime i run the presentation i get an object required error on line 2. I am fairly new to VBA so have no idea how to deal with it
Maybe, coding like this
Val(Slide2.TextBox1.Value)*0.1
There are any number of problems with your code as written. This will work, assuming, as Tim asks, that there IS a shape named TextBox1 on Slide 2:
Private Sub TextBox1_Change()
With ActivePresentation.Slides(2).Shapes("TextBox1")
.OLEFormat.Object.Text = CStr(CDbl(.OLEFormat.Object.Text) * 0.0225)
End With
End Sub
A bit of explanation:
ActivePresentation.Slides(2).Shapes("TextBox1")
There might be more than one presentation open, so you have to tell VBA which one to look at, in this case the Active presentation, the one you're looking at right now.
.Slides(2)
This is the correct syntax to refer to the second slide in the presentation, and from there you need to specify which Shape on the slide you want to work with, so
.Shapes("TextBox1")
Since it's an ActiveX shape and not a normal PPT shape, you have to access its properties in a rather odd way:
.OLEFormat.Object.PropertyName
In this case, the property name in question is Text ... the text in the textbox.
Then we convert the text in the text box to a number (using CDbl) before doing arithmetic on it, and then convert the result back to a string to stuff into the textbox.
VBA will try to do these string to number, number to string conversions for you automatically, but it's generally better practice to be explicit about what you want rather than going with what VBA ASS-U-MEs you want.
This code doesn't seem to be working. I don't really know what loop to use to get it too add the information the user puts into the machine to print again.
The aim of this is for the user to either pick:
to print a menu that they have typed in an earlier database. If they haven't typed anything into the database, then it should be blank
Should let the user enter information into the database (What I am mostly struggling with) and do error checking so that it tells them if they have entered a number when they should have entered a letter and
To end the program.
When I do (2) It lets me type but it doesn't recall the information back in the database. Also it needs a number (4) which should return to the main menu. I think this is where the loops come in but I don't know which one to use.
Here is the code:
Structure Cars
Public carmake As String
Public carmodel As String
Public caryear As Integer
Public carlicence As String
End Structure
Module Module1
Sub Main()
Dim userchoice
Console.WriteLine("Choose weather to open the database(1), print it (2) or end (3)")
userchoice = Console.ReadLine()
If userchoice = 1 Then
Dim cardatabase(4) As Cars
Console.WriteLine("This will allow you to view the database.")
Console.WriteLine("Please enter the car make,licence,model and year")
cardatabase(1).carmake = Console.ReadLine()
cardatabase(1).carlicence = Console.ReadLine()
cardatabase(1).carmodel = Console.ReadLine()
cardatabase(1).caryear = Console.ReadLine()
ElseIf userchoice = 2 Then
Console.WriteLine("The database is,")
ElseIf userchoice = 3 Then
Console.WriteLine("Thanks for using this program.")
End If
Console.ReadLine()
End Sub
End Module
You have several issue with this code. Here's what I would recommend:
You need some sort of looping structure such as a While loop. Your test condition could be the userchoice variable.
In your If statements, you need to check if userchoice is equal to a string value instead of an integer value. So the line If userchoice = 1 Then should actually be If userchoice = "1" Then.
The cardatabase array should be declared outside of the loop. When you declare it in the loop, it will keep re-creating the array instead of adding more items to it.
Your Cars structure needs to be inside the Module Module1 block.
Since you don't know how many times the user may want to add a new car before they quit, I'd recommend using a List instead of an Array. Lists allow for easy dynamic re-sizing.
You need an integer variable to track the number of cars entered and use this as an index for your cardatabase collection.
Array/List indexes start with 0.
Cars should probably be a class instead of a structure. Also, it should be named Car. It's a single structure (or class, if you change it). The array itself should be called cars as it is a collection of a multiple Car structures (or objects if you change it to a class).
I was going to write example code here to demonstrate my points but it would be nearly an entire re-write and this would not help you understand why I made the changes I did.
My best advice to you is to go back through your book or tutorials that you read previously to get to this point and really try to understand what they are saying. If you don't get the concept, look it up elsewhere until you do.
Disclaimer: My recommendations are not comprehensive. I stopped examining your code when I identified all of the above issues right off the bat.
Im having trouble working out making a converter for mutliple currencys using multiple subs. I keep receiving an error saying that number is a friend , and therefore cannot be used in the jap conversion . can anyone help ? thank you in advance
Option Explicit On
'Option Strict On
Imports System
Module Yahtzed
Sub CANtoUSD()
Dim Number , USDConversion as Decimal
Number = Console.Readline
USDConversion =( Number * 1.0141)
Console.Writeline(USDConversion)
End Sub
Sub CANtoJAP()
Dim Number, JAPConversion as Decimal
Number = Console.Readline
JAPConversion =( Number * 79.9392)
Console.Writeline(JAPConversion)
End Sub
Sub Main()
Console.Writeline("Enter the CAN amount: ")
CANtoUSD()
CANtoJAP()
End Sub
End Module
Not a direct answer, but this requires more space than would work in a comment.
You have a fundamental design error in your code. You really want to structure it more like this:
Function CANtoUSD(Number As Decimal) As Decimal
Dim USDConversion as Decimal = 1.0141
Return USDConversion * Number
End Function
Function CANtoJAP(Number As Decimal) As Decimal
Dim JAPConversion as Decimal = 79.9392
Return JAPConversion * Number
End Function
Sub Main()
Console.Writeline("Enter the CAN amount: ")
Dim input As Decimal = Console.ReadLine()
Console.WriteLine(CANtoUSD(input))
Console.WriteLine(CANtoJAP(input))
End Sub
You don't want to mix responsibilities for you methods. The input/output should be strictly separated from the code that manipulates the data. If nothing else, this makes it easier to test that your specific conversion methods work exactly like they are supposed to, and could not be the source of your bug.
Later on, you'll learn how to also ahave a single method that accepts a key value for both source and destination types, and does a table lookup to convert any currency to any other by knowing the conversion factor to a common currency.