473,804 Members | 3,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics, System.ValueTyp e and Complex Numbers

Hello,

So I'm doing some mathematical modeling and it turns out I need
distinct imaginary and complex types to carry out complex contour
integration.

I want to have it in all three precisions (float, double, and decimal),
and the code is highly redundant, but I can't seem to come up with a
clean way to use Generics in the code. For example, the following
doesn't work

public struct Imag<Twhere T:System.ValueT ype
{
T imag;

public static Imag<Toperator +(Imag<Ta, Imag<Tb) { //implmentation}
public static Imag<Toperator -(Imag<Ta, Imag<Tb) { //implmentation}
public static T operator *(Imag<Ta, Imag<Tb) { //implmentation}
// and so on
}

Basically I need a way to make a Generic struct (can't be a class for
speed) that can only accept floats, doubles and decimals. Is this
possible in the current spec? I tried setting up an interface, but
those do not allow operators.

I really appreciate any help. Thanks.

Ravi

Jan 19 '07 #1
2 1812
I don't think there's a way to do what you want, unless the where
clause can support an Or concept..
Ravi Shekhar wrote:
Hello,

So I'm doing some mathematical modeling and it turns out I need
distinct imaginary and complex types to carry out complex contour
integration.

I want to have it in all three precisions (float, double, and decimal),
and the code is highly redundant, but I can't seem to come up with a
clean way to use Generics in the code. For example, the following
doesn't work

public struct Imag<Twhere T:System.ValueT ype
{
T imag;

public static Imag<Toperator +(Imag<Ta, Imag<Tb) { //implmentation}
public static Imag<Toperator -(Imag<Ta, Imag<Tb) { //implmentation}
public static T operator *(Imag<Ta, Imag<Tb) { //implmentation}
// and so on
}

Basically I need a way to make a Generic struct (can't be a class for
speed) that can only accept floats, doubles and decimals. Is this
possible in the current spec? I tried setting up an interface, but
those do not allow operators.

I really appreciate any help. Thanks.

Ravi
Jan 19 '07 #2

Ravi Shekhar wrote:
Hello,

So I'm doing some mathematical modeling and it turns out I need
distinct imaginary and complex types to carry out complex contour
integration.

I want to have it in all three precisions (float, double, and decimal),
and the code is highly redundant, but I can't seem to come up with a
clean way to use Generics in the code. For example, the following
doesn't work

public struct Imag<Twhere T:System.ValueT ype
{
T imag;

public static Imag<Toperator +(Imag<Ta, Imag<Tb) { //implmentation}
public static Imag<Toperator -(Imag<Ta, Imag<Tb) { //implmentation}
public static T operator *(Imag<Ta, Imag<Tb) { //implmentation}
// and so on
}

Basically I need a way to make a Generic struct (can't be a class for
speed) that can only accept floats, doubles and decimals. Is this
possible in the current spec? I tried setting up an interface, but
those do not allow operators.

I really appreciate any help. Thanks.
Well, all the generic is saving you is the trouble of writing three
separate classes. Really, all you're saving here is typing. There
really shouldn't be that much code; just type it out three times.

Generics are really intended for open-ended type creation. In other
words, if you're in the situation in which you say, "I need imaginary
numbers based on floats, doubles, and decimals, but later I might
create another value type and want an imaginary version of _that_,"
then you need a generic solution. If your set of possible type
parameters is closed, and only three, just write the code three times,
as ugly as that may seem, it's simple and easy to maintain.

Jan 19 '07 #3

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

Similar topics

11
4003
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we can expect when Generics are released? Thanks,
5
4032
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it is almost impossible to figure out what causes this. I've tried adding: AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (SystemErrorHandler); to my main method, and a handler function:
17
3334
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
10
2609
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results other than straining our relationship... I just downloaded Visual C# Express Edition to mess with .NET 2.0 generics. Being a numerically inclined developer, the first thing I wanted to write was a generic complex number class. I also have some...
23
2559
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics vs C++ templates. Does anyone knows a workaround to do this ? Thx : public class C<T> { private T myValue;
12
2748
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as it defines the language more completely. Great to use.
14
5041
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or Object) Is this even possible, or is the composite pattern doomed to use the System.Object type forever? I have tried using interfaces with generics, but I always stumble on the Value property (when trying to return the value of a particular node)
4
10127
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>, both works. private void Form1_Load(object sender, EventArgs e) { System.Collections.SortedList QA1 = new System.Collections.SortedList();
5
1758
by: Leon Mayne | last post by:
We currently have lots of checks in our businesslayer object's Load() functions that look like: If drCourse("txtTTMCnotes").Equals(DBNull.Value) Then Me._txtTTMCnotes = "" Else Me._txtTTMCnotes = CStr(drCourse("txtTTMCnotes")) End If and
0
9710
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...
1
10329
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
10085
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
7626
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
6858
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
5527
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
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
3000
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.