|
Well, he did mention VB. Which teaches everyone bad habits.
|
|
|
|
|
Since when is increasing programmer/developer (Whatever the term hipsters are using these days...) "teaching bad habits" ?
|
|
|
|
|
For one, VB teaches its users to take shortcuts.
Like leaving words out of your sentences, and then taking for granted that others will understand what you meant just by context.
|
|
|
|
|
Well it seems to me that documenting your code resolves that issue. AND it is easier and quicker to do, and still more productive.
Bad programmers will always "take shortcuts". Do not blame that on VB, or any other language, for that matter.
The language, any language, doesn't "teach" anything, to a programmer. If you shoot yourself in the foot with a handgun (after completing the required safety training), is it the gun's fault?
VB's problem is that it is not "Hip" anymore. Go listen to Billy Joel's "Still Rock and Roll". It is universally applicable.
What's the matter with the clothes I'm wearing?
"Can't you tell that your tie's too wide?"
Maybe I should buy some old tab collars?
"Welcome back to the age of jive.
|
|
|
|
|
Here you go:
public static string Left(this string theString, int length)
{
int sz = length > theString.Length + 1 ? theString.Length:length;
return theString.Substring(0, sz);
}
|
|
|
|
|
Don't give me workarounds when all I want to do is complain.
I know it's super simple to write one myself, but the point is I should not have to. Seems pretty basic.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Why not just ref the VB assembly and... oh, I can't keep it up.
|
|
|
|
|
|
RyanDev wrote: Don't give me workarounds when all I want to do is complain. i confess that that is something that grandly pisses me off.
RyanDev wrote: I know it's super simple to write one myself, but the point is I should not have to. It's that it was taken away because some moron decided for you that you didn't need it; that's what gets the teeth grinding.
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
Mark_Wallace wrote: It's that it was taken away Not sure C# ever had it.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Um...[^]
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
Um what? Microsoft.VisualBasic. And it's still there.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
On the VB tab, it gives the VB syntax:
Public Shared Function Left (
str As String,
Length As Integer
) As String
On the C# tab, it gives the C# syntax:
public static string Left(
string str,
int Length
)
I know that computery things seem complicated, Granny, but you ought to at least try to use them.
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
You're still missing what namespace the function is in.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Don't bother me with petty details when I'm in panic mode!
Looks like I've always used Substring, anyway.
Strange, that. It feels like I've used Left much more recently than I've used VB.
{Insert Sid James comment here]
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
|
But it was implemented. I'm now recompiling everything I've ever written in C#, to insert workarounds.
It's a very happy job, as I'm sure you can imagine.
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
No need to call Substring if the length is greater than (or equal to) the string's length.
And you'll get a NullReferenceException if the string is null , whereas VB would return an empty string instead. Seems like an odd choice, but when you're pandering to these VB devs...
public static string Left(this string theString, int length)
{
if (length < 0) throw new ArgumentOutOfRangeException(nameof(length));
if (theString == null || length == 0) return string.Empty;
if (theString.Length <= length) return theString;
return theString.Substring(0, length);
}
Now, let's hope he doesn't ask for a C# version of this VB6 abomination:
Dim s As String = "He11o"
Mid$(s, 3, 2) = "ll"
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Don't forget the null check! And a small tweak because I like doing it this way :P
It's called Truncate in my library, but if you're from VB, I guess Left is ok... Or you could just reference the Microsoft.VisualBasic assembly!
public static string Left(this string target, int length)
{
if(string.IsNullOrEmpty(target)
return target;
return target.SubString(0, Math.Min(target.Length, length));
}
|
|
|
|
|
RyanDev wrote: I miss VB. ..a special place in Hell.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Quote: I miss VB.
Well someone has to, I suppose...
|
|
|
|
|
Oh my, you are the quick to hate type, are you not?
One tiny method missing and boom I hate you MS! That escalated rather quickly!
At any rate I give you here, free of charge, a solution to use for your own coding pleasure!
No, no, no, there is no need to thank me!
public static class StringExtensions {
public static string Left(this string s, int n) {
if (n >= s.Length)
return "";
return s.Substring(s.Length - n);
}
}
|
|
|
|
|
Looks like a 'Right' to me, rather than a 'Left'
|
|
|
|
|
Ahem.. you know what?
You could even be right!
|
|
|
|
|
Excuse me but: Several GB worth of Framework, poorly documented, with an inextricable Hell of dependencies and Assembly idiosyncrasies and seemingly duplicated functionality which should live in the same ecosystem but are as incompatible as coffe and salt... and I have to write my farking own version of a function that existed 20 years ago to do the easiest and stupidest thing on Earth? So much for The Framework™.
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
|
|
|
|