Allow multiple variable names with single type specifier in functin call
Current:
Public Function DoSomething(x As Integer, y As Integer, w As Integer, h As Integer, Value As Integer, Name As String, Count As Integer) As Boolean
Propsed:
Public Function DoSomething(x, y, w, h, Value As Integer, Name As String, Count As Integer) As Boolean
All variables x, y, w, h, Value would be of type Integer, like in Dim statement.
15 comments
-
Anonymous commented
@PavlinII : Yeah I Agree with you, it's make uneasy to read when the lines meet upper than 10000 line code. When I create a long parameters for function for example 12 / 13 parameters, I create a structure or class, but if your suggest was available in next release vs, it's useful .. ;-)
-
PavlinII
commented
Anonymous: Last line of my comment from October 27, 2011:
> - I could use VB6 notations using %, @, ! and other special characters, but it does not meet my request for easily readable code.
-
Anonymous commented
Public Function DoSomething(x%, y%, w%, h%, value%, name$, count%) As Boolean
Return True
End FunctionIt's more simple and ready to use..
-
PavlinII
commented
You did not understand that this proposal extends current state and does not remove anything.
Current longer syntax have to be preserved of course.
-
Marc Johnston
commented
no vote ... breaks backwards compatibility
-
Nick Roberts
commented
Just to note that I largely accept Pavinll's comments.
-
PavlinII
commented
Nick Roberts:
DoSomething with x, y, w, h is just an example to illustrate the issue. You can always refactor many arguments into DoSomething(Args As DoSomethingArumentsWrapper) of course.But refactoring adds even more code and forces consumer of function to wrap arguments - add unnecessary wrapper creating code when more useful overload member is available.
Check Rectangle class constructor. location+size constructor is available. And x+y+width+height is available as well. Selection depends on specified scenario.
My original suggestion would save some code in scenarios when:
- you just have more arguments of same type
- you want to provide suitable overloasd (location+size and x+y+w+h)
- you have more arguments of same type that can't be grouped into meaningfull wrapper (SHA as you mentioned or: From, To, CC, BCC, ReplyTo, Subject, Body etc.)
- you do not want to write argument wrapper for small private helper functions
OR
- when you're writing that wrapper and you want it to have useful constructor ;)Inside that wrapper would be for example (prefixes ommited):
Private x, y, w, h, Value As Integer
And would just copy paste this into constructor parameters. Now, you have to copy it and add 4x As Integer clause.Even Point and Size2D constructors could use suggested feature.
I think it might be very useful.
-
Nick Roberts
commented
Hmmm. In your example (perhaps this is not fair) I would rather write:
Structure Point
Public X As Integer
Public Y As Integer
Public Sub New(X As Integer, Y As Integer)
Me.X = X
Me.Y = Y
End Sub
End Structure
Structure Size2D
Public Width As Integer
Public Height As Integer
Public Sub New(Width As Integer, Height As Integer)
Me.Width = Width
Me.Height = Height
End Sub
End Structure
Public Function CanDoSomething(TopLeft As Point, Size As Size2D, Value As Integer, Name As String, Count As Integer) As Boolean
...
End FunctionOr better still:
Public Function CanDoSomething(TopLeft As Point, -- top left corner of box
Size As Size2D, -- width and height of box
Value As Integer, -- median value expected
Name As String, -- name of division to search
Count As Integer) -- number of sub-boxes within box
As BooleanIn other words, I don't think this suggestion is likely to be genuinely useful very often.
Occasionally it might be. E.g. a function that computes SHA takes eight parameters the same type (if I remember correctly).
-
PavlinII
commented
Nebire:
Private Const x As ULong = 18446744073700000000UL
This line works, of course. But UL suffix is declaring literal value type. My suggestion was about variable name/type declaration.It's different scenario than my original suggestion, but I think our misunderstanding is clear now.
-
NEBIRE
commented
However, it seems a good idea.
-
NEBIRE
commented
Then, how you define any like this:
This line give error:
Private Const x As ULong = 18446744073700000000This line give it's ok:
Private Const x As ULong = 18446744073700000000ULThis function need special chars for correct values. If you retire UI from 0 give error, if you retire 0, give error overflow in both cases.
' convert 4 bytes from an array to uinteger.
Private Function Convert(ByRef Bytes() As Byte, ByVal Index As UInt32) As UInteger
Return ((0UI Or Bytes(Index)) Or (0UI Or (Bytes(Index + 1)) << 8) Or (0UI Or (Bytes(Index + 2)) << 16) Or (0UI Or (Bytes(Index + 3)) << 24))
'End Function
dim X as UInteger = 0UI
Does work for you, want or not want... -
PavlinII
commented
NEBIRE: I'ts far from what I want. I want the code to be read easily.
- hUL does not work for me.
- I could use VB6 notations using %, @, ! and other special characters, but it does not meet my request for easily readable code. -
NEBIRE
commented
You may declare variables with type letters, like vb6.
So: 'hUL' it's 'h as ULong' ... Don't is very different from you want. -
PavlinII
commented
Adding {} is not good idea.
- variables are declared here like Dim statement. And Dim statement's convention does not use {} in variable list.
- variable names are not collection initializers in this case
- {} are not necessary and does not add new information nor differentiate any other situation. -
Adam Speight
commented
I think this is good idea, be using the array literal syntax as way to group the parameter names
Eg
[code]
Public Function DoSomthing( {x,y,w,h} As Integer, {ForeName, Surname } As String ) As Boolean
[/code]