• Welcome to Valhalla Legends Archive.
 

[VB6] ByRef Replacement

Started by Joe[x86], June 23, 2005, 11:10 PM

Previous topic - Next topic

Joe[x86]

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. =)

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
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Eric

#1
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.

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 from: Vote Joe! on June 23, 2005, 11:10 PM
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


Also, using numeric constants is a bad idea.  Use the names, especially if VB provides them for you.

MyndFyre

Quote from: Vote Joe! on June 23, 2005, 11:10 PM
Don't you just hate those functions that use ByRef to return data? Geh, so do I.
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Yegg

Quote from: MyndFyre on June 24, 2005, 08:29 PM
Quote from: Vote Joe! on June 23, 2005, 11:10 PM
Don't you just hate those functions that use ByRef to return data? Geh, so do I.
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.
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.

TehUser

Quote from: Yegg on June 25, 2005, 06:45 PM
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.

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.

dxoigmn

Quote from: Yegg on June 25, 2005, 06:45 PM
Quote from: MyndFyre on June 24, 2005, 08:29 PM
Quote from: Vote Joe! on June 23, 2005, 11:10 PM
Don't you just hate those functions that use ByRef to return data? Geh, so do I.
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.
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.

How would you create a function that returns 2 things without using a reference?

Joe[x86]

#7
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.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

TehUser

Quote from: dxoigmn on June 26, 2005, 01:15 AM
How would you create a function that returns 2 things without using a reference?

Using a structure?

Blaze

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
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No

MyndFyre

Quote from: TehUser on June 28, 2005, 01:41 PM
Quote from: dxoigmn on June 26, 2005, 01:15 AM
How would you create a function that returns 2 things without using a reference?

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

Quote from: Blaze on June 28, 2005, 02:45 PM
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..
Then you have to retype your variables before operations can be performed on them.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

LivedKrad

Quote from: Yegg on June 25, 2005, 06:45 PM
Quote from: MyndFyre on June 24, 2005, 08:29 PM
Quote from: Vote Joe! on June 23, 2005, 11:10 PM
Don't you just hate those functions that use ByRef to return data? Geh, so do I.
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.
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.

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.