in a visual basic function how do I square something like
cnsquared
Printable View
in a visual basic function how do I square something like
cnsquared
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
...
Thanks for helping
x = y ^ 2
Or use the Math.Pow function
x = Math.Pow(y, 2)
cool how do I then create a global variable
I want to store the answer in a global variable
How global do you mean? You could add a module to your project, and make a public variable in the module. That variable would be visible to the whole project. However, it is generally good practice to limit visibility to only those parts that actually need to see it, so globals are not used all that much.
Well if I save a sum as answer and I want to display it on the next form what is the best way I can do that
Yea... if you have in your form:
Then "blah" will be seen to everything in Form1, and that's good enough for 99.9% of things.Code:Public Class Form1
Dim blah as Integer
End Class
If you want to send it to another form, then make a Property on that form and send it over, here's Form1:
Here's Form2:Code:Public Class Form1
Dim _blah As Integer
Private Sub MakeForm2()
Dim f As New Form2
f.Blah = _blah
f.Show()
End Sub
End Class
Code:Public Class Form2
Dim _blah As Integer
Public Property Blah() As Integer
Get
Return _blah
End Get
Set(ByVal value As Integer)
_blah = value
End Set
End Property
End Class
A global is the easiest.
The major issue people are dealing with concerning globals is that any part of a program can change a global, so you have the problem that the name of the variable could conflict (if you don't keep track of what names you have for variables), and the state of the global could be changed by other parts of a program. In many programs, especially smaller ones, the first issue is unlikely to be a problem, and the second issue won't be a problem.
However, it's not a good habit to get into. If the sum is calculated on one form, and you want to display it on another form, there are many options you can choose from:
If the second form is not created until after the sum has been calculated, then you can either add a custom constructor to the new form that takes the sum as an argument, or you can put a WriteOnly property on the new form that accepts the sum and shows it, or you can expose the sum via a property on the first form, though this could be much more difficult. In addition to those, you can simply add the sum to a control when you create the second form.
If the second form already exists when the sum is generated, then if the second form is visible to the first form, you can either add a WriteOnly property to the second form, or you can add the sum as a string directly to a control.
Boy was I slow.
Alright cause normally to pass variables to another form I create a variable on the form I want to pass it to and say the variable was username and the user entered their username in a text box on the prvious form i just say
dim username = form1.username_txt
so if the other way is better I wil have to start using it