Re: Delimited String to Array



AMDRIT <amdrit@xxxxxxxxxxx> wrote:
When talking about human readability, that would be why I would be as
complete as possible. Obviously, if the casting was not required, leave it
out, I just didn't know better.

The casting is not required, and neither is the parsing of the string
to a character.

So, to make explicit what the compiler does implicitly:

string[] mystring = "1,2,3,4,5".Split(new char[]{','});

Personally I'd still rather see

string[] mystring = "1,2,3,4,5".Split(',');

because it's easier to see what the *aim* is that way, even though it's
not as clear that an extra char array is being created.

in the case of a single if statement, I find the shorthand cumbersome and
difficult to read. Any thoughts?

I get lost when I see
[C#}
if (foo==bar)
//do something

Where as this is highly readble to me
{C#]
if (foo==bar)
{
//do something
}

I wouldn't say I get lost with the first way, but it's more error-
prone. I always include the braces.

Conversly, this seem readable to me
[VB]
if foo = bar then 'Do something


My biggest hang up with c sharp is that I am an old vb developer and the
c-like syntanx can get rough to read. I attempt to write complete code when
I make use of the language.

Another hangup I have is:

string[] mystring =
"1,2,3,4,5".split(",");

Maybe it is just becuse it is missing the "_". Only in vb we wouldn't be
able to use "_" there.

Well, it depends on the situation. I don't break lines for the sake of
it, but I do break them when they're getting too long.

Not to mention the use of variables with case differences.

private object apple = null;

object Apple()
{
get{return apple;}
set{apple = value;}
}

That's using a variable with a different case to a property, not two
variables with case differences.

I do like the quick and dirty property construct, I don't like that it is
not easily described as a property, that the understood value is the
underlying setter value.

The fact that it's capitalised should indicate that it's either a
readonly variable or a property.

All of these things make the langauge not easily human readable. Perhaps
programmer readable, but for one that has experience with c-like languages.

In the above case, it's more that one has experience with the .NET
conventions.

Don't get me wrong, I like c-sharp well enough, I just wish that the VB
constructs and languages were closer to the c-like cousins and then tasking
between the two wouldn't be so bad.

After all, in vb to convert the string to the array:

Dim myStrings() as String = "1,2,3,4,5".Split(",")

that is how I would do it there, knowing that the compiler will not have to
worry about any conversions.

No, the compiler *is* having to do conversions - indeed, the above
won't even compile when you've got option strict on. That's creating a
new char array, then loading it with the first character of a string.

So while this is preferred

string[] myString = "1,2,3,4,5".split(",");

No, that doesn't work. It has to be a *char*, but the char is
automatically converted to an array because the parameter is declared
with "params".

Where is it made clear to use the type casting and not to use it?

Well, "1,2,3,4,5" is always a string - it's a string literal - so
there's no need to cast, and doing so really doesn't help the reader at
all.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: on error resume next in c#
    ... > DirectCast is a way of casting which essentially bypasses preliminary type ... DirectCast are identical in the circumstances DirectCast is used, ... Dim Y As String = DirectCast ... C# compiler issues a stloc for the final assignment. ...
    (microsoft.public.dotnet.framework)
  • Re: on error resume next in c#
    ... >> DirectCast is a way of casting which essentially bypasses preliminary type ... > Dim Y As String = DirectCast ... > difference I'm seeing currently is that the VB compiler issues a pop and the ...
    (microsoft.public.dotnet.framework)
  • Re: I suppose the idea of classes and inheritance is
    ... my compiler is just happy with it. ... string which makes it part of the string. ... Well, since we're not talking about char array functions, what those functions do is irrelevant. ... The compiler only checks constant expressions at compile time. ...
    (alt.comp.lang.borland-delphi)
  • Re: How is strlen implemented?
    ... >>>that strlen can still return the number of characters of a char array. ... >> Your compiler is either not standard compilant, ... Of course, if the string were zero length, then.... ... CLC FAQ ...
    (comp.lang.c)
  • Re: casting an object to a specific type
    ... and a string identifying their type. ... The compiler doesn't like the casting that I do. ... Decimal num1 = Decimal.Parse; ...
    (microsoft.public.dotnet.languages.csharp)

Loading