The Student Room Group

GCSE Visual Basic help

Hey everyone,
I haven't used VB for quite a long time now and I need some help. I'm making console applications at school and one thing I don't get is how I can "share" variables between two procedures, if you know what I mean. I'm guessing I would need to use ByVal or ByRef but I can't really remember what they mean and what the difference is between the two. So could someone please explain to me:

(1) How to pass a variable between two sub procedures.

(2) The difference between ByVal and ByRef.

Thanks in advance.
Reply 1
When you have a sub, the byVal and byRef are used for inputted variables when it's called:

Sub Example (ByVal int as Integer, ByRef str as String)

To call it you would use:

Example(integer1, string1)

The when you want to refer to either variable in the Example sub you just have to use int or str. The difference between ByVal and ByRef is that with ByVal you can edit the value of it in the Example sub and it won't be affected in the Main sub at all. With ByRef, when you edit it in Example it will return whatever value it ends up with after running through Example.

Have a look at these if you are still confused:
http://homeandlearn.co.uk/NET/nets9p4.html
http://www.cpearson.com/excel/byrefbyval.aspx
Reply 2
Original post by D-Box
When you have a sub, the byVal and byRef are used for inputted variables when it's called:

Sub Example (ByVal int as Integer, ByRef str as String)

To call it you would use:

Example(integer1, string1)

The when you want to refer to either variable in the Example sub you just have to use int or str. The difference between ByVal and ByRef is that with ByVal you can edit the value of it in the Example sub and it won't be affected in the Main sub at all. With ByRef, when you edit it in Example it will return whatever value it ends up with after running through Example.

Have a look at these if you are still confused:
http://homeandlearn.co.uk/NET/nets9p4.html
http://www.cpearson.com/excel/byrefbyval.aspx


Thanks a lot :smile:
Is it vb6 or vb.net?
Reply 4
Original post by IntelligenceArtifi
Is it vb6 or vb.net?


.net

Quick Reply

Latest

Trending

Trending