Hmm, What happened to Left ?

  • Thread starter Thread starter Jm
  • Start date Start date
J

Jm

Hi all

I feel stupid for asking this, but i just went to use the left() function
from vb6 only to find it doesnt do what it used to under .NET. Im assuming
theres something else now im meant to do when i want to say for example

Dim Str as string
Str = "1234"
msgbox left(str,2)

Thanks
 
Microsoft.VisualBasic.Left()






--
Met vriendelijke groet
Kind regards,

Michel Posseth
Software Developer
Microsoft Certified Professional

Company : Nohau Systems B.V.
Division : Systems Development
 
Jm said:
Hi all

I feel stupid for asking this, but i just went to use the left() function
from vb6 only to find it doesnt do what it used to under .NET. Im assuming
theres something else now im meant to do when i want to say for example

Dim Str as string
Str = "1234"
msgbox left(str,2)

Thanks

In fact you can still use Left by invoking explicitly

Microsoft.VisualBasic.Left(string, length)

but this does seem a bit unneccesary now we have Substring.

Regards

--Jim.
 
Hi Cor

Thanks for the reply, i feel stupid saying it but i wasnt aware that
namespace existed. Sahil pointed out the new method which i used anyhow.
Thanks for the reply

Thanks
 
Hi Jim

Thanks for the reply, i feel stupid saying it but i wasnt aware that
namespace existed. Sahil pointed out the new method which i used anyhow.
Thanks for the reply

Thanks
 
JM,

That is not a new method, that is a very old method. It comes from the C
type languages.

The C type methods use a zero indexer and the VB type methods the First as
indexer.
I think it is good to stay to one kind because otherwise you will soon be
confused.

I never use methods which have a First as starting indexer although I think
it is better, the only reason for that is that I am used to that zero
indexer. (Therefore I use substring).

Cor
 
Sahil Malik said:
String.SubString <--- That's what u got now !!

Why?! I am not a fan of weak semantics...

'Left' is still available in the 'Microsoft.VisualBasic.Strings' module and
is IMO the best choice.

To avoid a name clash with a form's 'Left' property, use 'Strings.Left' in
Windows Forms code.
 
Jim said:
In fact you can still use Left by invoking explicitly

Microsoft.VisualBasic.Left(string, length)

but this does seem a bit unneccesary now we have Substring.

Well, why not use pure assembler? 'Substring' is unnecessary too...
 
Herfried,

The why is in my messages in this thread

As well as the why not by the way

:-)

Cor
 
Hefried,
Why?! I am not a fan of weak semantics...
?

How is String.SubString "weak semantics"? To me it is very OO, you have a
string object & you want to get part of that string object.

See my other post on using Strings.Left.

Jay
 
Jm,
In addition to all the other comments.

A Form has a Left property so it hides the VB.Left function, what I normally
do is use an Import Alias in my form source files to allow use of the
function.

Imports VB = Microsoft.VisualBasic

Public Class MainForm

...

Public Sub Test
Dim Str as string
Str = "1234"
msgbox VB.left(str,2)

End Class

A word of caution on String.SubString (System.String methods) & Strings.Left
(Microsoft.VisualBasic.Strings functions): System.String methods uses base 0
indexes while Microsoft.VisualBasic.Strings uses base 1 indexes. I would
recommend you stay with either the System.String methods or the
Microsoft.VisualBasic.Strings functions to avoid possible confusion & subtle
problems later...

Hope this helps
Jay
 
Jay,

Jay B. Harlow said:
?

How is String.SubString "weak semantics"? To me it is very OO, you have a
string object & you want to get part of that string object.

'Left', 'Mid' and 'Right' make it more obvious which part of the string is
returned, and thus make code more readable. 'Substring' can be used to to
the same, but the user will have to analyze the values passed to it in order
to be able to determine if the left, right, or an arbitrary part (middle) of
the string is returned. In other words: The name 'Substring' only tells
the reader that a part of the string is returned, names like 'Left' and
'Right' provide more/additional information.

Notice that I was not discussing that 'Substring' is more OO than 'Left',
'Right', etc. OO is a good tool, but in some cases (like working with
strings or dealing with numbers) it IMO doesn't add much benefit.

Would

\\\
Dim d As Double = ...
Dim e As Double = d.Cos() * 2
///

really have benefits over

\\\
Dim d As Double
Dim e As Double = Math.Cos(d) * 2
///

only because it's more OO?

SCNR
 
Herfried,
'Left', 'Mid' and 'Right' make it more obvious which part of the string is
returned, and thus make code more readable.
I agree it may be more obvious...

However! I'm just wondering about "weak semantics", to me saying it is "weak
semantics" is like insisting a glass is half full, when it is obvious it is
really half empty! Considering most of the time, it really doesn't matter if
I am getting the Left, Mid or Right of a string, I just want a part of the
string...

When you just want part of a string, any part of the string, SubString or
just Mid is sufficient. Left & Right can be implemented in terms of
SubString or Mid themselves...
the same, but the user will have to analyze the values passed to it in
order to be able to determine if the left, right, or an arbitrary part
(middle) of the string is returned.
If one uses SubString enough is this really an issue? In other words, they
soon pick up on:
- x.SubString(0, n) is Left
- x.SubString(n) is Right (not the same as VB.Right!)
- x.SubString(m,n) is Middle

Which if you believe Overloading is "evil", then yes I agree it may be "weak
semantics". However! I see overloading as a very powerful OO tool (which
like any construct can be easily abused), so the above does not cause me any
real issues... Info on "Overloading vs. Object Technology"
http://www.inf.ethz.ch/personal/meyer/publications/joop/overloading.pdf

I do find VB.Left, VB.Right, & VB.Mid can be "better" in that they have more
tolerant argument checking, which also makes String.SubString "better" as it
insists on correct arguments!

Jay
 
Jay,

Jay B. Harlow said:
I agree it may be more obvious...

However! I'm just wondering about "weak semantics", to me saying it is
"weak semantics" is like insisting a glass is half full, when it is
obvious it is really half empty! Considering most of the time, it really
doesn't matter if I am getting the Left, Mid or Right of a string, I just
want a part of the string...

When learning string algorithms for university, I found those much clearer
and easier to understand which used 'Left' or 'Right' instead of picking out
a part of a string starting at a certain position and ending at a certain
position, or starting at a certain position and being of a certain length,
respectively. Imagine describing an algorithm that works on a string to
somebody via telephone. Would you say "Take the substring starting at
position 22 and ending at the string's end" or would you say "Take the
string's last three characters". I'd say the latter. Sure, taking the
start and end of a string /is/ taking /part of/ the string, but that's a
more general description of what is done.
When you just want part of a string, any part of the string, SubString or
just Mid is sufficient. Left & Right can be implemented in terms of
SubString or Mid themselves...

If I just want a part of the string that I don't have more information
about, I use 'Mid'. For the special cases, I prefer 'Right' and 'Left' for
the reasons described above.
If one uses SubString enough is this really an issue? In other words, they
soon pick up on:
- x.SubString(0, n) is Left
- x.SubString(n) is Right (not the same as VB.Right!)
- x.SubString(m,n) is Middle

You definitely /can/ get used to it and then be able to use it without any
problems.

BTW: It's interesting that the method's name is 'Substring', but most
people refer to it as 'SubString', even those who are using case-sensitive
programming languages ;-).
Which if you believe Overloading is "evil", then yes I agree it may be
"weak semantics". However! I see overloading as a very powerful OO tool
(which like any construct can be easily abused), so the above does not
cause me any real issues... Info on "Overloading vs. Object Technology"
http://www.inf.ethz.ch/personal/meyer/publications/joop/overloading.pdf

Thanks for the link, that's an interesting article. Well, I really liked
the sample of named constructors the author of the article you referenced
above gave... ;-). I believe in the power of overloading and I am confident
that it is a very powerful OO tool, but my concerns were not primarily
related to overloading in general.
I do find VB.Left, VB.Right, & VB.Mid can be "better" in that they have
more tolerant argument checking, which also makes String.SubString
"better" as it insists on correct arguments!

:-)
 
Herfried,
BTW: It's interesting that the method's name is 'Substring', but most
people refer to it as 'SubString', even those who are using case-sensitive
programming languages ;-).
I was using Substring (in this thread), but I "corrected" myself, as
SubString was used earlier in this thread...

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top