How do I do this in C#?

  • Thread starter Thread starter Buzby
  • Start date Start date
B

Buzby

Hi Newsgroupies,

I am an old C++ dog trying to learn new C# tricks! :P

For example, how do I do this in C#?

// C++
typedef double FLOAT_TYPE;

And where would I place it in my code to be 'globally' visible? (I fully
appreciate that 'Global' is a dirty word in C# )

And if anyone can recommend a good 2003.Net book; no authors please ;-)

Thanks!


Buzby, UK
 
I don't believe you can.
There's no such thing as #define, typedef and while derivation might be an
option, you cannot extend primitive types.
 
Buzby said:
I am an old C++ dog trying to learn new C# tricks! :P

For example, how do I do this in C#?

// C++
typedef double FLOAT_TYPE;

You can't. There's no equivalent of typedef in C#.
And where would I place it in my code to be 'globally' visible? (I fully
appreciate that 'Global' is a dirty word in C# )

And if anyone can recommend a good 2003.Net book; no authors please ;-)

I've found Jeffrey Richter's "Applied Microsoft .NET Framework
Programming" to be excellent, but it really depends on what you want.
 
John Wood said:
I don't believe you can.
There's no such thing as #define, typedef and while derivation might be an
option, you cannot extend primitive types.

#define *does* exist, but in a different way - basically
"preprocessor" symbols (there's no real preprocessor, but the symbols
are used in a way reminiscent of the C preprocessor) are either defined
or not, and that's used in #if blocks, by the ConditionalAttribute etc.
You can't #define a symbol to a value, or a macro, or anything like
that.
 
Right, I meant no #define that can be used for substitution to solve the
guy's problem.
 
Thanks guys!

Yes I know there is no 'typedef' in C# but there MUST be some equivalent
way of doing this?

Is there no way of creating your own type in C#? Otherwise in the future
I would have to trawl through my entire code and replace 'double' with
'decimal' or 'float' if I need to change the accuracy.

I think there is a way of doing it by deriving from a struct?

Thanks again

Buzby, UK
 
I think its correct to say that the next release of the framework is
supposed to support user defined type. Google: "ASP.NET 2.0"

As for books check out "Andrew Troelsen" [1]

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

[1]
http://www.amazon.com/exec/obidos/tg/detail/-/1893115593/002-7479876-6306456?v=glance
 
Thanks guys!

Yes I know there is no 'typedef' in C# but there MUST be some equivalent
way of doing this?

nope, unfortunately not
Is there no way of creating your own type in C#? Otherwise in the future
I would have to trawl through my entire code and replace 'double' with
'decimal' or 'float' if I need to change the accuracy.

search and replace, I'm afraid, maybe some refactoring in the future? I won't hold my breath waiting for it though.
I think there is a way of doing it by deriving from a struct?

nope, can't do that either. no struct inheritance.
 
you can do

using FLOAT_TYPE = double;

but this doesn't work globally, it works only sourcefile based. I wouln't
recommend it, unless you are trying to use it to avoid name collisions.

The only thing you can do is to wrap your double into a struct.
Maybe generics can be used also.
 
Thanks guys!

I knew you wouldn't let me down!

And if any of you should see 'Bill' on your travels please tell him from
me that his "C-Sharp" should be renamed "C-Hash" :P

Buzby, UK
 
ah yeah - great idea. it never occurred to me, i always thought of aliasing
with the using keyword as providing a shorter name for a class in an
arbitrary namespace. but you can indeed use it for primitive types also.
genius!

--
John Wood
EMail: first name, dot, second name at priorganize.com


Daniel Jin said:
:

actually you know what, I take that back. aliasing seems to be the trick.

using Float_Type = System.Double; seems to do the trick. makes your code
somewhat unreadable though.
 
I experimented in one interop project with the using directive, i.e.

using zfloat = System.Double;

because it does alias types similar to a C++ typedef. However, I
really think this was designed as another method for avoiding naming
collisions only. There is no way to put the alias in effect for
multiple source files. You can keep copying using directives into
every new file where you need it but it defeats the point.
 
This is true but it is better to have to change it in every file rather
than having to change it in every line
 
Back
Top