|
-
Apr 22nd, 2008, 02:48 PM
#1
Thread Starter
Hyperactive Member
squred
in a visual basic function how do I square something like
cnsquared
-
Apr 22nd, 2008, 02:52 PM
#2
Re: squred
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
...
-
Apr 22nd, 2008, 02:53 PM
#3
Thread Starter
Hyperactive Member
-
Apr 22nd, 2008, 02:55 PM
#4
Re: squred
x = y ^ 2
Or use the Math.Pow function
x = Math.Pow(y, 2)
-
Apr 22nd, 2008, 03:00 PM
#5
Thread Starter
Hyperactive Member
Re: squred
cool how do I then create a global variable
I want to store the answer in a global variable
-
Apr 22nd, 2008, 03:14 PM
#6
Re: squred
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.
My usual boring signature: Nothing
 
-
Apr 22nd, 2008, 03:15 PM
#7
Thread Starter
Hyperactive Member
Re: squred
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
-
Apr 22nd, 2008, 03:18 PM
#8
Re: squred
Yea... if you have in your form:
Code:
Public Class Form1
Dim blah as Integer
End Class
Then "blah" will be seen to everything in Form1, and that's good enough for 99.9% of things.
-
Apr 22nd, 2008, 03:21 PM
#9
Re: squred
If you want to send it to another form, then make a Property on that form and send it over, here's Form1:
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
Here's Form2:
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
-
Apr 22nd, 2008, 03:22 PM
#10
Re: squred
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.
My usual boring signature: Nothing
 
-
Apr 22nd, 2008, 03:22 PM
#11
My usual boring signature: Nothing
 
-
Apr 23rd, 2008, 02:32 AM
#12
Thread Starter
Hyperactive Member
Re: squred
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|