Simple question I can't answer xD
I bet the answer is very easy :blush:, 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 :D
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
Re: Simple question I can't answer xD
Thank you! Knew the answer was simple :bigyello:
Thanks for the fast reply ;)
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.
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.
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.
Re: Simple question I can't answer xD
LOL well it was just an example :p
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". ;)