Results 1 to 7 of 7

Thread: Simple question I can't answer xD

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Question Simple question I can't answer xD

    I bet the answer is very easy , maybe just one extra word, but:
    How can you make a function/sub change the inputvalue?

    As example:
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim testval As Integer = 1
            MsgBox("Before: " & testval)
            Testfunc(testval)
            MsgBox("After: " & testval)
        End Sub
    
        Public Overridable Function Testfunc(ByVal Testval As Integer)
            Testval = 2
        End Function
    The after value should be "2", but at the moment it stays "1".

    Hoping for a quick reply

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,830

    Re: Simple question I can't answer xD

    ByVal means to pass a copy of the VALUE of the parameter. This means any changes to the variable is done on the copy, preserving the original.
    ByRef means to pass a copy of the ADDRESS (or pointer) to the parameter. Any changes to the variable is then done to the value at that address.

    So, in short change
    Code:
    Testfunc(ByVal Testval As Integer)
    to
    Code:
    Testfunc(ByRef Testval As Integer)
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Simple question I can't answer xD

    Thank you! Knew the answer was simple

    Thanks for the fast reply

  4. #4
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Simple question I can't answer xD

    Try changing your code to what's below. You should get the result you're looking for. You weren't setting Testfunc equal to anything in the Function Testfunc and you had Testfunc(testval) in the Button1_Click sub but you weren't setting testval equal to it.

    Code:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim testval As Integer = 1
            MsgBox("Before: " & testval)
            testval =   Testfunc(testval)
            MsgBox("After: " & testval)
        End Sub
    
        Public Overridable Function Testfunc(ByVal Testval As Integer) As Integer
            Testfunc = 2
        End Function
    Or you can have Testfunc(ByRef Testval As Integer) As Integer. You need the function to be decalared as something. If Option Strict is Off you get a warning and if Option Strict is On then it isn't allowed if the function isn't declared as an integer or something.
    Last edited by EntityX; Apr 25th, 2010 at 10:54 AM.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: Simple question I can't answer xD

    However, you generally have to be returning a value to be using a Function. Since you have no return value, you should be using a Sub. Changing the arguments of a method, while possible, as you can see, is what is known as a side effect. There are situations where a side effect is the only way for the code to work, but they often create trouble, as people don't usually expect a method to alter its parameters.
    My usual boring signature: Nothing

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: Simple question I can't answer xD

    Well, as I was typing that, EntityX snuck in with an example of what I was saying.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Simple question I can't answer xD

    LOL well it was just an example

    This is what I really tried to do:
    Code:
        Public Function RenderPictureNode(ByRef pnode As PictureNode) As Image
            If pnode.ColorChanged = True Then
                pnode.ColoredImage = ColorImage(pnode.TextureImage, pnode.Color.A / 255, pnode.Color.R / 255, pnode.Color.G / 255, pnode.Color.B / 255)
            End If
            If pnode.SizeChanged = True Or pnode.ColorChanged = True Then
                pnode.SizedImage = ResizeImage(pnode.ColoredImage, pnode.Size.Width, pnode.Size.Height, True)
            End If
            If pnode.PosRotChanged = True Or pnode.SizeChanged = True Or pnode.ColorChanged = True Then
                pnode.FinalImage = New Bitmap(800, 600)
                Dim g As Graphics = Graphics.FromImage(pnode.FinalImage)
                g.TranslateTransform(pnode.DRotationMid.X + 400, pnode.DRotationMid.Y + 300)
                g.RotateTransform(pnode.DRotation)
                g.TranslateTransform((pnode.Position.X + pnode.DOffsetX) - pnode.DRotationMid.X - 400 + pnode.SizedImage.Width * 0.5, (pnode.Position.Y + pnode.DOffsetY) - pnode.DRotationMid.Y - 300 + pnode.SizedImage.Height * 0.5)
                g.RotateTransform(pnode.StaticRotation)
                g.TranslateTransform(pnode.SizedImage.Width * -0.5, pnode.SizedImage.Height * -0.5)
                g.DrawImage(pnode.SizedImage, New Point(0, 0))
                g.Dispose()
            End If
            pnode.SizeChanged = False
            pnode.PosRotChanged = False
            pnode.ColorChanged = False
            Return pnode.FinalImage
        End Function
    BTW it's for a image-edit like program that supports "nodes".

Tags for this Thread

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