Confused about ByVal
-
Hi, I'm a little confused about sending a variable into a function ByVal. I've always understood it that if you send it by value the program will make a copy of the variable and anything you do to that variable inside the function will not affect the original one. Thing is my code seems to contradict this. (i hate it when people post loads of code so i've simplified it)
class SomeClass
' a ticket has a property .Message which is a Mymailer.Message
private ticket as MyMailer.Ticketprivate function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
message.Subject = "Test subject"
message.Body = "This is a test message"
return message
end functionprivate sub SomeSubroutine()
dim newMessage as MyMailer.Message = DoMailMerge(ticket.Message)
end subend class
for some reason when i call SomeSubroutine() the message contained in the variable 'ticket' is changed, even though i passed it by value. can anyone see why the variable ticket would be affected? it works if the DoSomething function is:
private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
dim subject as string = "TestSubject"
dim body as string = "This is a test message"dim newmessage as new MyMailer.Message
newmessage.Subject = subject
newmessage.Body = body
return newmessage
end functionkevin
-
Hi, I'm a little confused about sending a variable into a function ByVal. I've always understood it that if you send it by value the program will make a copy of the variable and anything you do to that variable inside the function will not affect the original one. Thing is my code seems to contradict this. (i hate it when people post loads of code so i've simplified it)
class SomeClass
' a ticket has a property .Message which is a Mymailer.Message
private ticket as MyMailer.Ticketprivate function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
message.Subject = "Test subject"
message.Body = "This is a test message"
return message
end functionprivate sub SomeSubroutine()
dim newMessage as MyMailer.Message = DoMailMerge(ticket.Message)
end subend class
for some reason when i call SomeSubroutine() the message contained in the variable 'ticket' is changed, even though i passed it by value. can anyone see why the variable ticket would be affected? it works if the DoSomething function is:
private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
dim subject as string = "TestSubject"
dim body as string = "This is a test message"dim newmessage as new MyMailer.Message
newmessage.Subject = subject
newmessage.Body = body
return newmessage
end functionkevin
If your object is a reference type ( a class ) then ByVal means nothing, it's always ByRef
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi, I'm a little confused about sending a variable into a function ByVal. I've always understood it that if you send it by value the program will make a copy of the variable and anything you do to that variable inside the function will not affect the original one. Thing is my code seems to contradict this. (i hate it when people post loads of code so i've simplified it)
class SomeClass
' a ticket has a property .Message which is a Mymailer.Message
private ticket as MyMailer.Ticketprivate function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
message.Subject = "Test subject"
message.Body = "This is a test message"
return message
end functionprivate sub SomeSubroutine()
dim newMessage as MyMailer.Message = DoMailMerge(ticket.Message)
end subend class
for some reason when i call SomeSubroutine() the message contained in the variable 'ticket' is changed, even though i passed it by value. can anyone see why the variable ticket would be affected? it works if the DoSomething function is:
private function DoSomething(ByVal message as MyMailer.Message) as Mymailer.Message
dim subject as string = "TestSubject"
dim body as string = "This is a test message"dim newmessage as new MyMailer.Message
newmessage.Subject = subject
newmessage.Body = body
return newmessage
end functionkevin
Objects and arrays are reference types. Basically an object is a pointer to a structure and the name of the object is essentially a pointer/reference. So when you pass them byval the compiler makes a copy of the pointer/reference but both copies point to the same "structure". That's why any change made by one will affect the other.
-
Objects and arrays are reference types. Basically an object is a pointer to a structure and the name of the object is essentially a pointer/reference. So when you pass them byval the compiler makes a copy of the pointer/reference but both copies point to the same "structure". That's why any change made by one will affect the other.
Thanks Christian and Mike. Now I understand!