locked
typedef in c#..? RRS feed

  • Question

  • Hi,

    in C# can i use "using apiStatus=System.Int16" instead of "typedef short apiStatus " in C ???

    thanks in advance..
    Monday, December 21, 2009 1:58 PM

Answers

  • You can use "using apiStatus = System.Int16"

    Unless you defined your own System namespace, this will create an alias to short.
    Monday, December 21, 2009 3:06 PM
  • While that appears to be a pretty cool replacement for typedef, this article brings out some interesting and important differences.  Mainly, the need to define the alias in every page and the lack of a single header definitions file.

    Because of that limitation this idea can result in inconsistencies and defeats the code-once use-many rule.  In other words, be cautious when "using" thing's it wasn't designed for :D.

    E.G. 

    #Page1.cs
    using apiStatus = System.Int16;

    #Page2.cs
    using pooStatus = System.Int16;//inconsistency

    #Page3.cs
    using apiStatus = System.Int16;
    '
    #Page4.cs
    MyMethod()
    {
      apiStatus myStatus = 0;//ERROR, no using statement on this page...code-once use-many
    }
    BrianMackey.NET
    Monday, December 21, 2009 6:04 PM

All replies

  • No.
    The .NET does not allow you define/redefine your own Value Types using the primitive types already defined within the Framework, such as Int16.  This was a carefully made decision to avoid the confusions that can arise when developers can redefine pre-existing Value Type definitions.  This has nothing to with C#, and more to do with the .NET Framework Class Library. 

    Although, there is no C# equivalent to "typedef" as found in C, you can create your own Value Type definitions by declaring Structures.

    Hope this helps.

    Rudy  -8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, December 21, 2009 2:11 PM
    Moderator
  • i know that we use struct for our own value type definitions and we use class for our own reference type definitions..i have an unmanaged Dll which is written in C++..the Dll has value type definitions like

    typedef struct
    {
       unsigned char TagType;
       unsigned char AntNum;
       unsigned char Ids[12];
    }TagIds;

    typedef short apiStatus;

    and i want to use these value types in VS2005 c#.. How can i do this???

    Monday, December 21, 2009 2:51 PM
  • You can use "using apiStatus = System.Int16"

    Unless you defined your own System namespace, this will create an alias to short.
    Monday, December 21, 2009 3:06 PM
  • You can use "using apiStatus = System.Int16"

    Unless you defined your own System namespace, this will create an alias to short.

    does it mean i can use apiStatus instead of short in C#..? for example

    using apiStatus = System.Int16;

    ..
    ..

     

    //apiStatus __declspec(dllexport) __stdcallDrfGetFirmwareVersion (HANDLE hCom, unsigned char *major,unsigned char *minor, unsigned char ReaderAddr);

    public static extern apiStatus firmVersAl(IntPtr hCom, [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] major, [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] minor, [MarshalAs(UnmanagedType.LPArray)] byte[] ReaderAddr);
    Monday, December 21, 2009 3:23 PM
  • You can use "using apiStatus = System.Int16"

    Unless you defined your own System namespace, this will create an alias to short.


    Wow!
    I either forgot or didn't know that you could use a type name like that.
    I had thought that it always had to be a namespace.  Like Forms below.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using apiStatus = System.Int16;
    using MyForms = System.Windows.Forms;

    Thanks, Louis.  Rudy.

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, December 21, 2009 3:29 PM
    Moderator
  • Yes, and if you put your mouse over an apiStatus variable in VisualStudio, you will see it defined as "short".
    Monday, December 21, 2009 3:34 PM
  • While that appears to be a pretty cool replacement for typedef, this article brings out some interesting and important differences.  Mainly, the need to define the alias in every page and the lack of a single header definitions file.

    Because of that limitation this idea can result in inconsistencies and defeats the code-once use-many rule.  In other words, be cautious when "using" thing's it wasn't designed for :D.

    E.G. 

    #Page1.cs
    using apiStatus = System.Int16;

    #Page2.cs
    using pooStatus = System.Int16;//inconsistency

    #Page3.cs
    using apiStatus = System.Int16;
    '
    #Page4.cs
    MyMethod()
    {
      apiStatus myStatus = 0;//ERROR, no using statement on this page...code-once use-many
    }
    BrianMackey.NET
    Monday, December 21, 2009 6:04 PM
  • Brian,
    So maybe a struct is better to use.
    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, December 21, 2009 7:13 PM
    Moderator
  • Rudy, Do you mean something like this?
            
        static void Main()
        {
              Int16 myInt = 10;
             faketypedef avar = myInt;           
        }
        public struct faketypedef
        {
            public Int16 babyInt;
            public static implicit operator faketypedef(Int16 anint)
            {
                  faketypedef avar = new faketypedef();
                  avar.babyInt = anint;
                  return avar;
            }
        }
    

    BrianMackey.NET
    Monday, December 21, 2009 8:00 PM
  • Not even that complicated.
    A struct is a Value Type


        public struct ApiStatus
        {
            public short Status;
        }



    Naming it var might not be good.  It is identical to the keyword.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, December 21, 2009 8:14 PM
    Moderator