473,848 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what are generic Subs and Functions for?

While experimenting with the Feb CTP Edition of VB 2005, I came across
"generic procedures". You can write:

Public Class Foo

Public Sub MySub(Of tDisp As IDisposable)(By Val vMyParm As Integer)

End Sub
End Class

I know what generic classes are for, but a generic Sub? Does anybody know
why they are here? I found nothing about it in the help, it is just
explained as a valid syntax for the Sub statement.

Urs


Nov 21 '05 #1
6 2789
Urs,
| I know what generic classes are for, but a generic Sub? Does anybody know
| why they are here?
They allow you to define a method (sub or function) that behaves
polymorphically across types. Just as generic classes allow you to define
classes that behave polymorphically across types...

Consider:

' VS.NET 2005 syntax
Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T
If expression Then
Return truePart
Else
Return falsePart
End If
End Function

Which allows you to use the above IIf in Whidbey (aka VS.NET 2005, due out
later in 2005) with Option Strict On, without needing
to cast the return value.

Dim x, y, r As Integer
r = IIf(x < y, x, y)

Dim a, b, c As String
r = IIf(a Is Nothing, a, b)

NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
specifically needed...

I can see defining a similar routine for Choose, something like:

' VS.NET 2005 syntax
Public Function Choose(Of T)(ByVal index As Integer, _
ParamArray ByVal choice() As T) As T
Return choice(index-1)
End Function

Again you can use the above Choose with Option Strict On without needing to
cast the return value! (see below)

Dim r As String = Choose(x, "Happy", "Sad", "Indifferen t")

I can also see, Min and Max:

' VS.NET 2005 syntax
Public Function Min(Of T As IComparable(Of T))(ByVal value1 As T, ByVal
value2 As T) As T
If value1.CompareT o(value2) <= 0 Then
Return value1
Else
Return value2
End If
End Function

' VS.NET 2005 syntax
Public Function Max(Of T As IComparable(Of T))(ByVal value1 As T, ByVal
value2 As T) As T
If value1.CompareT o(value2) >= 0 Then
Return value1
Else
Return value2
End If
End Function

NOTE: Min & Max need the constraint that says the type given can compare
themselves with others, the IComparable(Of T) prevents boxing...

Dim r As Integer = Min(1, 2)
Dim r As Double = Max(1.5, 2.5)

Hope this helps
Jay


"Urs Eichmann" <xx@yy.com> wrote in message
news:ea******** ******@TK2MSFTN GP10.phx.gbl...
| While experimenting with the Feb CTP Edition of VB 2005, I came across
| "generic procedures". You can write:
|
| Public Class Foo
|
| Public Sub MySub(Of tDisp As IDisposable)(By Val vMyParm As Integer)
|
| End Sub
|
|
| End Class
|
| I know what generic classes are for, but a generic Sub? Does anybody know
| why they are here? I found nothing about it in the help, it is just
| explained as a valid syntax for the Sub statement.
|
| Urs
|
|
|
|
Nov 21 '05 #2
Jay,
Urs,
| I know what generic classes are for, but a generic Sub? Does anybody know
| why they are here?
They allow you to define a method (sub or function) that behaves
polymorphically across types. Just as generic classes allow you to define
classes that behave polymorphically across types...

Consider:

' VS.NET 2005 syntax
Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T
If expression Then
Return truePart
Else
Return falsePart
End If
End Function
thanks for the explanation. I guess I can also write

Public Function ToArray(Of T)(vList as IList) As T()
return directcast(vLis t.ToArray(T),T( ))
End Function

instead of the horrible

dim a() as MyClass= DirectCast(vLis t.ToArray(getty pe(MyClass)),My Class())

I use today to convert an IList to an array. Now I guess I can just use

dim a() as MyClass = ToArray(Of MyClass)(vList)

I look forward to it already :-)

NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
specifically needed...
is that new? 2003 doesn't do it

I can see defining a similar routine for Choose, something like:

' VS.NET 2005 syntax
Public Function Choose(Of T)(ByVal index As Integer, _
ParamArray ByVal choice() As T) As T
Return choice(index-1)
End Function


Boah, is somebody still using Choose? :-)

Thanks for your help
Urs
Nov 21 '05 #3
Urs,
| Public Function ToArray(Of T)(vList as IList) As T()
| return directcast(vLis t.ToArray(T),T( ))
| End Function
I have not tried (I'm in the middle of rebuilding my test machine).

I'm not sure if VB and/or the Framework will allow the DirectCast or not.
There are a number of rules on what is allowed & what is not allowed, I
don't remember them all...

I would mostly expect it would be allowed.

However! As you may know with the new Generic list classes your conversion
is "unneeded". ;-)

| > NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
| > specifically needed...
| is that new? 2003 doesn't do it

Yes its new as Generics are new... it is closely related to overloading
methods. In C# its called Type Inference & is explained here, I don't see
the VB definition.

http://msdn2.microsoft.com/library/t...us,vs.80).aspx

Here is a VB swap sample (based on the above):

Public Module UtilityModule

Public Sub Swap(Of T)(ByRef lhs As T, ByRef rhs As T)
Dim temp As T
temp = lhs
lhs = rhs
rhs = temp
End Sub

End Module

Then you can call Swap with either:

Dim a As Integer = 1
Dim b As Integer = 2

Swap(Of Integer)(a, b) ' explicitly call Swap(Of Integer)
Swap(a, b) ' implicitly call Swap(Of Integer)
'as Integer was inferred from the parameters.

| Boah, is somebody still using Choose? :-)
I use it where it makes sense to use it! It makes sense to use it where you
have a list of values to choose from. Granted I don't need it very often...
I know in one program I overloaded Choose with a dozen type safe versions,
using Choose(Of T) will reduce it to a single version...

FWIW: I find Choose to be more useful then the Switch function, as its easy
to create a type safe Choose, however its hard (impossible?) to make a type
safe Switch function...

Hope this helps
Jay


"Urs Eichmann" <ur***@online.n ospam> wrote in message
news:OA******** ******@tk2msftn gp13.phx.gbl...
| Jay,
|
| > Urs,
| > | I know what generic classes are for, but a generic Sub? Does anybody
know
| > | why they are here?
| > They allow you to define a method (sub or function) that behaves
| > polymorphically across types. Just as generic classes allow you to
define
| > classes that behave polymorphically across types...
| >
| > Consider:
| >
| > ' VS.NET 2005 syntax
| > Public Function IIf(Of T)(ByVal expression As Boolean, _
| > ByVal truePart As T, ByVal falsePart As T) As T
| > If expression Then
| > Return truePart
| > Else
| > Return falsePart
| > End If
| > End Function
|
| thanks for the explanation. I guess I can also write
|
| Public Function ToArray(Of T)(vList as IList) As T()
| return directcast(vLis t.ToArray(T),T( ))
| End Function
|
| instead of the horrible
|
| dim a() as MyClass= DirectCast(vLis t.ToArray(getty pe(MyClass)),My Class())
|
| I use today to convert an IList to an array. Now I guess I can just use
|
| dim a() as MyClass = ToArray(Of MyClass)(vList)
|
| I look forward to it already :-)
|
| >
| > NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
| > specifically needed...
|
| is that new? 2003 doesn't do it
|
| >
| > I can see defining a similar routine for Choose, something like:
| >
| > ' VS.NET 2005 syntax
| > Public Function Choose(Of T)(ByVal index As Integer, _
| > ParamArray ByVal choice() As T) As T
| > Return choice(index-1)
| > End Function
|
| Boah, is somebody still using Choose? :-)
|
| Thanks for your help
| Urs
Nov 21 '05 #4
>
| Boah, is somebody still using Choose? :-)
I use it where it makes sense to use it! It makes sense to use it where you
have a list of values to choose from. Granted I don't need it very often...
I know in one program I overloaded Choose with a dozen type safe versions,
using Choose(Of T) will reduce it to a single version...

FWIW: I find Choose to be more useful then the Switch function, as its easy
to create a type safe Choose, however its hard (impossible?) to make a type
safe Switch function...


I use neither because the tend to become lengthy and spaghetti-like. I
always use Select Case. But this is my personal preference, I guess.

urs
Nov 21 '05 #5
Urs,
| I use neither because the tend to become lengthy and spaghetti-like. I
| always use Select Case. But this is my personal preference, I guess.
Odd, Select Case seems to be longer & more "spaghetti-like" then Choose,
especially for the simple cases where I would use choose Choose over Select
Case!

One short & simple line for the Choose
Dim x As Integer = Choose(index, "x", "xxx", "xx", "xxxx")

Where as there are 13 lines for the Select Case.
Dim x As Integer
Select Case index
Case 0
x = "x"
Case 1
x = "xxx"
Case 2
x = "xx"
Case 3
x = "xxxx"
Case Else
Throw New ArgumentOutOfRa ngeException(.. .)
End Case

FWIW:

Select Case by itself suggests using the "Replace Condition with
Polymorphism" refactoring
http://www.refactoring.com/catalog/r...ymorphism.html

Both cases (Choose & Select Case) in my example suggests using the
"Substitute Algorithm" refactoring
http://www.refactoring.com/catalog/s...Algorithm.html

Hope this helps
Jay

"Urs Eichmann" <ur***@online.n ospam> wrote in message
news:eI******** *****@tk2msftng p13.phx.gbl...
|>
| > | Boah, is somebody still using Choose? :-)
| > I use it where it makes sense to use it! It makes sense to use it where
you
| > have a list of values to choose from. Granted I don't need it very
often...
| > I know in one program I overloaded Choose with a dozen type safe
versions,
| > using Choose(Of T) will reduce it to a single version...
| >
| > FWIW: I find Choose to be more useful then the Switch function, as its
easy
| > to create a type safe Choose, however its hard (impossible?) to make a
type
| > safe Switch function...
|
| I use neither because the tend to become lengthy and spaghetti-like. I
| always use Select Case. But this is my personal preference, I guess.
|
| urs
Nov 21 '05 #6
Jay,
Odd, Select Case seems to be longer & more "spaghetti-like" then Choose,
especially for the simple cases where I would use choose Choose over Select
Case!


yes, the code is longer, but not "spaghetti-like" because it is clearly
structured. Also, I get intellisense for example when selecting with an
eNum. And it is far more flexible, because you can do things like "Case
0 to 2". And if you have more than 3 or 4 items to choose from, you end
up counting with your fingers to tell which one will be choosed with a
certain index.

So instead of replacing Choose with Select Case sometimes later, I use
Select case in the first place. By using a keyboard shortcut, I don't
even have to type much...

Best regards,
Urs
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2794
by: Colin Mc Mahon | last post by:
Hi all, First off I know the difference between functions and subs, basically a function returns a value on execution of a code block wheras a sub just executes a code block (more or less). I have found however that if I 'call' a function in the same way as I would a sub the function executes the code as if it were a sub ie: Call openDB 'this is a function which creates a connection object to
13
2107
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in with the code. So I have decided to try and make things neater by grouping it into subs. So i have a sub to create the variables, a sub for the connection etc. Only problem is when i call my subs, it says that the variable is undefined. If i...
51
13406
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
3
238
by: jm | last post by:
I have a question about codebehind. I have an upload namespace, class and all in a codebehind. I currently have it so it works with one of my .aspx pages. I want to use it for another .aspx page. The problem is that they codebehind looks at a specific directory (since it was only being used by one page). I need it to decide which directory to go to based upon which page is using the codebehind page. Can I just reference the private...
11
1896
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I mean, for one, delegates point to subs, so when you call a delegate, why not just call the sub dierectly and not bother adding the extra code involved adding the delegate?
1
1254
by: Sam | last post by:
Hi All Does anyone know if there is a standard format where subs and functions should be placed in a class. I always put them at the very end of my class except my constructor (subs first then function)? For example below is my order 1. Constructor Sub New1 2. Constructor Sub New2
2
1333
by: Gonzosez | last post by:
I have created several functions and subs in both VB and Java. How can I put these into a file to be called by different pages. Thanks in advance, Gonzo
8
12628
by: Jeff | last post by:
Still new to vb.net in VS2005 web developer... What is the proper/standard way of doing the following - setting the value of a variable in one sub and calling it from another? E.g., as below. The code below draws an error as indicated. Surely there has to be a better way than to make xxx a session variable? Thanks
2
3526
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to manipulate the bitfields. For instance: <Flags()_ Enum ActionFlags As Byte
0
9893
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11000
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10663
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10348
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7893
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5919
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.