String.concat method in vb.net

  • Thread starter Thread starter Jacky
  • Start date Start date
J

Jacky

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
 
Jacky said:
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.
 
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
 
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"


:
: 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
:
:
 
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
 
Hello,

Fergus Cooney said:
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#).
 
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
 
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
 
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
 
Hello,

Cor said:
Did send this wrong
It was for my message to Fergus,


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_.
 
Hi Herfried,
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
 
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"


: Hello,
:
: > 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
:
:
 
Hello,

Tom Spink said:
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'.
 
Exactly....

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

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"


: Hello,
:
: > 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
:
:
 
Back
Top