|
Thread: +=
-
Apr 7th, 2003, 12:52 AM
#1
Thread Starter
PowerPoster
+=
How come in VB it uses += for text and numbers?
Example:
Dim i as integer
i+=1
Also
Dim s as string
s+="asd"
Shouldn't it be
s&="asd"
I mean for continuity sake? i've always been taught not to use the "+" sign to add strings... it seems very odd.
Or am i completely wrong about the sign? This is what i've seen in a book i'm learning from.
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 7th, 2003, 01:12 AM
#2
You can use s &="abc" too. If you use it with numbers it adds it like it would a string.
VB Code:
Dim s As Integer = 1
s &= 2
Me.Text = s 'equals 12
Dim s As String= "a"
s &= "bc"
Me.Text = s 'equals abc
Dim s As Integer = 1
s += 2
Me.Text = s 'equals 3
-
Apr 7th, 2003, 03:19 AM
#3
If you need &= a lot, I would recommend to use a stringbuilder instead. It is much faster.
-
Apr 7th, 2003, 04:09 AM
#4
New Member
well yeah i got taught that too
id stick with using the ampersand
but i fink if the string being added is all characters no numbers then its fine
but i fink using the new way
+=
or -=
is very versatile
in comparison to before in vb6
i= i+1
thats just yucky
-
Apr 7th, 2003, 05:51 AM
#5
Frenzied Member
Now taht we don't have variants the danger in using += (i.e. the ambiguity) is gone so it is safe to use it in strings.
However, as mentioned above, you should use a StringBuilder instead if possible.
-
Apr 7th, 2003, 05:57 AM
#6
Originally posted by MerrionComputin
Now taht we don't have variants the danger in using += (i.e. the ambiguity) is gone so it is safe to use it in strings.
If Option Strict is OFF, there still is a danger of implicit conversions when two different types of variables are used.
eg
MyString += MyInteger
P.S. I recommend to always have Option Strict ON
-
Apr 7th, 2003, 10:29 AM
#7
PowerPoster
If you use the += and -= ones, you will be more familiar with C# and C++ code when you see the same thing in them.
-
Apr 7th, 2003, 01:13 PM
#8
Thread Starter
PowerPoster
OK so there is a &=. I wasn't sure as i hadn't tested it. But the guy who wrote this book i am reading is using += to add strings together, so i was a bit confused.
Also i've noticed microsoft and also this guy are fond of using single lower case letters as variable names....what the heck is up with that?
example:
Dim s As String = "My Name"
And microsoft uses it in thier event commands for buttons etc..
ByVal e As System.EventArgs
I've always been taught that, that is bad practice and variables should be named according to what they are doing and whjat sort of variable they are.
Example:
Dim strName As String = "My Name"
So has all of that changed?
ohh well, thanks!
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 7th, 2003, 01:14 PM
#9
Thread Starter
PowerPoster
Ohh yah, and what is this "StringBuilder" you speak of?
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 7th, 2003, 01:35 PM
#10
PowerPoster
Yes, all of that has changed... At least Microsoft is encouraging newer naming conventions instead of the old VB6 and lower ways.
Now that I use C#, I have adopted a completely new style compared to VB6. Look here:
http://msdn.microsoft.com/library/de...guidelines.asp
-
Apr 7th, 2003, 01:52 PM
#11
Thread Starter
PowerPoster
That's why i'm confused... this is what i got off the site you recomended
The following rules outline the naming guidelines for parameters:
Use descriptive parameter names. Parameter names should be descriptive enough that the name of the parameter and its type can be used to determine its meaning in most scenarios.
Use camel case for parameter names.
Use names that describe a parameter's meaning rather than names that describe a parameter's type. Development tools should provide meaningful information about a parameter's type. Therefore, a parameter's name can be put to better use by describing meaning. Use type-based parameter names sparingly and only where it is appropriate.
Do not use reserved parameters. Reserved parameters are private parameters that might be exposed in a future version if they are needed. Instead, if more data is needed in a future version of your class library, add a new overload for a method.
Do not prefix parameter names with Hungarian type notation.
The following are examples of correctly named parameters.
[Visual Basic]
GetType(typeName As String)As Type
Format(format As String, object [] args)As String
[C#]
Type GetType(string typeName)
string Format(string format, args() As object)
But microsoft goes against it's own paramter naming policy and uses the Lcase letter "e" as a parameter name.
Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 7th, 2003, 02:54 PM
#12
PowerPoster
sender is descriptive.
As far as e, this is what I found on the www.gotdotnet.com site:
1 The second parameter of an event is named 'e'
Type Name: EventSecondParametersHaveNameE
Description: By convention, .NET events have two parameters that specify the event sender and event data, as in void MyEventHandler(object sender, e EventArgs). The state associated with the event is encapsulated in an instance of an EventArgs, named 'e'.
Long Description: Events that do not provide event data should use the System.EventHandler delegate type.
Still doesn't explain how they got it. Maybe it is a standard used with other languages.
-
Apr 8th, 2003, 08:49 AM
#13
I still think the e was used as to make sure real programmers wouldnt accidently use it for a variable name .
Just to mention, the stringbuilder class is only of use if you are concat strings in loops like 1000+. If you dont know how many loops you have throw it in anyway.
-
Apr 8th, 2003, 04:23 PM
#14
yay gay
even if not concatenating large sstrings but more than 7 or 8 i like the stringbuilder.Append() method , it looks nice and clean
\m/  \m/
-
Apr 8th, 2003, 06:34 PM
#15
PowerPoster
The reason the stringbuilder class is faster is because it doesn't return a completly new reference to a string. Example:
if you just use a regular string and add something to it, the framework will create a new string in memory and put the old and new string into it, then return that. This is a lot of overhead when doing a lot of string building.
The stringbuilder class just adds more memory to the original and puts the new contents into that. It does this by allocating chunks of memory at a time, which means it doesn't have to allocate memory if there is enough there for the new string to fit into.
-
Apr 8th, 2003, 09:18 PM
#16
I wonder how many charact
I first started off in JAVA, and it was quite the norm to use
Code:
public void itemStateChanged(ItemEvent e)
I'm not entirely sure, but i think it started for simplicity sake... since Java is case sensitive... myEvent could easily be error prone if you typed mYEvent ... etc...
Anyway, I only noticed they truly use it alot in event messages or exception messages... which probably was used so it was understood this was a event handling variable...
.Net is mirroring Java to lure those J programmers over..
-
Apr 8th, 2003, 09:20 PM
#17
PowerPoster
Ya, I have seen it used in other languages as well. I just wish I knew for sure why they use it. There has to be some reason because it would suck to find out it was a 'just because' type of thing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|