Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Extract Data from PDF's Pin
OriginalGriff1-Aug-23 11:15
mveOriginalGriff1-Aug-23 11:15 
GeneralRe: Extract Data from PDF's Pin
Gerry Schmitz1-Aug-23 14:27
mveGerry Schmitz1-Aug-23 14:27 
GeneralRe: Extract Data from PDF's Pin
Pete O'Hanlon1-Aug-23 19:46
mvePete O'Hanlon1-Aug-23 19:46 
GeneralRe: Extract Data from PDF's Pin
OriginalGriff1-Aug-23 20:51
mveOriginalGriff1-Aug-23 20:51 
GeneralRe: Extract Data from PDF's Pin
Pete O'Hanlon1-Aug-23 23:02
mvePete O'Hanlon1-Aug-23 23:02 
AnswerRe: Extract Data from PDF's Pin
Randor 1-Aug-23 16:38
professional Randor 1-Aug-23 16:38 
AnswerRe: Extract Data from PDF's Pin
jschell11-Aug-23 10:45
jschell11-Aug-23 10:45 
QuestionShould we ever create enums? (C#, maybe valid for C++) Pin
Paulo Zemek31-Jul-23 11:32
mvaPaulo Zemek31-Jul-23 11:32 
The title os this message might look like a joke, but it is actually very serious.

The other day I wanted to implement my own BigInteger class, and I was using bytes as the backing fields. Yet, I would only use values from 0 to 9 for each digit.
Bing AI suggested me to create an enum with values ranging from D0 to D9 (I think their actual values are obvious).
Yet, using an enum like that doesn't forbid users from doing things like (DecimalDigit)56 and pass 56 to an enum that was only supposed to support values ranging from 0 to 9.
Of course I can validate the values at run-time... but the entire purpose of using an enum was to avoid mistakes like that.

So, my solution was to create a class (in fact, a struct, but a class serves the same purpose) that has a private constructor, and has public static readonly fields ranging from D0 to D9. This way, users outside of the class, except if they really want to mess up (like using unsafe reflection) cannot pass values that aren't in the 0-9 range.

This also reminded me of a job where we had one enum with like 20 values... and then, many, many, many switches to get the many different traits of those enums.
Wouldn't it be better to just have classes, with all the traits, and use the classes?
Aside from the use of the enum in switch statements, they work the same in most cases, work even easier in cases where we usually had to use helper methods... and if a new trait is added, we have a single place (where the enum values are declared) to fix... with no chance of "forgetting" a case in a switch somewhere else.

What do you guys think?

Example:
C#
public struct DecimalDigit
{
  public static readonly DecimalDigit D0 = new(0);
  public static readonly DecimalDigit D1 = new(1);
  public static readonly DecimalDigit D2 = new(2);
  public static readonly DecimalDigit D3 = new(3);
  public static readonly DecimalDigit D4 = new(4);
  public static readonly DecimalDigit D5 = new(5);
  public static readonly DecimalDigit D6 = new(6);
  public static readonly DecimalDigit D7 = new(7);
  public static readonly DecimalDigit D8 = new(8);
  public static readonly DecimalDigit D9 = new(9);

  private DecimalDigit(byte value)
  {
    _value = value;
  }

  private readonly byte _value;
  public byte ByteValue
  {
    get => _value;
  }
}

// vs

public enum DecimalDigit:
  byte
{
  D0,
  D1,
  D2,
  D3,
  D4,
  D5,
  D6,
  D7,
  D8,
  D9
}

Notice that although the enum version is smaller, if we need to add names for the values, in the class we just add a property, for the real enum, we create a helper method.
If we need to convert them to numbers, add an emoji or whatever, in the first version it is just a matter of adapting the class, while in the second it is a matter of creating more (and somewhat unrelated) methods.

Edit: I had some questions about why create a new decimal class. There is not a real need to create one. I just wanted to do it as an exercise. I can tell that .NET implemented BigInteger is way faster than my class. Yet, just by writing the UnsignedDecimalInteger I saw opportunities to write Quadbits (effectively, half of a hexadecimal value... or just 4 bits), so in one byte I can store 2 Quadbits. I also saw opportunities for caching of the internal buffers I use... and I am just "relearning" how to do math the "old way" using decimal values. I will, at some point, improve it to use 32 or 64 bits at once.
Also, one of the next steps, be it with BigInteger or my UnsigedDecimalInteger, is to create a BigDecimal or similar class. In fact, having a value alone (without caring about operations), I just need to have a value telling where the dot separating the integer part and the fractional part. Or, I can literally have two BigInteger (or similar), one for the left side, and one for the right side, of the decimal.

modified 1-Aug-23 18:09pm.

AnswerRe: Should we ever create enums? (C#, maybe valid for C++) Pin
Gerry Schmitz31-Jul-23 16:50
mveGerry Schmitz31-Jul-23 16:50 
SuggestionRe: Should we ever create enums? (C#, maybe valid for C++) Pin
Richard Deeming31-Jul-23 22:04
mveRichard Deeming31-Jul-23 22:04 
GeneralRe: Should we ever create enums? (C#, maybe valid for C++) Pin
Paulo Zemek1-Aug-23 1:25
mvaPaulo Zemek1-Aug-23 1:25 
GeneralRe: Should we ever create enums? (C#, maybe valid for C++) Pin
Paulo Zemek1-Aug-23 1:46
mvaPaulo Zemek1-Aug-23 1:46 
GeneralRe: Should we ever create enums? (C#, maybe valid for C++) Pin
Richard Deeming1-Aug-23 1:50
mveRichard Deeming1-Aug-23 1:50 
GeneralRe: Should we ever create enums? (C#, maybe valid for C++) Pin
Paulo Zemek1-Aug-23 11:35
mvaPaulo Zemek1-Aug-23 11:35 
AnswerRe: Should we ever create enums? (C#, maybe valid for C++) Pin
BillWoodruff1-Aug-23 2:15
professionalBillWoodruff1-Aug-23 2:15 
AnswerRe: Should we ever create enums? (C#, maybe valid for C++) Pin
trønderen1-Aug-23 12:37
trønderen1-Aug-23 12:37 
GeneralRe: Should we ever create enums? (C#, maybe valid for C++) Pin
Paulo Zemek1-Aug-23 15:14
mvaPaulo Zemek1-Aug-23 15:14 
GeneralRe: Should we ever create enums? (C#, maybe valid for C++) Pin
harold aptroot2-Aug-23 6:37
harold aptroot2-Aug-23 6:37 
QuestionNew Kind of Application for me (centralized/cloud). Looking for learning resources. Pin
pr1mem0ver29-Jul-23 20:23
pr1mem0ver29-Jul-23 20:23 
AnswerRe: New Kind of Application for me (centralized/cloud). Looking for learning resources. Pin
Pete O'Hanlon29-Jul-23 23:28
mvePete O'Hanlon29-Jul-23 23:28 
GeneralRe: New Kind of Application for me (centralized/cloud). Looking for learning resources. Pin
pr1mem0ver30-Jul-23 10:44
pr1mem0ver30-Jul-23 10:44 
GeneralRe: New Kind of Application for me (centralized/cloud). Looking for learning resources. Pin
Pete O'Hanlon30-Jul-23 20:23
mvePete O'Hanlon30-Jul-23 20:23 
GeneralRe: New Kind of Application for me (centralized/cloud). Looking for learning resources. Pin
BillWoodruff31-Jul-23 3:26
professionalBillWoodruff31-Jul-23 3:26 
AnswerRe: New Kind of Application for me (centralized/cloud). Looking for learning resources. Pin
BillWoodruff30-Jul-23 1:22
professionalBillWoodruff30-Jul-23 1:22 
GeneralRe: New Kind of Application for me (centralized/cloud). Looking for learning resources. Pin
OriginalGriff30-Jul-23 2:35
mveOriginalGriff30-Jul-23 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.