473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Generic param must implement "shift" operation

Hi all,

I want to write a generic class that does this:

Public Class X (Of T)
Public Sub Method(param As T)
dim x as T = param >3
End Sub
End Class

So, I need to constrain T to types that implement the ">>" operator. Is
there a way to do this?

TIA - Bob
Dec 10 '07 #1
11 1859
"Bob Altman" <rd*@nospam.nospamschrieb
Hi all,

I want to write a generic class that does this:

Public Class X (Of T)
Public Sub Method(param As T)
dim x as T = param >3
End Sub
End Class

So, I need to constrain T to types that implement the ">>" operator.
Is there a way to do this?

No, probably not. See topic "base numeric type?" three days ago.

What else do you intend to do with this type? If it was only shifting, you
wouldn't need this Sub.
Armin

Dec 10 '07 #2
Hi Bob,

Yes, if your generic class's certain method will call the parameter
object's particular operator, you need to make sure the passed parameter
objects support the certain operator. In VB.NET 9 (from VS 2005), VB.NET
has started supporting operator overload:

#Overloading Operators in VB.NET 2.0
http://www.developer.com/net/vb/arti...0926_3512311_2

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>From: "Bob Altman" <rd*@nospam.nospam>
Subject: Generic param must implement "shift" operation
Date: Mon, 10 Dec 2007 09:58:59 -0800

Hi all,

I want to write a generic class that does this:

Public Class X (Of T)
Public Sub Method(param As T)
dim x as T = param >3
End Sub
End Class

So, I need to constrain T to types that implement the ">>" operator. Is
there a way to do this?

TIA - Bob
Dec 11 '07 #3
"Steven Cheng[MSFT]" <st*****@online.microsoft.comschrieb
Hi Bob,

Yes, if your generic class's
The classes are not classes but value types called SByte, Byte, Short,
UShort, Integer, UInteger, Long, or ULong.

What he is looking for is something like

Public Sub Method(Of t As {SByte, Byte, Short, UShort, Integer, UInteger,
Long, ULong})()
End Sub

which obviously is not possible. So, only method overloading is left.
certain method will call the parameter
object's particular operator, you need to make sure the passed
parameter objects support the certain operator. In VB.NET 9 (from
VS 2005), VB.NET has started supporting operator overload:

#Overloading Operators in VB.NET 2.0
http://www.developer.com/net/vb/arti...0926_3512311_2

The >operator is a VB.Net native operator that can only be applied to the
types mentioned above. Therefore the question in my previous post what else
has to be done to the value passed.
Armin

Dec 11 '07 #4
Yes, Armin has it exactly correct. I want to be able to perform a numeric
operation (in this case, a shift operation, but it could be as simple as
addition) on a generic argument. This means that I need to constrain the
argument to types that implement the numeric operation. As Armin points
out, the built-in types that implement a shift function are limited to the
numeric value types.

FWIW, here's the longer story of what I'm trying to accomplish. The BCL
contains a class that manages an array of bits (Collections.BitArray). This
class is significant overkill for the simple bit twiddling that I want to
perform. So I wrote a structure (a value-type) that implements simple bit
set and clear operations on an underlying UInt. Since it's a value type its
data is allocated on the stack, so it's a lot cheaper to use that BitArray.

So... my Bit32 structure accepts a 32-bit integer as a constructor argument,
lets you access individual bits, and lets you set or retrieve the entire
32-bit value. That got me to thinking that I really don't want to copy and
paste the code to a 64-bit version, which differs from the 32-bit version
only by the type of the underlying data value. But I can't come up with a
way to code a generic Bits(Of T) structure because I need to perform And,
Or, and Shift operations on a type T.

- Bob

"Armin Zingler" <az*******@freenet.dewrote in message
news:ea**************@TK2MSFTNGP06.phx.gbl...
"Steven Cheng[MSFT]" <st*****@online.microsoft.comschrieb
>Hi Bob,

Yes, if your generic class's

The classes are not classes but value types called SByte, Byte, Short,
UShort, Integer, UInteger, Long, or ULong.

What he is looking for is something like

Public Sub Method(Of t As {SByte, Byte, Short, UShort, Integer, UInteger,
Long, ULong})()
End Sub

which obviously is not possible. So, only method overloading is left.
>certain method will call the parameter
object's particular operator, you need to make sure the passed
parameter objects support the certain operator. In VB.NET 9 (from
VS 2005), VB.NET has started supporting operator overload:

#Overloading Operators in VB.NET 2.0
http://www.developer.com/net/vb/arti...0926_3512311_2


The >operator is a VB.Net native operator that can only be applied to
the types mentioned above. Therefore the question in my previous post what
else has to be done to the value passed.
Armin

Dec 11 '07 #5
Thanks for Armin's further input.

Hi Bob,

Yes, now I got your actual concern, and your generic types are limited in a
certain colletion of numeric types. If so, I agree with Armin that method
overloading is the reasonable approach here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Bob Altman" <rd*@nospam.nospam>
References: <OW**************@TK2MSFTNGP03.phx.gbl>
<bg**************@TK2MSFTNGHUB02.phx.gbl>
<ea**************@TK2MSFTNGP06.phx.gbl>
>Subject: Re: Generic param must implement "shift" operation
Date: Tue, 11 Dec 2007 10:24:35 -0800

Yes, Armin has it exactly correct. I want to be able to perform a numeric
operation (in this case, a shift operation, but it could be as simple as
addition) on a generic argument. This means that I need to constrain the
argument to types that implement the numeric operation. As Armin points
out, the built-in types that implement a shift function are limited to the
numeric value types.

FWIW, here's the longer story of what I'm trying to accomplish. The BCL
contains a class that manages an array of bits (Collections.BitArray).
This
>class is significant overkill for the simple bit twiddling that I want to
perform. So I wrote a structure (a value-type) that implements simple bit
set and clear operations on an underlying UInt. Since it's a value type
its
>data is allocated on the stack, so it's a lot cheaper to use that BitArray.

So... my Bit32 structure accepts a 32-bit integer as a constructor
argument,
>lets you access individual bits, and lets you set or retrieve the entire
32-bit value. That got me to thinking that I really don't want to copy
and
>paste the code to a 64-bit version, which differs from the 32-bit version
only by the type of the underlying data value. But I can't come up with a
way to code a generic Bits(Of T) structure because I need to perform And,
Or, and Shift operations on a type T.
>>
Dec 12 '07 #6
Hi Steven,

I don't understand. If you go back to my original question (slightly
modified to make it a little more complete):

Public Structure Bits(Of T)
Private _value As T

Public Sub New(value as T)
_value = value
End Sub

Public Function GetBit(bitNumber as Int32) As Boolean
Return CBool((_value >bitNumber) And 1)
End Function
End Class

How do I use "method overloading" to get the the >and And functions in
GetBit to compile?

- Bob

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:yy**************@TK2MSFTNGHUB02.phx.gbl...
Thanks for Armin's further input.

Hi Bob,

Yes, now I got your actual concern, and your generic types are limited in
a
certain colletion of numeric types. If so, I agree with Armin that method
overloading is the reasonable approach here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>From: "Bob Altman" <rd*@nospam.nospam>
References: <OW**************@TK2MSFTNGP03.phx.gbl>
<bg**************@TK2MSFTNGHUB02.phx.gbl>
<ea**************@TK2MSFTNGP06.phx.gbl>
>>Subject: Re: Generic param must implement "shift" operation
Date: Tue, 11 Dec 2007 10:24:35 -0800

Yes, Armin has it exactly correct. I want to be able to perform a numeric
operation (in this case, a shift operation, but it could be as simple as
addition) on a generic argument. This means that I need to constrain the
argument to types that implement the numeric operation. As Armin points
out, the built-in types that implement a shift function are limited to the
numeric value types.

FWIW, here's the longer story of what I'm trying to accomplish. The BCL
contains a class that manages an array of bits (Collections.BitArray).
This
>>class is significant overkill for the simple bit twiddling that I want to
perform. So I wrote a structure (a value-type) that implements simple bit
set and clear operations on an underlying UInt. Since it's a value type
its
>>data is allocated on the stack, so it's a lot cheaper to use that
BitArray.

So... my Bit32 structure accepts a 32-bit integer as a constructor
argument,
>>lets you access individual bits, and lets you set or retrieve the entire
32-bit value. That got me to thinking that I really don't want to copy
and
>>paste the code to a 64-bit version, which differs from the 32-bit version
only by the type of the underlying data value. But I can't come up with a
way to code a generic Bits(Of T) structure because I need to perform And,
Or, and Shift operations on a type T.
>>>

Dec 12 '07 #7
"Bob Altman" <rd*@nospam.comschrieb
Hi Steven,

I don't understand. If you go back to my original question
(slightly modified to make it a little more complete):

Public Structure Bits(Of T)
Private _value As T

Public Sub New(value as T)
_value = value
End Sub

Public Function GetBit(bitNumber as Int32) As Boolean
Return CBool((_value >bitNumber) And 1)
End Function
End Class

How do I use "method overloading" to get the the >and And
functions in GetBit to compile?
Overloading wasn't meant in combination with generics.

The overloaded versions of this (impossible) method

Public Sub Method(Of t As {SByte, Byte, Short, UShort, Integer, UInteger,
Long, ULong})()
End Sub

are

Public Sub Method(Value as SByte)
End Sub

Public Sub Method(Value as Byte)
End Sub

...
Armin
Dec 12 '07 #8
"Bob Altman" <rd*@nospam.nospamschrieb
Yes, Armin has it exactly correct. I want to be able to perform a
numeric operation (in this case, a shift operation, but it could be
as simple as addition) on a generic argument. This means that I
need to constrain the argument to types that implement the numeric
operation. As Armin points out, the built-in types that implement a
shift function are limited to the numeric value types.

FWIW, here's the longer story of what I'm trying to accomplish. The
BCL contains a class that manages an array of bits
(Collections.BitArray). This class is significant overkill for the
simple bit twiddling that I want to perform. So I wrote a structure
(a value-type) that implements simple bit set and clear operations
on an underlying UInt. Since it's a value type its data is
allocated on the stack, so it's a lot cheaper to use that BitArray.
cheaper than?
So... my Bit32 structure accepts a 32-bit integer as a constructor
argument, lets you access individual bits, and lets you set or
retrieve the entire 32-bit value. That got me to thinking that I
really don't want to copy and paste the code to a 64-bit version,
which differs from the 32-bit version only by the type of the
underlying data value. But I can't come up with a way to code a
generic Bits(Of T) structure because I need to perform And, Or, and
Shift operations on a type T.
Well,, it's not possible because VB needs specific types to do this. These
are basic operations on the native data types that are compiled inline and
therefore the compiler exactly need to know what it is dealing with. It
needs to know the size, the storage format of the value. For example,
performing an And operation on Integer values must create different code and
only access 32 bits. Without the compiler knowing the type it can not know
which code to generate.
Armin

Dec 12 '07 #9
>Since it's a value type its data is
>allocated on the stack, so it's a lot cheaper to use that BitArray.

cheaper than?
A Structure behaves in many ways like a Class. It encapsulates data and
code, and it can expose public data, methods, and properties. However, data
for a Structure is allocated on the stack. If you allocate a local
Structure variable then its data resides on the stack until it goes out of
scope, at which time its stack space is simply reclamed. On the other hand,
a class is allocated from the managed heap. When its life is over then the
GC needs to run to clean it up.

In my case, I have a routine that executes frequently. That routine wants
to manipulate bits in an Integer. I could do this:

Sub ExecuteThis10TimesPerSecond()
Dim myBits As New BitArray(m_value)
...
End Sub

The problem with this is that every time my routine runs I create another
instance of the BitArray class which needs to be returned to the heap by the
garbage collector. But by writing my own "lightweight" bit manipulation
Structure rather than a Class then the above code no longer allocates memory
from the managed heap.
Well,, it's not possible because VB needs specific types to do this. These
are basic operations on the native data types that are compiled inline and
therefore the compiler exactly need to know what it is dealing with. It
needs to know the size, the storage format of the value. For example,
performing an And operation on Integer values must create different code
and only access 32 bits. Without the compiler knowing the type it can not
know which code to generate.
I actually knew that from the start. My real reason for posting the
question was to see if anyone had some brilliant (or sneaky) way around the
fact that generics don't allow you to constrain their type to either numeric
value types or to types that implement a particular operator. The answer
will probably be "no, you can't get there from here", but it doesn't hurt to
ask.
Dec 12 '07 #10
"Bob Altman" <rd*@nospam.nospamschrieb
Since it's a value type its data is
allocated on the stack, so it's a lot cheaper to use that
BitArray.
cheaper than?

A Structure behaves in many ways like a Class. [...]
Nothing new so far. :)
[...] But by writing my own "lightweight" bit manipulation Structure
rather than a Class then the above code no longer allocates memory from
the managed heap.
Then I think you consider your structure being "cheaper than" the BitArray.
You wrote "it's a lot cheaper to use that BitArray." Therefore my question
"cheaper than?".

>[...] The answer will probably be "no, you can't get
there from here", but it doesn't hurt to ask.
No, it doesn't. Sometimes the answer hurts. ;)
Armin

Dec 12 '07 #11
Thanks for Armin's clarify.

Hi Bob,

Yes, as Armin explained, method overload means create multiple version of
the same function which accept different type of the same parameter. Method
overload no longer involve generic. For your scenario here, generic doesn't
quite work.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Armin Zingler" <az*******@freenet.de>
References: <OW**************@TK2MSFTNGP03.phx.gbl>
<bg**************@TK2MSFTNGHUB02.phx.gbl>
<ea**************@TK2MSFTNGP06.phx.gbl>
<uS**************@TK2MSFTNGP03.phx.gbl>
<yy**************@TK2MSFTNGHUB02.phx.gbl>
<uf**************@TK2MSFTNGP04.phx.gbl>
>Subject: Re: Generic param must implement "shift" operation
Date: Wed, 12 Dec 2007 17:52:05 +0100
>
"Bob Altman" <rd*@nospam.comschrieb
>Hi Steven,

I don't understand. If you go back to my original question
(slightly modified to make it a little more complete):

Public Structure Bits(Of T)
Private _value As T

Public Sub New(value as T)
_value = value
End Sub

Public Function GetBit(bitNumber as Int32) As Boolean
Return CBool((_value >bitNumber) And 1)
End Function
End Class

How do I use "method overloading" to get the the >and And
functions in GetBit to compile?

Overloading wasn't meant in combination with generics.

The overloaded versions of this (impossible) method

Public Sub Method(Of t As {SByte, Byte, Short, UShort, Integer, UInteger,
Long, ULong})()
End Sub

are

Public Sub Method(Value as SByte)
End Sub

Public Sub Method(Value as Byte)
End Sub

...
Armin
Dec 13 '07 #12

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

Similar topics

1
by: John C. Young | last post by:
Hi, I work in a 24/7 Network Operation Center. One of my responsibilities is to provide a report of all of the happenings during my 8 hour shift to those who are scheduled after me and to...
0
by: John C. Young | last post by:
Hi, I work in a 24/7 Network Operation Center. One of my responsibilities is to provide a report of all of the happenings during my 8 hour shift to those who are scheduled after me and to...
3
by: Kannan | last post by:
Hello, I have a requirement that specifies that I display a "Please Wait..." window (preferably modal) along with a gif that mimics a progress bar during a save operation. The dialog needs to be...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
8
by: silentlights | last post by:
Hi, Is there a possibiliy to improve division or Modulo operations in the following, tmp1 = 123; tmp2 = 123; frame = ((char)((tmp1/100)+48)); // Division tmp1 = (tmp2...
13
by: Dave win | last post by:
howdy.... plz take a look at the following codes, and tell me the reason. 1 #define swap(a,b) a=a^b;b=b^a;a=a^b 2 3 int main(void){ 4 register int a=4; 5 register int b=5;...
1
by: ray | last post by:
Hi, Now, I have a datagrid with data input. The data type is nvarchar. While I press "SHIFT+ENTER", it moves to a new row for the same data row. Can I prevent it from happening? Thanks a lot,...
12
by: Thomas Zhu | last post by:
hello all, n is an 32bit-integer, how to calculate sqrt(n) I know high(n) is the the value, but how to use bit operation to implement this function. thanks in advance. yours Yin
3
by: =?Utf-8?B?Sm9uIEU=?= | last post by:
I have an interface class with maybe eight functions, defined in one workspace and am defining a class in a second workspace that derives from this interface. Unfortunately only 7 of the 8...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.