|
C#:
string[] codes = { "bad", "good" };
int rating = Array.IndexOf(codes, "bad") * 5;
Now do the VB one!
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 2 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
modified on Wednesday, April 15, 2009 11:05 AM
|
|
|
|
|
Um, the Array.IndexOf method looks a little off. Shouldn't it be Array.IndexOf(codes, "bad") ?
|
|
|
|
|
Yeah, now fixed, my brain needs rest now!
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 2 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
leppie wrote: Yeah, now fixed, my brain needs rest now!
...too much VB code for that one
|
|
|
|
|
I wouldn't know where to start, except for tossing out all those fancy brackets and semi-colons...
|
|
|
|
|
Leppie, I think you need to step away from the computer and get some rest.
|
|
|
|
|
Doesn't this actually gives a rating of 0 ("bad" is the first element, and 0 * anything = 0)?
Dybs
|
|
|
|
|
Looks like VB is scoring better,...
Dim codes() As String = New String(){"bad", "good"}
Dim rating As Integer = (Array.IndexOf(codes, "bad") + 1) * 5
It feels good to learn and achieve
|
|
|
|
|
Ah the expressive powers of VB and its native speakers
|
|
|
|
|
Oh, come on... Even VB can do this in one line plus exception handling...
Try
Return t.Select("linatipedi='N'").Length = 0
Catch
Return False
End Try
Man, if this is a 5, I'd really hate to see a 10...
Translation: Post one of the 10's! Heheh
|
|
|
|
|
Ian Shlasko wrote: Translation: Post one of the 10's! Heheh
A 6 or 7 (I'm not sure) is this code is from a function that should never had been called / written, so your better implementation is useless.
|
|
|
|
|
Ian Shlasko wrote: // *resists urge to type two ='s*
'Couldn't resist the temptation to use C++-style comments though.
|
|
|
|
|
Ack! I totally missed that...
Well, shows how much I've tried to get away from VB.
|
|
|
|
|
public static int GetAge(string birthDate)
{
DateTime birth;
try
{
birth = DateTime.Parse(birthDate);
}
catch (Exception ex)
{
throw new ApplicationException("Unable to read birth date.", ex);
}
if (DateTime.Today.Month < birth.Month)
{
return DateTime.Today.Year - birth.Year - 1;
}
else if (DateTime.Today.Month > birth.Month)
{
return DateTime.Today.Year - birth.Year;
}
else
{
if (DateTime.Today.Day < birth.Day)
{
return DateTime.Today.Year - birth.Year - 1;
}
else
{
return DateTime.Today.Year - birth.Year;
}
}
}
To be seen @ http://msdn.microsoft.com/en-us/library/bb126477.aspx[^]
UPDATE:
I was under the impression, Timespan had a TotalYears property. But now, looking at the logic, I can see why it does not have one.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 2 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
modified on Wednesday, April 15, 2009 10:40 AM
|
|
|
|
|
Yes. Ok. This is a bit easier on the eye...
public static int GetAge(string birthDate)
{
DateTime birth;
try
{
birth = DateTime.Parse(birthDate);
}
catch (Exception ex)
{
throw new ApplicationException("Unable to read birth date.", ex);
}
DateTime now = DateTime.Today;
if (now.DayOfYear < birth.DayOfYear)
{
return now.Year - birth.Year - 1;
}
return now.Year - birth.Year;
}
Oh, and it works around midnight, too...
|
|
|
|
|
Oh dear!
Another one bites the dust!
bad hint: TimeSpan
UPDATE:
Sorry
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 2 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
modified on Wednesday, April 15, 2009 10:58 AM
|
|
|
|
|
Part of the ugly code is due to the fact that TimeSpan does not have Months or Years properties;
[EDITED]
while it's MaxValue corresponds to more than 10 million days, without knowing the reference it can't decide on how many days fit in a month, and how leap years need handled.
[/EDITED]
modified on Wednesday, April 15, 2009 9:52 AM
|
|
|
|
|
Arggh, I could swear TimeSpan had those properties! Now I feel like the fool!
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 2 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Why not just use something like this?
public static int GetAge(string birthDate)
{
DateTime birth;
TimeSpan livingAge;
if(!DateTime.Parse(birthDate, out birth))
throw new InvalidCastException("Unable to parse birth date");
livingAge = DateTime.Now - birth;
return (livingAge + DateTime.MinValue).Years - 1;
}
|
|
|
|
|
Huh?
either you want an exception, so you use Parse; or you don't want one and use TryParse.
|
|
|
|
|
It's just my rather broken coding style. I never learned one formally. If you wanted, you could replace the throw statement with return -1
|
|
|
|
|
Cool, not what I had in mind, but definitely better than what I had in mind!
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 2 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Thanks. For what its worth, what did you have in mind?
|
|
|
|
|
Computafreak wrote: For what its worth, what did you have in mind?
For some reason I thought Timespan had a TotalYears property...
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 2 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
yet incorrect, since it assumes months and leap years evolve from day of birth in the same way as they do from DateTime.MinValue
The net result is that close to one's birthday the formula can be off by 1.
This one is much safer, although still not perfect:
DateTime.Today.Subtract(DateTime.Parse(birthday)).Days/365.242374
The perfect solution really uses ToDay.Years-DateTime.Parse(birthday).Years and adjusts by one according to the result of ToDay.DayOfYear < DateTime.Parse(birthday).DayOfYear
|
|
|
|