Use of Mid Statement in VB.Net

M

mayayana

As you probably know, it is impossible to change the content of a
String
In .NET you CAN NOT modify a string. If it appears a string is being
modified, a new string is being created.

Boy, this is turning into quite a party. :)

Several people have already pointed out what
you're saying. I said Mid in *VB*.
At the risk of beating a dead horse:

* There was a confusion because VB and VB.Net
both have a Mid statement, but they're entirely
different things under the hood.

* The Mid statement in VB is very fast and doesn't
create a new string.

* The Mid statement in VB.Net is apparently only
for compatibility and does create a new string.
Therefore it's not much good when speed is needed.

* The aim, with that in mind, is to find the
fastest possible string-editing function in .Net.
 
A

Armin Zingler

Mike said:
Don't tell lies Zingler in your attempt to score points.

Tell me, where is the lie?
Harry also said
that he knows the Mid statement is also available in VB.Net.

I know, as you should already know from my reply to David Anton and also
from what you've quoted above.
In fact you've
already admitted that yourself in your curt reply to David Anton.

Obviously you don't know the meaning of the word "admit". Making someone
aware of something that somebody else wrote has nothing to do with "admit".

Harry in
fact said, "The beauty of the old VB6 Mid statement (which is available in
VB.Net) is that . . ."

Why do you quote this again? We all know this already.
He then went on to say that he wants to deal with
several thousand records in a very fast time, although he did not specifying
how many times he would need to use the Mid statement on each record.
Correct.


So, it
would appear that either the OP meant to say, "which is /not/ available in
VB.Net", whereas in fact it is, or that he actually did know it was
available in VB.Net

Read the OP's posting again! He does know that it is available in VB.Net.

but (as indicated by his use of the phrase "very fast
time") he knew that the VB.Net version is painfully slow by comparison. The
OP has asked a question, Zingler.

And the answer to the question is the solution I've provided.
Why don't you just help him out

I did already, I did. If you have a better solution, feel free to post it.
But keep in mind what the OP needs: A quick solution in VB.Net.
Til now, I haven't seen any contribution from you to help the OP at all.
and leave
it at that, instead of trolling around telling lies.

Again, where is the lie? You're always shooting yourself in the foot
as long as you're unfoundedly accusing other people of whatever just
crosses your mind. That's why you are making a troll of yourself.
 
A

Armin Zingler

mayayana said:
Several people have already pointed out what
you're saying. I said Mid in *VB*.

Yes, VB, we know. The VB we talk about here in this group is VB.Net.
You must write VB6 or VB classic if you're referring to these
versions in this group.
 
M

Mike Williams

Tell me, where is the lie?

You said that he only mentioned that the Mid statement was already there in
VB6 and that he now needs a solution for VB.Net, whereas in fact he said
that he knew it was available in both VB6 and in VB.Net.
Read the OP's posting again! He does know
that it is available in VB.Net.

I don't need to read it again. I've already read it and I've already pointed
it out to you, because of your statement that he only mentioned that it was
there in VB6. However, it is always possible that a person has said
something which is actually quite different to what he meant to say (a typo
or whatever, with perhaps just a single word missing), so I thought I would
let you off the hook by mentioning that possibility.
Obviously you don't know the meaning of the word "admit".

Yes I do. You said that the OP had "only said it was available in VB6",
whereas in fact he said a lot of other things as well, including a statement
to the effect that he knew that it was also available in VB.Net. Perhaps you
don't know the meaning of the word "only"?

Your turn next, Zingler. Be a good troll and make it quick, though. I
haven't got all night :)

Mike
 
M

Mike Williams

Armin Zingler said:
Yes, VB, we know. The VB we talk about here in this group is VB.Net.
You must write VB6 or VB classic if you're referring to these
versions in this group.

Perhaps it might have been better if Micro$oft had not deliberately spun
their web of marketing lies in the first place. Then there would not have
been any confusion.

Mike
 
T

Tom Shelton

Hi All

If anyone has the time, I think feedback on the following may be of interest
to all:

I have to build a lot of bank files which are generally of the fixed length
field type eg aba format. The beauty of the old VB6 Mid statement (which is
available in VB.Net) is that it allows me to replace n chars at a specified
position in a string eg

'(air code)
Private Enum MyFields
mfRecordType = 1
mfTransactionType = 2
'etc
End Enum

Dim buffer as New String(120, " "c)
Dim pointer as Integer = 1

Mid(buffer, pointer, MyFields.mfRecordType) = "A"
pointer += MyFields.mfRecordType

Mid(buffer, pointer, MyFields.mfTransactionType ) = "AB"
pointer += mfTransactionType 'etc etc

There appears no native VB.Net equivalent to this method. The StringBuilder
Class does not have this method either. It should be noted that the Replace
method for either String or StringBuilder would not be appropriate for the
above.

The above algorithm that I use will actually write several thousands records
to file in very fast time (around or under 1 second)

As I have stated, this whole question is of an academic nature. Having said
that, is there any equivalent method (as used above) for the Mid Statement
in VS (any language)?

Thanks for your time.

To summerize, I put together a function myself and then included Armin's and
the VB.NET mid statement method:

Here is the output (release mode build, no debugger attached) - total time for
30000000 iterations each:

Tom's StringBuilder AppendField extension: 00:00:03.8856089
Armin's StringBuilder AppendWithPadding extension: 00:00:05.6895440
VB.NET mid statement: 00:00:14.5048549
Press any key to continue . . .

And here is the code:
Option Strict On
Option Explicit On

Imports System.Text
Imports System.Runtime.CompilerServices


Module Module1

Enum Fields
First = 1
Second = 3
Third = 1
End Enum

Const RecordLength As Integer = 5

Sub Main()
Dim sw As New Stopwatch()
Dim buffer As New StringBuilder(RecordLength, RecordLength)
sw.Start()
For i As Integer = 1 To 30000000
buffer.Length = 0
buffer.AppendField(Fields.First, "A")
buffer.AppendField(Fields.Second, "CD")
buffer.AppendField(Fields.Third, "EF")
Next
sw.Stop()
Console.WriteLine("Tom's StringBuilder AppendField extension: {0}", sw.Elapsed)

sw.Reset()

Dim sb As New System.Text.StringBuilder(RecordLength, RecordLength)
sw.Start()
For i As Integer = 1 To 30000000
sb.Length = 0
sb.AddWithPadding("A", Fields.First)
sb.AddWithPadding("CD", Fields.Second)
sb.AddWithPadding("E", Fields.Third) ' armins causes an error if this is longer then the max lenght
Next
sw.Stop()
Console.WriteLine("Armin's StringBuilder AppendWithPadding extension: {0}", sw.Elapsed)

sw.Reset()
sw.Start()
For i As Integer = 1 To 30000000
Dim ptr As Integer = 1
Dim sbuffer As New String(" "c, RecordLength)

Mid(sbuffer, ptr, Fields.First) = "A"
ptr += Fields.First

Mid(sbuffer, ptr, Fields.Second) = "CD"
ptr += Fields.Second

Mid(sbuffer, ptr, Fields.Third) = "EF"

Next
sw.Stop()
Console.WriteLine("VB.NET mid statement: {0}", sw.Elapsed)
End Sub

<Extension()> _
Sub AppendField(ByVal buffer As StringBuilder, ByVal maxLength As Integer, ByVal value As String)
For i As Integer = 0 To maxLength - 1
If i < value.Length Then
buffer.Append(value(i))
Else
buffer.Append(" "c)
End If
Next
End Sub

<Runtime.CompilerServices.Extension()> _
Sub AddWithPadding(ByVal sb As System.Text.StringBuilder, ByVal Text As String, ByVal TotalLength As Integer)
sb.Append(Text)
sb.Append(New String(" "c, TotalLength - Text.Length))
End Sub

End Module
 
T

Tom Shelton

Perhaps it might have been better if Micro$oft had not deliberately spun
their web of marketing lies in the first place. Then there would not have
been any confusion.

Or maybe it would be better if you just specified the version your speaking about.
 
M

Mike Williams

Or maybe it would be better if you just specified
the version your speaking about.

Version? They're not two versions of the same thing. They are two entirely
different products, one of which is for marketing purposes sprinkled with
some VB salt and pepper.

Mike
 
T

Tom Shelton

Version? They're not two versions of the same thing. They are two entirely
different products, one of which is for marketing purposes sprinkled with
some VB salt and pepper.

Regardless of your oppinion, right or wrong - since VB.NET has been on the
market for going on 8 years I don't imagine Microsoft has any intention of
changing the name....

So, for clarity of communication, I say again it would probably be better if
you just specified the version you are talking about.
 
A

Armin Zingler

Mike said:
Yes I do. You said that the OP had "only said it was available in VB6",
whereas in fact he said a lot of other things as well, including a statement
to the effect that he knew that it was also available in VB.Net.
Perhaps you
don't know the meaning of the word "only"?

I do know it, but the following two phrase have a completely different meaning:

"Harry only mentioned that the Mid statement was already there in VB6."
"Harry mentioned that the Mid statement was only there in VB6."

I wrote the forme one. You're trying to impute the latter one to me.
Well, I did not write it, as everybody can read. So, who is the liar?

I'm not even sure if you see the difference between the two phrases.
The former means, it was the only time he mentioned VB6 in his post,
whereas the latter means that he is disavowing the existence of the
Mid statement in VB.Net.
 
A

Armin Zingler

Mike said:
Perhaps it might have been better if Micro$oft had not deliberately spun
their web of marketing lies in the first place.

Do you mean the statement that VB.Net is for beginners? I don't call it a lie
but I don't agree.
Then there would not have
been any confusion.

Only few people are confused. Those that ignore the name of this group,
for nobody's interested in any marketing statements while asking or answering
questions about the VB.Net language here.
 
H

Harry Strybos

Tom Shelton said:
To summerize, I put together a function myself and then included Armin's
and
the VB.NET mid statement method:

Here is the output (release mode build, no debugger attached) - total time
for
30000000 iterations each:

Tom's StringBuilder AppendField extension: 00:00:03.8856089
Armin's StringBuilder AppendWithPadding extension: 00:00:05.6895440
VB.NET mid statement: 00:00:14.5048549
Press any key to continue . . .

And here is the code:
Option Strict On
Option Explicit On

Imports System.Text
Imports System.Runtime.CompilerServices


Module Module1

Enum Fields
First = 1
Second = 3
Third = 1
End Enum

Const RecordLength As Integer = 5

Sub Main()
Dim sw As New Stopwatch()
Dim buffer As New StringBuilder(RecordLength, RecordLength)
sw.Start()
For i As Integer = 1 To 30000000
buffer.Length = 0
buffer.AppendField(Fields.First, "A")
buffer.AppendField(Fields.Second, "CD")
buffer.AppendField(Fields.Third, "EF")
Next
sw.Stop()
Console.WriteLine("Tom's StringBuilder AppendField extension: {0}",
sw.Elapsed)

sw.Reset()

Dim sb As New System.Text.StringBuilder(RecordLength, RecordLength)
sw.Start()
For i As Integer = 1 To 30000000
sb.Length = 0
sb.AddWithPadding("A", Fields.First)
sb.AddWithPadding("CD", Fields.Second)
sb.AddWithPadding("E", Fields.Third) ' armins causes an error
if this is longer then the max lenght
Next
sw.Stop()
Console.WriteLine("Armin's StringBuilder AppendWithPadding
extension: {0}", sw.Elapsed)

sw.Reset()
sw.Start()
For i As Integer = 1 To 30000000
Dim ptr As Integer = 1
Dim sbuffer As New String(" "c, RecordLength)

Mid(sbuffer, ptr, Fields.First) = "A"
ptr += Fields.First

Mid(sbuffer, ptr, Fields.Second) = "CD"
ptr += Fields.Second

Mid(sbuffer, ptr, Fields.Third) = "EF"

Next
sw.Stop()
Console.WriteLine("VB.NET mid statement: {0}", sw.Elapsed)
End Sub

<Extension()> _
Sub AppendField(ByVal buffer As StringBuilder, ByVal maxLength As
Integer, ByVal value As String)
For i As Integer = 0 To maxLength - 1
If i < value.Length Then
buffer.Append(value(i))
Else
buffer.Append(" "c)
End If
Next
End Sub

<Runtime.CompilerServices.Extension()> _
Sub AddWithPadding(ByVal sb As System.Text.StringBuilder, ByVal Text As
String, ByVal TotalLength As Integer)
sb.Append(Text)
sb.Append(New String(" "c, TotalLength - Text.Length))
End Sub

End Module

Thanks Tom, very useful information. I am currently playing around with my
own StringBuffer class which uses a Byte array internally. Your test code
will prove highly valuable to me.
As I said at the beginning of my original post, my enquiry was academic.
Sure didn't mean to start a flame war. Sorry, guys, however, thank you all
for your input.
 
A

Armin Zingler

Mike said:
Version? They're not two versions of the same thing.

Well, if there are two versions, there must be differences.
So it's impossibly the "same" thing.

The problem is that you think that you are the one
who establishes the criteria that must be met in order
to make two things be "two versions of the same thing".

How do you measure the degree of similarity? How much
difference do you accept?

Isn't it the basic language keywords that make a language be
recognizable as such? As they still haven't changed a lot,
I have never hade any problem to recognize VB.Net code as VB
code even if a lot of improvements, enhancements and changes have
been made and everything is based on a new platform. Therefore
I will never understand your attitude.
 
M

Mike Williams

I do know it, but the following two phrase have a completely different
meaning:
"Harry only mentioned that the Mid statement was already there in VB6."
"Harry mentioned that the Mid statement was only there in VB6."
I wrote the forme one. You're trying to impute the latter one to me.

No, I am not trying to do that at all. You are mistaken again. I said that
you had said the former one. If you had said the latter (Harry mentioned
that the Mid statement was only there in VB6" then (despite the slightly
incorrect grammar) the word "only" would have referrred to the phrase "there
in VB6", carrying with it the clear meaning that Harry thought it was not
there in anything else. But I never accused you of saying that.

If you had said the former (which you did) then the word "only" refers to
what he mentioned, meaning that he said /nothing else/ other than the fact
that the Mid statement is there in VB6. But you were wrong to make that
statement. Harry did not /only/ say that. He said other things as well, one
of which was the fact that he knew that it was also available in VB.Net.

In either case, the statement you made does not agree with the facts of what
Harry said.

Simple. Even a troll such as yourself should be able to understand it.

Now be a good troll Zingler and reply as soon as you can . . . let's see how
many posts we can get into this thread. But don't bother replying straight
away because I won't be here again until tomorrow. Look forward to seeing
you then ;-)

Mike
 
A

Armin Zingler

Harry said:
Thanks Tom, very useful information. I am currently playing around with my
own StringBuffer class which uses a Byte array internally. Your test code
will prove highly valuable to me.
As I said at the beginning of my original post, my enquiry was academic.
Sure didn't mean to start a flame war. Sorry, guys, however, thank you all
for your input.

No need to apologize. You're welcome! Only very few people here have their,
let's say "special" opinion about VB.Net - which is acceptable - but
unfortunatelly that is often expressed in unfounded allegations, twisting
one's words or laying into someone the nasty way.

May I ask what you're trying with the Byte array? I have doubts because
it seems to imply (time consuming) character en/decoding.
 
T

Tom Shelton

Hi All

If anyone has the time, I think feedback on the following may be of interest
to all:

I have to build a lot of bank files which are generally of the fixed length
field type eg aba format. The beauty of the old VB6 Mid statement (which is
available in VB.Net) is that it allows me to replace n chars at a specified
position in a string eg

'(air code)
Private Enum MyFields
mfRecordType = 1
mfTransactionType = 2
'etc
End Enum

Dim buffer as New String(120, " "c)
Dim pointer as Integer = 1

Mid(buffer, pointer, MyFields.mfRecordType) = "A"
pointer += MyFields.mfRecordType

Mid(buffer, pointer, MyFields.mfTransactionType ) = "AB"
pointer += mfTransactionType 'etc etc

There appears no native VB.Net equivalent to this method. The StringBuilder
Class does not have this method either. It should be noted that the Replace
method for either String or StringBuilder would not be appropriate for the
above.

The above algorithm that I use will actually write several thousands records
to file in very fast time (around or under 1 second)

As I have stated, this whole question is of an academic nature. Having said
that, is there any equivalent method (as used above) for the Mid Statement
in VS (any language)?

Thanks for your time.

Another followup... I decided to compare to do the same thing in VB6 using
the native VB6 mid-statement. Here is the code:

''''' Form 1

Option Explicit


Private Enum Fields
First = 1
Second = 3
Third = 1
End Enum


Private Sub Command1_Click()
Dim t1 As Currency, t2 As Currency, freq As Currency, overhead As Currency
Call QueryPerformanceFrequency(freq)
Call QueryPerformanceCounter(t1)
Call QueryPerformanceCounter(t2)
overhead = t2 - t1

Dim str As String
Dim i As Long
Dim ptr As Long

Call QueryPerformanceCounter(t1)
For i = 0 To 30000000
ptr = 1
str = Space$(5)

Mid(str, ptr, Fields.First) = "A"
ptr = ptr + Fields.First

Mid(str, ptr, Fields.Second) = "BC"
ptr = ptr + Fields.Second

Mid(str, ptr, Fields.Third) = "CD"
Next i
Call QueryPerformanceCounter(t2)
MsgBox ((t2 - t1 - overhead) / freq) & "seconds"

End Sub

'''' Module 1
Declare Function QueryPerformanceCounter Lib "Kernel32" _
(X As Currency) As Boolean
Declare Function QueryPerformanceFrequency Lib "Kernel32" _
(X As Currency) As Boolean
Declare Function GetTickCount Lib "Kernel32" () As Long
Declare Function timeGetTime Lib "winmm.dll" () As Long

Compiled to native code, run on the same hardware with all the advanced
optimizations on:

6.2373452491867 seconds

So, what do we learn - the vb.net mid statement is about ~.5x the speed of the
VB6 mid statement. But, System.Text.StringBuilder is ~2x the speed of the VB6
mid statement.

I know a few techniques that can be used to do in place editing of a vb6
string that are much faster then mid - but, I'm not sure they would gain much
in speed over the VB.NET versions using StringBuilder and would take
significantly more code - of course, you could put the code in a class library
and then you could reuse it :) So, it maybe 6's in the end.
 
A

Armin Zingler

Mike said:
No, I am not trying to do that at all. You are mistaken again. I said that
you had said the former one. If you had said the latter (Harry mentioned
that the Mid statement was only there in VB6" then (despite the slightly
incorrect grammar) the word "only" would have referrred to the phrase "there
in VB6", carrying with it the clear meaning that Harry thought it was not
there in anything else. But I never accused you of saying that.

Of course, you did. You did write that I said that he wrote that VB.Net
has no Mid statement. I hope you can still follow.
If you had said the former (which you did) then the word "only" refers to
what he mentioned, meaning that he said /nothing else/ other than the fact
that the Mid statement is there in VB6. But you were wrong to make that
statement. Harry did not /only/ say that. He said other things as well, one
of which was the fact that he knew that it was also available in VB.Net.

Again you didn't read carefully. I did not say he said nothing else. I said he
didn't say anything else about VB6, and that is true. Therefore you are wrong
again.

Once again what I wrote:
"The former means, it was the only time he mentioned VB6 in his post,"

If this phrase means to you that he only said things about VB6 then you
are wrong again. Anyway, no further explanations. If I say 1+1=2 you will
blame me for saying 2*3=4.

In either case, the statement you made does not agree with the facts of what
Harry said.

Only the facts that spring from your imagination do not agree.
Simple. Even a troll such as yourself should be able to understand it.

Now be a good troll Zingler and reply as soon as you can . . . let's see how
many posts we can get into this thread. But don't bother replying straight
away because I won't be here again until tomorrow. Look forward to seeing
you then ;-)

As long as your distortion of perception is stronger than reality, we could
play the game forever. I feel sorry for you if it's the only way to make
someone deal with you. I won't play this game any longer but be sure that
I won't stop letting you disseminate lies about me.
 
M

Mike Williams

Do you mean the statement that VB.Net is
for beginners? . . . I don't agree.

As we keep being told by the various dotnet evangelists who infest the VB6
group, it is entirely up to Micro$oft how they describe their products as
long as they remain within the law. If you don't agree then you should
complain to Micro$oft, who made that statement, not to me. Don't be
frightened to criticise your masters.
Only few people are confused. Those that ignore the
name of this group, for nobody's interested in any
marketing statements while asking or answering
questions about the VB.Net language here.

The Clark / Scot / Alex / McCarthy multi headed troll is very interested in
making its dotnet marketing statement in the Classic VB group. It posts its
evangelistic dotnet diatribe there on a regular basis. Why should it have
all the fun ;-)

Mike
 
M

Mike Williams

Of course, you did. You did write that I said that he
wrote that VB.Net has no Mid statement.

No I did not, Adolf. You need to go back and read them all again. This time
open your eyes. I said that you were lying in your statement referring to
his original post, the statement you made in your third response in this
thread, when you said, "Harry only mentioned that the Mid statement was
already there in VB6 and he is now working with VB.Net, and he needs a
solution for VB.Net". That is not correct Adolf. That is not the only thing
he said in his original post. You are now trying to tell me that you meant
something different than that, but I was responding to what you actually
said at the time, and I was responding to it /before/ you made your
amplification. Your statement might not have been a deliberate attempt to
deceive, and I suppose in that case calling it a lie is a little harsh, but
your statement was definitely not the truth. The fact is that is NOT the
only thing Harry said in his original post. He said other things as well,
including a statement to the effect that he knows the Mid statement is also
available in VB.Net. Now, be a good troll Adolf and go play somewhere else.

Mike
 

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

Similar Threads


Top