All,
It was possible before in Pascal, C++, etc. to define our custom data type
or redefine the existing type, like in Turbo Pascal we could assume that
shortint is int and use all references to shortint like all these variables
became int.
How can we define our own user data type in C# is we can?
Just D.
Jun 23 '06
12 13342
On 22 Jun 2006 20:03:24 -0700, "Bruce Wood" <br*******@cana da.com>
wrote:
just a word, bruce...
what do you say about basic types like int32, uint32, int16, uint16
and so on? they are in fact even more readable than the original
one's. In Unix I also found datatypes like this: u8, i8, u16, i16,
u32, i32... unix programmers always tend to write as less as possible
:-)
I agree to you, if someone uses int32 as a 16 bit type, then you have
the right to complain. on the other hand, if you need one hour to
debug a misuse of a data type, which in first sight has another name
than the original ones, then I doubt a bit of your programming skills
at this time ;-)
And yes, I agree to JustD , this C "feature" was one of the first
things I missed in C#.
so long,
noah Just D wrote: It was possible before in Pascal, C++, etc. to define our custom data type or redefine the existing type, like in Turbo Pascal we could assume that shortint is int and use all references to shortint like all these variables became int.
How can we define our own user data type in C# is we can?
In general terms, defining a new class in C# _is_ defining a new type.
If, however, you mean that you want to define an "alias" for an existing data type, so that, for example, all places in your code that say
short x;
and suchlike use the int type instead of the "short" that is written there, then no, you cannot do that in C#. Furthermore, it's a great feature (or un-feature) that you can't do that. In 20 years of C programming I saw this feature (ab)used only once, and it took me an hour of staring at the code and debugging, wondering why it was acting so #$&*$@ weird, before I looked inside a header file and found that some jackass had redefined a basic type in this way. Fortunately he no longer worked for the company, or I would have been honour-bound to have killed him and mounted his head on a pike or something like that.
I'm very happy that C# doesn't let people do crap like redefine existing, basic types.
However, if you want to define a brand new type, then just declare a new class and there you have it.
noah <noah@home> wrote: I agree to you, if someone uses int32 as a 16 bit type, then you have the right to complain. on the other hand, if you need one hour to debug a misuse of a data type, which in first sight has another name than the original ones, then I doubt a bit of your programming skills at this time ;-)
To me, that just suggests you haven't been in macro hell. When things
end up getting redefined (so that potentially the same name can mean
different things at different points in the same file) life can get
very nasty indeed.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
> I agree to you, if someone uses int32 as a 16 bit type, then you have the right to complain. on the other hand, if you need one hour to debug a misuse of a data type, which in first sight has another name than the original ones, then I doubt a bit of your programming skills at this time ;-)
That was exactly the sort of thing that happened. Someone had created a
macro like this, if I recall correctly (it was a long time ago):
#define int short
I spent an hour or so stepping through the code, trying to figure out
why the code was failing for large values, when everything was stored
in ints which were clearly capable of storing the information.
Anyway, it was something along those lines. Stuff like this, I don't
miss. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: s |
last post by:
Can I do this:
#define MYSTRING "ABC"
..
..
..
char mychar = MYSTRING;
..
..
..
|
by: theotyflos |
last post by:
Hi all,
I have the following:
/*--- SNIP ---*/
typedef struct Argument_s
{
char *address;
int type;
|
by: Angel Lopez |
last post by:
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but...
|
by: Jürgen Hochwald |
last post by:
Hi
Maybe this is a simple question, but I don't know how to solve.
Background: A weather station connected to the serial port sends data
packets. This data packets are containing variables fom one byte up to 4
byted in mixed order. Now I want to define a structure to overlay it with
UNION over the receive buffer for easily access to the single values in the
data packet.
The problem is, that I cannot defind a variable, which only...
|
by: Pol Bawin |
last post by:
Hi All,
Did somebody already define attributes for numerical properties to define
value : minima, maxima, a number of decimal, ...? This information would be
useful to unify syntax
Polo
| |
by: Kay |
last post by:
Hi all,
In vb6, I can define a custom type like this:
Private Type uClient
sName As String
sMonday As Double
sMondayHeadCnt As Integer
End Type
|
by: bokiteam |
last post by:
Hi All,
I have this error msg, but I have already import lib, could you please
advice?
Imports System.Data.OleDb
Public Class Form1 Inherits System.Windows.Forms.Form
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
|
by: Christian Christmann |
last post by:
Hi,
what is the difference between a "#define" and typedef?
A "#define" is handled by the preprocessor and serves as a name
substitude whereas "typedef" is a declaration of a new data type
based on an exisiting one and handled by a compiler.
So, actually both things are equivalent? Or are there any special
cases where it makes a difference which specifier is used?
|
by: alan |
last post by:
Hello world, I'm wondering if it's possible to implement some sort of
class/object that can perform mapping from class types to strings?
I will know the class type at compile time, like so:
const char *s = string_mapper<thetype>();
However I do not know the string to be associated with the type at
compile time, and will need a way to set up the mapping, to be created
at run time, possibly like so:
void foo(char*...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |