473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

formatting strategies

Requirements for a field that represents an EmployeeNumber:

· The field is always 6 characters long, and is stored as a 'string'
in the db

o All characters must be digits

§ Regex pattern?

o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').

§ String.Format({ "0:D6", empNumber) ?

o The non-padded number should be easily extractable

§ empNbrString.Su bstring(_firstC harGreaterThanZ ero(empNbrStrin g))
(ie, '006728' == 6728, '000012' == 12)

Each requirement sounds relatively easy to do by brute force, but I'm sure
there is a more elegant approach some of you experts have used before.

Would you use a custom format provider? That may be overkill for the
employee, but I have similar formatting requirements for projectNumbers
which are complicated by the fact that different formats may apply to the
same project, depending on the organizational point of view.

Thanks for the Guidance
Aug 16 '08 #1
5 1233
Berryl Hesh <ef******@yahoo .comwrote:
Requirements for a field that represents an EmployeeNumber:

· The field is always 6 characters long, and is stored as a 'string'
in the db
Why is it stored as a string in the database if it's always an integer?
o All characters must be digits

§ Regex pattern?
That would certainly be pretty simple. Or just check the length and
that each character is in the range 0-9, which is easy by brute force.
It depends on how regex-savvy the maintainers will be.
o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').
I thought you said it *was* always 6 characters?
§ String.Format({ "0:D6", empNumber) ?
That will only work if empNumber is an integer rather than a string.
o The non-padded number should be easily extractable

§ empNbrString.Su bstring(_firstC harGreaterThanZ ero(empNbrStrin g))
(ie, '006728' == 6728, '000012' == 12)
Just use int.Parse.
Each requirement sounds relatively easy to do by brute force, but I'm sure
there is a more elegant approach some of you experts have used before.

Would you use a custom format provider? That may be overkill for the
employee, but I have similar formatting requirements for projectNumbers
which are complicated by the fact that different formats may apply to the
same project, depending on the organizational point of view.
I don't see any particular call for a custom format provider here, but
it depends on what you want to do. For data binding that *may* be
useful - I'm not terribly hot on data binding.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #2

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
Berryl Hesh <ef******@yahoo .comwrote:
Requirements for a field that represents an EmployeeNumber:

· The field is always 6 characters long, and is stored as a
'string'
in the db
>>Why is it stored as a string in the database if it's always an integer?
Good point. I was guessing that PSoft displayed it that way because it
stored it that way. Truthfully I have no idea why they even display it this
way, but they've been fairly successful so there must be something behind
that.
o All characters must be digits

§ Regex pattern?
>>>That would certainly be pretty simple. Or just check the length and
that each character is in the range 0-9, which is easy by brute force.
It depends on how regex-savvy the maintainers will be.
I'm not very savvy with that myself - could you show me what the Regex check
would look like??
o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').
>>I thought you said it *was* always 6 characters?
As a convenience to data input.
§ String.Format({ "0:D6", empNumber) ?
That will only work if empNumber is an integer rather than a string.
o The non-padded number should be easily extractable

§ empNbrString.Su bstring(_firstC harGreaterThanZ ero(empNbrStrin g))
(ie, '006728' == 6728, '000012' == 12)
>>>Just use int.Parse.
Yup - Thank you
Each requirement sounds relatively easy to do by brute force, but I'm sure
there is a more elegant approach some of you experts have used before.

Would you use a custom format provider? That may be overkill for the
employee, but I have similar formatting requirements for projectNumbers
which are complicated by the fact that different formats may apply to the
same project, depending on the organizational point of view.
>>I don't see any particular call for a custom format provider here, but
it depends on what you want to do. For data binding that *may* be
useful - I'm not terribly hot on data binding.
I was thinking about a different formatting scenario with project numbers
that may be formatted differently for different systems. The number is
always based on the year the project is assigned and the next available
number. A legacy accounting system, for example, requires that number to
look like 008-00132 (assuming 132 is the next number available for a project
assigned in 2008). Project managers just use 08-132 because its easier to
work with.

Thanks for the response.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #3
Berryl Hesh <ef******@yahoo .comwrote:
>Why is it stored as a string in the database if it's always an integer?
Good point. I was guessing that PSoft displayed it that way because it
stored it that way. Truthfully I have no idea why they even display it this
way, but they've been fairly successful so there must be something behind
that.
So that's a "given" - you don't have control over it?
>>That would certainly be pretty simple. Or just check the length and
that each character is in the range 0-9, which is easy by brute force.
It depends on how regex-savvy the maintainers will be.
I'm not very savvy with that myself - could you show me what the Regex check
would look like??
Here's some code which uses the pattern and tests it:

using System;
using System.Text.Reg ularExpressions ;

class EmployeeNumber
{
static readonly Regex Pattern = new Regex("^[0-9]{6}$",
RegexOptions.Co mpiled);

static void Main()
{
Test("123456");
Test("1234567") ;
Test("12345");
Test("12AA56");
Test("AA123456B B");
}
static void Test(string candidate)
{
Console.WriteLi ne("{0}: {1}",
candidate,
Pattern.IsMatch (candidate));
}
}

o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').
>I thought you said it *was* always 6 characters?
As a convenience to data input.
Okay, I see what you mean. In that case I'd parse it to an integer,
then pad it as you showed below.
>I don't see any particular call for a custom format provider here, but
it depends on what you want to do. For data binding that *may* be
useful - I'm not terribly hot on data binding.
I was thinking about a different formatting scenario with project numbers
that may be formatted differently for different systems. The number is
always based on the year the project is assigned and the next available
number. A legacy accounting system, for example, requires that number to
look like 008-00132 (assuming 132 is the next number available for a project
assigned in 2008). Project managers just use 08-132 because its easier to
work with.
I think you can still do that reasonably easily with just a custom
format string (but not a custom provider). Not sure how feasible the
parsing would be, but the formatting would be easy.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #4
Thanks for the info!

RE: data binding.
I've seen another experienced programmer that I respected also knock data
binding, awhile ago (I think it was Jeremy Miller), yet I see other
programmers, not to mention MS, continue to spend what seems like
considerable effort on it - do you have any links on the topic you
particularly like?

- Berryl Hesh
Aug 17 '08 #5
Berryl Hesh <ef******@yahoo .comwrote:
RE: data binding.
I've seen another experienced programmer that I respected also knock data
binding, awhile ago (I think it was Jeremy Miller), yet I see other
programmers, not to mention MS, continue to spend what seems like
considerable effort on it - do you have any links on the topic you
particularly like?
It's not that I particularly dislike it - although I have had problems
on the rare occasions when I've used it - but I just don't do much
client side coding, so I haven't had much experience with it.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 17 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1520
by: robin | last post by:
The Oblique Strategies were originally a set of one-hundred cards, each bearing a short phrase. They were devised by Brian Eno and Peter Schmidt as ways of working through creative problems. When a blockage occurs, draw a card, and see if it can direct you in a tangential way that helps solve the problem. I have created a Python...
13
1757
by: CoreyWhite | last post by:
When playing games, perhaps the most simple is tic-tac-toe. The game has two simple strategies, one is defensive and the other offensive. It is not hard at first to learn how to tie games when playing an opponent. And then the next stage in development comes after you learn how to beat an opponent. You really can only employ either...
24
2495
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do you ensure that the code will work correctly in the future? Short version:
0
1900
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall intrusion detection installation than monitoring alone. Monitoring can help you spot problems in your network, as well as identify performance problems,...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.