Valhalla Legends Forums Archive | Visual Basic Programming | [VB6] ByRef Replacement

AuthorMessageTime
JoeTheOdd
Don't you just hate those functions that use ByRef to return data? Geh, so do I. What am I talking about? Things like S, in Socket.GetData S, vbString. I've come up with a method to replace these with a simple function that will return the data as its return, not ByRef. Below is a implementation of this method for use with a Socket, and Line Input'ing from a file. Other uses should be rather similar, and if you actually know VB you will figure it out without any effort whatsoever. =)

[code]Option Explicit

Public Function GetSocketData(Socket As Winsock) As String
    Dim S As String
    Socket.GetData S, 8
    Let GetSocketData = CStr(S)
End Function

Public Function GetLine(FileNum As Integer) As String
    Dim S As String
    Line Input #FileNum, S
    Let GetLine = CStr(S)
End Function[/code]
June 24, 2005, 4:10 AM
LoRd
This not only defeats the purpose of the function using references in the first place, but it's also much slower than just working with it as is...

Perhaps you should take some time to actually learn the purpose of these things before attempting to modify the way that they function.
June 24, 2005, 6:04 AM
TehUser
Well, aside from the overhead associated with function calls, it's not that bad.  Although, if I remember correctly, VB allocates an object of the return type of the function, so rather than creating new strings, I'd have simplified it by doing:

[quote author=Vote Joe! link=topic=11947.msg117180#msg117180 date=1119586212]
[code]Option Explicit

Public Function GetSocketData(Socket As Winsock) As String
Socket.GetData GetSocketData, vbString
End Function

Public Function GetLine(FileNum As Integer) As String
Line Input #FileNum, GetLine
End Function[/code]
[/quote]

Also, using numeric constants is a bad idea.  Use the names, especially if VB provides them for you.
June 24, 2005, 12:17 PM
Myndfyr
[quote author=Vote Joe! link=topic=11947.msg117180#msg117180 date=1119586212]
Don't you just hate those functions that use ByRef to return data? Geh, so do I. [/quote]
It's unclear to me why you would hate these kinds of functions anyway.  Using ByRef enables you to return more than one value, which is pretty convenient and useful IMO.

It's the closest thing VB has to pointers.
June 25, 2005, 1:29 AM
Yegg
[quote author=MyndFyre link=topic=11947.msg117324#msg117324 date=1119662950]
[quote author=Vote Joe! link=topic=11947.msg117180#msg117180 date=1119586212]
Don't you just hate those functions that use ByRef to return data? Geh, so do I. [/quote]
It's unclear to me why you would hate these kinds of functions anyway.  Using ByRef enables you to return more than one value, which is pretty convenient and useful IMO.

It's the closest thing VB has to pointers.
[/quote]
But ByRef isn't always necessary in my opinion. Even though it is there and it may be faster or a better solution, I always find that passing arguments by value is much easier and in the end will usually do the same task.
June 25, 2005, 11:45 PM
TehUser
[quote author=Yegg link=topic=11947.msg117510#msg117510 date=1119743112]
But ByRef isn't always necessary in my opinion. Even though it is there and it may be faster or a better solution, I always find that passing arguments by value is much easier and in the end will usually do the same task.
[/quote]

Just remember that you're sacrificing memory to do so.  Arguments passed by value must be copied for use in the called function.  Arguments passed by reference don't suffer the same problem.
June 26, 2005, 3:04 AM
kamakazie
[quote author=Yegg link=topic=11947.msg117510#msg117510 date=1119743112]
[quote author=MyndFyre link=topic=11947.msg117324#msg117324 date=1119662950]
[quote author=Vote Joe! link=topic=11947.msg117180#msg117180 date=1119586212]
Don't you just hate those functions that use ByRef to return data? Geh, so do I. [/quote]
It's unclear to me why you would hate these kinds of functions anyway.  Using ByRef enables you to return more than one value, which is pretty convenient and useful IMO.

It's the closest thing VB has to pointers.
[/quote]
But ByRef isn't always necessary in my opinion. Even though it is there and it may be faster or a better solution, I always find that passing arguments by value is much easier and in the end will usually do the same task.
[/quote]

How would you create a function that returns 2 things without using a reference?
June 26, 2005, 6:15 AM
JoeTheOdd
You wouldn't. This is for fixing things where its totally un-needed but used anyhow.

Er, on second thought, to return more than one string you could just 0x00 delimit them and split them after being returned, although that could be considered only one string. Oh well.

EDIT -
LoRd[nK], you go write your code however you want to, I'm not making you use this. You say my code is bad, and that it cannot even be considered programming? Hm.. I love your code style in Floodbot. If you don't have something nice to say then fuck off.
June 28, 2005, 4:31 PM
TehUser
[quote author=dxoigmn link=topic=11947.msg117550#msg117550 date=1119766539]
How would you create a function that returns 2 things without using a reference?
[/quote]

Using a structure?
June 28, 2005, 6:41 PM
KkBlazekK
If its possible to return an array, you could do that too and if its not, you could have a delimiter. I would just use a reference though..
June 28, 2005, 7:45 PM
Myndfyr
[quote author=TehUser link=topic=11947.msg117914#msg117914 date=1119984116]
[quote author=dxoigmn link=topic=11947.msg117550#msg117550 date=1119766539]
How would you create a function that returns 2 things without using a reference?
[/quote]

Using a structure?
[/quote]
Yes, but then you have to create a different structure every time you have a different set of values to return.

[quote author=Blaze link=topic=11947.msg117926#msg117926 date=1119987953]
If its possible to return an array, you could do that too and if its not, you could have a delimiter. I would just use a reference though..
[/quote]
Then you have to retype your variables before operations can be performed on them.
June 28, 2005, 9:26 PM
LivedKrad
[quote author=Yegg link=topic=11947.msg117510#msg117510 date=1119743112]
[quote author=MyndFyre link=topic=11947.msg117324#msg117324 date=1119662950]
[quote author=Vote Joe! link=topic=11947.msg117180#msg117180 date=1119586212]
Don't you just hate those functions that use ByRef to return data? Geh, so do I. [/quote]
It's unclear to me why you would hate these kinds of functions anyway.  Using ByRef enables you to return more than one value, which is pretty convenient and useful IMO.

It's the closest thing VB has to pointers.
[/quote]
But ByRef isn't always necessary in my opinion. Even though it is there and it may be faster or a better solution, I always find that passing arguments by value is much easier and in the end will usually do the same task.
[/quote]

They have their different uses. I wouldn't think one is supposed to be a replacement for the other otherwise both options wouldn't be there.
June 29, 2005, 12:44 AM

Search