|
addMethod.Invoke(m_columnMappings(ordinalColumnPosition).ColumnProperty.GetValue(dataObject), {objectValue})
Taking data from excel named ranges into List<> properties using reflection
|
|
|
|
|
An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:
private string EncodeFlags(EventType _eventType)
{
if (_eventType == EventType.Alarm)
{
return "A";
}
if (_eventType == EventType.PreAlarm)
{
return "P";
}
if (_eventType == EventType.Malfunction)
{
return "M";
}
return "";
} Already this function alone is some kind of WTF.
But let's look at the definition of the input parameter which he translates:
[Flags]
public enum EventType
{
None = 0,
PreAlarm = 1,
Alarm = 2,
Malfunction = 4
} So he translates an enum with a Flags attribute to a string , ignoring the fact that they are flags, in order to store that value in a database eventually.
|
|
|
|
|
He has a bug in in code by not returning a useful defalut value. And its also missing in the enum.
If he is an expert in Solitaire I may know him?
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
KarstenK wrote: useful defalut What's that?
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
I suspect it happened the other way around. The database was already screwed -- storing a character flag (yuck) -- but the dev is smart enough to use an enum in the code instead.
However, I (not being an average bear) would add a DescriptionAttribute to each member of the enum:
[Flags]
public enum EventType
{
[Description("")]
None = 0,
[Description("P")]
PreAlarm = 1,
[Description("A")]
Alarm = 2,
[Description("M")]
Malfunction = 4
}
Then, to get the textual representation of the state, you get the Description from the enum -- and members can be added and removed to/from the enum without having to update the code. To take it a step further, I store the enum members and their textual representations in a Dictionary for easy look-up.
|
|
|
|
|
Surely it would be simpler to cheat?
public enum EventType : ushort
{
None = 0,
PreAlarm = 'P',
Alarm = 'A',
Malfunction = 'M',
}
var foo = (EventType)'P';
char bar = (char)foo;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes.
Many years ago, in C, I used a short to hold two characters for some text file parsing I needed to do.
modified 4-Jul-17 13:50pm.
|
|
|
|
|
PIEBALDconsult wrote: Then, to get the textual representation of the state
It's a flagged enum, so how would he represent the state Alarm | Malfunction ?
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson
Our heads are round so our thoughts can change direction - Francis Picabia
|
|
|
|
|
Well spotted.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
Bernhard Hiller wrote: store that value in a database eventually.
Did I miss important new trend in databases? When did they stopped supporting integers?
|
|
|
|
|
Looking at "QA" they also seem to have dropped support for dates.
|
|
|
|
|
It's worse than that: based on QA, they've also dropped support for parameterized queries!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It is even worst than that, based on the QA they have also dropped support for brains
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Bernhard Hiller wrote: with some 25+ years of experience in software engineering
var experience = EventType.None | EventType.Alarm | EventType.Malfunction;
Marc
Latest Article - Create a Dockerized Python Fiddle Web App
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
Experience is the ability to recognize a mistake when you repeat it.
|
|
|
|
|
It makes no sense:
1 - There are combinations not covered by the encoder. ie Alarm | Malfunction
2 - It only makes sense to encode it if whatever is using does not support integers (or maybe it's meant to have a meaningful representation on a digital display, still what if it's alarming and malfunctioning).
3 - Should have used enum's HasFlag method.
Now, an expert in C# and 25+ years in programming could only do this if he's hung over, lazy, dumb or having an idea so bright nobody can understand.
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson
Our heads are round so our thoughts can change direction - Francis Picabia
|
|
|
|
|
First, assume a legacy database schema...
|
|
|
|
|
Fabio Franco wrote: could only do this if he's pissed-off at legacy crap.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Bernhard Hiller wrote: So he translates an enum with a Flags attribute to a string , ignoring the fact that they are flags, in order to store that value in a database eventually.
I learned decades ago that its a fool errand to insult the previous developer(s), especially if one was not there participating in the decision making process. There are a multitude of factors and/or external forces that impact decisions like this and arrogantly criticizing these choices accomplishes little.
|
|
|
|
|
|
Especially knowing that I am also a previous developer to someone else.
|
|
|
|
|
thund3rstruck wrote: I learned decades ago that its a fool errand to insult the previous developer(s), especially if one was not there participating in the decision making process. There are a multitude of factors and/or external forces that impact decisions like this and arrogantly criticizing these choices accomplishes little. I agree with you.
I know the guy who wrote that code, and the circumstances: it's fresh code written this week.
Some time ago, I discussed with him his obsession of encoding everything in magical ints or one-letter-strings, sometimes also providing "endode"/"decode" functions for converting between the magical ints and the one-letter-strings and the other way round...
|
|
|
|
|
Ignore my previous post.
I used to work with someone like that. He no longer works here. Smart guy, very capable, but continuously made "mistakes" like that. His "code" has caused us lots of grief.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Bernhard Hiller wrote: ignoring the fact that they are flags It could have been changed to [Flags] after-the-fact and this code was just not updated.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
I was just browsing doing some rnd ... for a personal learning project.. the amount of JavaScript frameworks
google search is turning up is overwhelming...and the names they have.. ho my...hundreds of frameworks....
Caveat Emptor.
"Progress doesn't come from early risers – progress is made by lazy men looking for easier ways to do things." Lazarus Long
|
|
|
|