2

I would like to define new 'simple' types like this (in delphi):

type
  TString2        = string[2];
  TString10       = string[10];
  TYesNo          = (isNull=-1, isNo=0,   isYes=1);
  TBit2           = 0..3;

And then, use that inside my class-fields, like this (in delphi again):

TCMDchild = class(TCMDParent)
strict protected
    fSgMrMs:        TString2;
    fSgIsMale:      TYesNo;
    fSgValue1:      TBit2;
    ......

¿ Are there any way to get the same easy "simple type construction" in C# (VS2010) ?

Thanks for your comments.

ferpega
  • 3,182
  • 7
  • 45
  • 65
  • possible duplicate of [Equivalent of typedef in C#](http://stackoverflow.com/questions/161477/equivalent-of-typedef-in-c) – ChrisWue Jul 03 '11 at 09:09
  • 2
    @ChrisWue That's no duplicate. It's the same answer, but to a different question. :) No Delphi use will think of the term 'TypeDef' when looking for an answer to this question. – GolezTrol Jul 03 '11 at 09:13
  • Similar to [This Question](http://stackoverflow.com/questions/3214177/c-type-alias-custom-type) – James Jul 04 '11 at 09:36
  • Yes you can, have look at this answer : http://stackoverflow.com/a/9258058/970420 – Spongebob Comrade Feb 27 '13 at 05:26

3 Answers3

0

Yes you can do that

You can use using keyword to do something like delphi Type or typedef in C and C++.

More information can be found here :

https://stackoverflow.com/a/9258058/970420

Community
  • 1
  • 1
Spongebob Comrade
  • 1,495
  • 2
  • 17
  • 32
  • How do you define a `string[2]`? – Guffa Feb 27 '13 at 11:23
  • @Guffa Instead of string[2] you can use String. Open arrays can cover fixed size arrays so I think it covers the case that you have mentioned. BTW, Instead of thinking about limitations(that I think they can be evaded) think about new features that **using** keyword opens for us. Good Luck. – Spongebob Comrade Feb 28 '13 at 12:37
  • If you just use the type `string`, you can't limit its length, so `using` doesn't cut it. – Guffa Feb 28 '13 at 15:39
  • @Guffa Firstly I should say that `string[2]` in **Delphi** means that we have a string with two characters limit but in **CSharp** it means that we have an array of two strings. And also consider that we can not implement those Delphi way limited strings in CSharp : http://stackoverflow.com/questions/3825979/c-limit-the-length-of-a-string .So when converting a code from **Delphi** to **CSharp** there is no way to implement limited strings by simple declaration so your mentioned case doesn't relate to using `using` or not using `using`! – Spongebob Comrade Feb 28 '13 at 17:12
  • 1
    Yes, I know what a `String[2]` does in Delphi. – Guffa Feb 28 '13 at 17:19
0

No, there aren't any type aliases like that in C#. It was not included, because most of the time it has been used to hide away what the code does instead of making the code clearer.

Besides, you don't specify the size of a string in C#, and there are no range limited numbers. You can use properties that check the values when you set them.

For the YesNo type you can use an enum:

public enum YesNo {
  No = 0,
  Yes = 1,
  Null = -1
}

class CommandChild : CommandParent {

  private string _fSgMrMs;
  private string _fSgValue1;

  public string fSgMrMs {
    get { return _fSgMrMs; }
    set {
      if (value.Length > 2) {
        throw new ArgumentException("The length of fSgMrMs can not be more than 2.");
      }
      _fSgMrMs = value;
    }
  }

  public YesNo fSgIsMale { get; set; }

  public int fSgValue1 {
    get { return _fSgValue1; }
    set {
      if (value < 0 || value > 3) {
        throw new ArgumentException("The value of fSgValue1 hase to be between 0 and 3.");
      }
      _fSgValue1 = value;
    }
  }

}

Note: You should try to use more descriptive names than things like "fSgMrMs".

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks @Guffa the fSgMrMs is actually only an example. It equals to a Mister/Mrs abreviation... :-) But as I said it is only an example, not the real field. – ferpega Jul 03 '11 at 09:20
  • 1
    You should also not use a boolean for gender, even a nullable boolean. It's too easy to forget whether null means "is unknown" "refused to state" or "neither androgenous nor intersex", and you have three major genders that you can't encode (presumably yes means M2F, no means F2M and you want to exclude M,F and H) –  Jul 03 '11 at 23:54
0

For the TYesNo you can use an enum:

public enum TYesNo
{
    IsNull = -1,
    No = 0,
    Yes = 1
}

For the others you could use properties and check the lenght in the setter:

public class TCmdChild : TCmdParent
{
    public TYesNo FSgIsMale { get; set; }

    protected string fSgMrMs;
    public string FSgMrMs
    {
        get { return fSgMrMs; }
        set
        {
            if(value.Length > 2)
                throw new OutOfRangeException("Value.Length needs to be <= 2");
            fSgMrMs = value;
        }
    }
}
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • Thanks for your answer #chrfin it is the same but it is a few newest than the @guffa answer. Regards. – ferpega Jul 03 '11 at 09:47