|
-
Jun 11th, 2007, 12:24 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] Simple Function that returns value to textboxes
Hi,
My function Def is as follows:
Code:
Private Sub Extract(ByVal n As Integer, ByVal m As Integer, ByRef a As Double, ByRef b As Double, ByRef c As Double)
a= 100
b= 200
c= 300
End Sub
I have used the following decl and it is returns null values in Textbox1,2 & 3, where as I expected 100,200 & 300 respectively..
Code:
Extract(CInt(Val(textbox5.Text)), CInt(Val(textbox6.Text)), CDbl(textbox1.Text), CDbl(textbox2.Text), CDbl(textbox3.Text))
Please help...
-
Jun 11th, 2007, 12:31 AM
#2
Re: [2005] Simple Function that returns value to textboxes
This:creates a variable of type Double, parses the Text of the TextBox to a Double and assigns it to that variable. It is that Double variable whose value you are setting within the method. The Text property of the TextBox, which is type String, is not set anywhere and is thus unaffected. The Double variable you created is not maintained so it is immediately lost. The sum total is that nothing useful is done.
-
Jun 11th, 2007, 12:37 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Simple Function that returns value to textboxes
Hello Jmc,
Not sure if I understood you fully.
Will be thankful if you can post a small line of code..
thanx
-
Jun 11th, 2007, 01:58 AM
#4
Re: [2005] Simple Function that returns value to textboxes
If you want to change the Text property of a TextBox inside a method then you have to pass the Text property of the TextBox to that method. You're not. You're passing the Text property of your TextBox to the CDbl function. CDbl returns a result and it's that result that you're passing to your method. That is simply not going to do what you want.
-
Jun 11th, 2007, 02:04 AM
#5
Fanatic Member
Re: [2005] Simple Function that returns value to textboxes
You can pass the textbox object to your function and then change the value of the text
-
Jun 11th, 2007, 02:50 AM
#6
Junior Member
Re: [2005] Simple Function that returns value to textboxes
your code need to be rewritten completely
if you want to change values of the text boxes, then you need assign it in your subject.
you need to create an array of textboxes and pass the index of the array to the function and in that array you have to assign the value of textbox
-
Jun 11th, 2007, 02:51 AM
#7
Re: [2005] Simple Function that returns value to textboxes
Functions shouldn't do things to textboxes, they should return meaningful values which can then be used however you please.
-
Jun 11th, 2007, 03:12 AM
#8
Thread Starter
Hyperactive Member
Re: [2005] Simple Function that returns value to textboxes
Thank you both for inputs.
I have option strict on and hence I am getting the following error.
Will be thankful if you can have a look..
thanks
Option Strict On disallows narrowing from type 'Object' to type 'String' in copying the value of 'ByRef' parameter 'output1' back to the matching argument.
my Code...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Test(TextBox1.Text, 1.5, TextBox4.Text, TextBox7.Text)
Test(TextBox2.Text, 2.5, TextBox5.Text, TextBox8.Text)
Test(TextBox3.Text, 3.5, TextBox6.Text, TextBox9.Text)
MsgBox("Completed")
End Sub
Private Function Test(ByVal input As Object, ByVal factor As Double, ByRef output1 As Object, ByRef output2 As Object) As Boolean
output1 = input.ToString
output2 = CDbl(input.ToString) * factor
'Test = output2
End Function
-
Jun 11th, 2007, 03:18 AM
#9
Fanatic Member
Re: [2005] Simple Function that returns value to textboxes
the first paramater in your function is an object and you are passing a string. This won't be allowed with option strict on. The type must match for your program to work.
-
Jun 11th, 2007, 03:19 AM
#10
Thread Starter
Hyperactive Member
Re: [2005] Simple Function that returns value to textboxes
Without Option Strict On, the result textboxes are getting populated correctly.
There are errors only when Option Strict On is enabled...
thanks
-
Jun 11th, 2007, 03:26 AM
#11
Fanatic Member
Re: [2005] Simple Function that returns value to textboxes
every type derive from object, that means if option strict is off, the compiler will automatically understand that by inheritance a string is an object and execute your function. On the other hand, with option strict on, the iheritance is not allowed, the paramater type must match with the type of the value that you are passing to the function.
in your function, some parameters are of type object and you are passing strings which is not correct with option strict on
-
Jun 11th, 2007, 03:42 AM
#12
Thread Starter
Hyperactive Member
Re: [2005] Simple Function that returns value to textboxes
Without realizing, I was making an obvious mistake; thanks a lot for your feedback...
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
|