473,763 Members | 3,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1901
"Bob Altman" <rd*@nospam.nos pamschrieb
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.nos pam>
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.coms chrieb
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.Bi tArray). 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*******@free net.dewrote in message
news:ea******** ******@TK2MSFTN GP06.phx.gbl...
"Steven Cheng[MSFT]" <st*****@online .microsoft.coms chrieb
>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.nos pam>
References: <OW************ **@TK2MSFTNGP03 .phx.gbl>
<bg************ **@TK2MSFTNGHUB 02.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.Bi tArray).
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(bitNumbe r 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.comw rote in message
news:yy******** ******@TK2MSFTN GHUB02.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.nos pam>
References: <OW************ **@TK2MSFTNGP03 .phx.gbl>
<bg************ **@TK2MSFTNGHUB 02.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.Bi tArray).
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.com schrieb
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(bitNumbe r 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.nos pamschrieb
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.Bi tArray). 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 ExecuteThis10Ti mesPerSecond()
Dim myBits As New BitArray(m_valu e)
...
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 "lightweigh t" 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

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

Similar topics

1
2673
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 management. For a number of years, this has been done via email format, cutting and pasting to a template.
0
1469
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 management. For a number of years, this has been done via email format, cutting and pasting to a template.
3
13486
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 closed at the end of the operation. How do I go about this... Issues are: 1. I believe a showModaldialog would stop all back ground processing and shift control to the modal dialog. 2. I would like to close the dialog from the parent window...
388
21887
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
8
29445
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 % 100); // Mod frame = ((char)((tmp1/10)+48)); tmp1 = (tmp2 % 10);
13
2093
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; 6 swap(a,b); 7
1
1548
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, Ray
12
6319
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
15754
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 functions in my derived class compile whilst just one of them refuses to be recognised, leading to an error message "...does not implement interface member...". Try as I might, I can't fix the error. I have cut and paste names so can rule out simple...
0
9563
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
10145
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...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
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
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.