469,336 Members | 5,745 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

String.concat method in vb.net


Hi,

Concat wors as
tmpStr = tmpStr.Concat(tmpStr, tmpStr2)

Why it do not refer to owner object so

tmpStr.Concat(tmpStr, tmpStr2)

and now tmpStr has same value as upper?

Regards, jacky
Nov 20 '05 #1
16 7762
"Jacky" <ja******@hotmail.com> schrieb

Concat wors as
tmpStr = tmpStr.Concat(tmpStr, tmpStr2)
Concat is a shared member. The "better" syntax is:

tmpStr = String.Concat(tmpStr, tmpStr2)

Why it do not refer to owner object so

tmpStr.Concat(tmpStr, tmpStr2)

and now tmpStr has same value as upper?


I hope I know what you mean, but if I do, why don't you ask for this syntax:

tmpStr.Concat(tmpStr2)

Your version contains tmpStr twice.

The answer is that Strings are "immutable". Their content can not be
changed. Consequently, you must have the assignment like in your first
version.
--
Armin


Nov 20 '05 #2
Hi Jacky,

Overloads Public Shared Function Concat (String, String) As String

Being a Shared Function means that it belongs to the Class and is
available as
tmpStr = String.Concat (tmpStr, tmpStr2)

Instances can access their own methods and all the Class methods. Hence:
tmpStr = tmpStr.Concat (tmpStr, tmpStr2)

Accessing Concat using an instance of String is handy but, as you've
noticed, misleading, because the object is not acting in its own right but as
a representative of the Class.

Regards,
Fergus
Nov 20 '05 #3
Hi, the Concat method is shared, which means it cannot access the instance
data of the class it is contained within. It's logical that this is the case
because strings are immutable (take a look at MSDN). Basically, the data
cannot be changed, so when you do "change" the data, the instance is
destroyed and a new instance is created.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"
"Jacky" <ja******@hotmail.com> wrote in message
news:Op**************@TK2MSFTNGP10.phx.gbl...
:
: Hi,
:
: Concat wors as
: tmpStr = tmpStr.Concat(tmpStr, tmpStr2)
:
: Why it do not refer to owner object so
:
: tmpStr.Concat(tmpStr, tmpStr2)
:
: and now tmpStr has same value as upper?
:
: Regards, jacky
:
:
Nov 20 '05 #4
"Jacky" <ja******@hotmail.com> wrote in news:OpIbOdfgDHA.2084
@TK2MSFTNGP10.phx.gbl:

Why it do not refer to owner object so


In addition to what the others said, also look into the StringBuilder
class.

Chris
Nov 20 '05 #5
Hello,

"Armin Zingler" <az*******@freenet.de> schrieb:
Concat wors as
tmpStr = tmpStr.Concat(tmpStr, tmpStr2)


Concat is a shared member. The "better" syntax is:


Better: "The syntax _I_ prefer is...".

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #6
Hi Herfried,

It is actually 'better' for non-personal reasons. ;-)

From my post
|| Accessing Concat using an instance of String is handy but, as you've
|| noticed, misleading, because the object is not acting in its own right
|| but as a representative of the Class.

Regards,
Fergus
Nov 20 '05 #7
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
It is actually 'better' for non-personal reasons. ;-)

From my post
| Accessing Concat using an instance of String is handy but,
| as you've noticed, misleading, because the object is not
| acting in its own right but as a representative of the Class.


You are right. Nevertheless shared members can be seen as members of _all_
instances of the class, that's why they can be accessed through instance
members (that's impossible in C#).

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #8
Cor
Fergus Armin Herfried,
I doubt what the preferable methode is.
The so called "non better" method is so widely used, that it is become a
kind standard to use it. And therefore people do it ("Who did write most
people just copy code?)
Maybe it will in future be better to use the "better" method.
Therefore, when I am writing advices here in future, I shall use the Armin
and Fergus "better" method.
Cor
Nov 20 '05 #9
Good morning, Cor,

I think many people use it because it's shown by Intellisense and they
don't realise/notice/care that it's a class member. I'm sure I'm guilty of
that at times.It <does> have the potential to mislead, though, as it did with
Jacky. Perhaps Intellisense could be cleverer and insert the class name (as an
optional, of course).

Regards,
Fergus
Nov 20 '05 #10
Cor
Herfried,
Did send this wrong
It was for my message to Fergus,
Better: "The syntax _I_ prefer is...".


This is what I prefer to use in future too.
I shall try to remember it.

Cor

Nov 20 '05 #11
Cor
Herfried,
Better: "The syntax _I_ prefer is...".


This is what I prefer to use in future too.
I shall try to remember it.

Cor

Nov 20 '05 #12
Hello,

"Cor" <no*@non.com> schrieb:
Did send this wrong
It was for my message to Fergus,
Better: "The syntax _I_ prefer is...".


This is what I prefer to use in future too.
I shall try to remember it.


I prefer it too, nevertheless calling _shared_ members by using an instance
variable _makes sense_.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #13
Cor
Hi Herfried,
Better: "The syntax _I_ prefer is...".
This is what I prefer to use in future too.
I shall try to remember it.


I prefer it too, nevertheless calling _shared_ members by using an

instance variable _makes sense_.


Both answers where right, but I prefer to use the words "I prefer" instead
of "Better is" in future,
that fits better to VB.net., "Better is" fits in my opinion more for C#

:-)
Cor
Nov 20 '05 #14
You do know that you can Import(s) a class and access it's instance
members...

///
Imports System.Threading.Thread

..
..
..
Sleep(5000)
..
..
..
///

Cool.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:#g**************@TK2MSFTNGP09.phx.gbl...
: Hello,
:
: "Cor" <no*@non.com> schrieb:
: > Did send this wrong
: > It was for my message to Fergus,
: >
: > > Better: "The syntax _I_ prefer is...".
: >
: > This is what I prefer to use in future too.
: > I shall try to remember it.
:
: I prefer it too, nevertheless calling _shared_ members by using an
instance
: variable _makes sense_.
:
: --
: Herfried K. Wagner
: MVP · VB Classic, VB.NET
: http://www.mvps.org/dotnet
:
:
Nov 20 '05 #15
Hello,

"Tom Spink" <th**********@ntlworld.com> schrieb:
You do know that you can Import(s) a class and access
it's instance members...

///
Imports System.Threading.Thread

.
.
.
Sleep(5000)
.
.
.
///

Cool.


'Sleep' is a shared member of 'Thread'.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #16
Exactly....

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
: Hello,
:
: "Tom Spink" <th**********@ntlworld.com> schrieb:
: > You do know that you can Import(s) a class and access
: > it's instance members...
: >
: > ///
: > Imports System.Threading.Thread
: >
: > .
: > .
: > .
: > Sleep(5000)
: > .
: > .
: > .
: > ///
: >
: > Cool.
:
: 'Sleep' is a shared member of 'Thread'.
:
: --
: Herfried K. Wagner
: MVP · VB Classic, VB.NET
: http://www.mvps.org/dotnet
:
:
Nov 20 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Hector A | last post: by
3 posts views Thread by John Ford | last post: by
3 posts views Thread by Dominique Vandensteen | last post: by
1 post views Thread by Trint Smith | last post: by
3 posts views Thread by Mythran | last post: by
17 posts views Thread by ramadu | last post: by
7 posts views Thread by Leonel Gayard | last post: by
15 posts views Thread by James | last post: by
3 posts views Thread by shapper | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.