Results 1 to 17 of 17

Thread: +=

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    +=

    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.


  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can use s &="abc" too. If you use it with numbers it adds it like it would a string.

    VB Code:
    1. Dim s As Integer = 1
    2.         s &= 2
    3.         Me.Text = s 'equals 12
    4.  
    5.         Dim s As String= "a"
    6.         s &= "bc"
    7.         Me.Text = s 'equals abc
    8.  
    9.         Dim s As Integer = 1
    10.         s += 2
    11.         Me.Text = s 'equals 3

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    If you need &= a lot, I would recommend to use a stringbuilder instead. It is much faster.
    Frans

  4. #4
    New Member
    Join Date
    Apr 2003
    Posts
    10
    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

  5. #5
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    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.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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
    Frans

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    If you use the += and -= ones, you will be more familiar with C# and C++ code when you see the same thing in them.

  8. #8

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    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.


  9. #9

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    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.


  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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

  11. #11

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    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.


  12. #12
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  13. #13
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    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.

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  15. #15
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  16. #16
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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..

  17. #17
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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
  •  



Click Here to Expand Forum to Full Width