Results 1 to 12 of 12

Thread: [RESOLVED] [2005] Simple Function that returns value to textboxes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Resolved [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...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Simple Function that returns value to textboxes

    This:
    vb.net Code:
    1. CDbl(textbox1.Text)
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    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

  6. #6
    Junior Member
    Join Date
    Jun 2007
    Location
    andaman, India
    Posts
    29

    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

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    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

  9. #9
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    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.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    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

  11. #11
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    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

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    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
  •  



Click Here to Expand Forum to Full Width