Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Joe[x86] on June 23, 2005, 11:10 PM

Title: [VB6] ByRef Replacement
Post by: Joe[x86] on June 23, 2005, 11:10 PM
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
Title: Re: [VB6] ByRef Replacement
Post by: Eric on June 24, 2005, 01:04 AM
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.
Title: Re: [VB6] ByRef Replacement
Post by: TehUser on June 24, 2005, 07:17 AM
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.
Title: Re: [VB6] ByRef Replacement
Post by: 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.
Title: Re: [VB6] ByRef Replacement
Post by: 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.
Title: Re: [VB6] ByRef Replacement
Post by: TehUser on June 25, 2005, 10:04 PM
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.
Title: Re: [VB6] ByRef Replacement
Post by: dxoigmn on June 26, 2005, 01:15 AM
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?
Title: Re: [VB6] ByRef Replacement
Post by: Joe[x86] on June 28, 2005, 11:31 AM
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.
Title: Re: [VB6] ByRef Replacement
Post by: 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?
Title: Re: [VB6] ByRef Replacement
Post by: 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..
Title: Re: [VB6] ByRef Replacement
Post by: MyndFyre on June 28, 2005, 04:26 PM
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.
Title: Re: [VB6] ByRef Replacement
Post by: LivedKrad on June 28, 2005, 07:44 PM
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.