Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. operator overloding in VB

operator overloding in VB

Scheduled Pinned Locked Moved Visual Basic
comquestion
8 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Populate123
    wrote on last edited by
    #1

    Is it possible to use operator overloading in visual basic. I just try to do it on vb6.0 using + operator(i.e to plus with numeric number and concatenate with string number). But this does not work properly. Any idea? ---------------------CODE--------------------- Private Sub cmdDo_Click() Dim ty If ((VarType(Me.txtInput.Text) = vbString) And (VarType(Me.txtOutput.Text) = vbString)) Then ty = Me.txtInput.Text & "--" & Me.txtOutput.Text MsgBox ty ElseIf ((VarType(Me.txtInput.Text) = vbInteger) And ((VarType(Me.txtOutput.Text) = vbInteger))) Then ty = (Me.txtInput.Text) + Me.txtOutput.Text MsgBox ty Else MsgBox "Unknow Format or mismatch data" End If End Sub ------------------------END CODE------------------- Regards, himadrish himadrish@yahoo.com:-O Himadrish Laha

    D 1 Reply Last reply
    0
    • P Populate123

      Is it possible to use operator overloading in visual basic. I just try to do it on vb6.0 using + operator(i.e to plus with numeric number and concatenate with string number). But this does not work properly. Any idea? ---------------------CODE--------------------- Private Sub cmdDo_Click() Dim ty If ((VarType(Me.txtInput.Text) = vbString) And (VarType(Me.txtOutput.Text) = vbString)) Then ty = Me.txtInput.Text & "--" & Me.txtOutput.Text MsgBox ty ElseIf ((VarType(Me.txtInput.Text) = vbInteger) And ((VarType(Me.txtOutput.Text) = vbInteger))) Then ty = (Me.txtInput.Text) + Me.txtOutput.Text MsgBox ty Else MsgBox "Unknow Format or mismatch data" End If End Sub ------------------------END CODE------------------- Regards, himadrish himadrish@yahoo.com:-O Himadrish Laha

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      First, VB6 doesn't do true OOP and does not support operator overloading at all. Second, the code you wrote is making an incorrect assumption about the VarType function. VarType will return the Type of the Variable, not its contents.

      Private Sub cmdDo_Click()
      Dim ty
      If ((VarType(Me.txtInput.Text) = vbString) And (VarType(Me.txtOutput.Text) = vbString)) Then
      ty = Me.txtInput.Text & "--" & Me.txtOutput.Text
      MsgBox ty
      ElseIf ((VarType(Me.txtInput.Text) = vbInteger) And ((VarType(Me.txtOutput.Text) = vbInteger))) Then
      ty = (Me.txtInput.Text) + Me.txtOutput.Text
      MsgBox ty
      Else
      MsgBox "Unknow Format or mismatch data"
      End If
      End Sub

      The VarType statements of all your Text properties will ALWAYS return vbString. If you hit F2, select the TextBox control in the left pane and then the Text property in the right pane, and look at property details at the bottom of the window, you'll see that it says

      Property Text As String
      Member of VB.TextBox
      Returns/sets the text contained in the control.

      On top of that, both the '&' and '+' operators, when used with strings (.Text properties) will always append one string to another. In your code, you essentially did the same thing twice. Both 'ty=' statements will append one string to another. RageInTheMachine9532

      L 1 Reply Last reply
      0
      • D Dave Kreskowiak

        First, VB6 doesn't do true OOP and does not support operator overloading at all. Second, the code you wrote is making an incorrect assumption about the VarType function. VarType will return the Type of the Variable, not its contents.

        Private Sub cmdDo_Click()
        Dim ty
        If ((VarType(Me.txtInput.Text) = vbString) And (VarType(Me.txtOutput.Text) = vbString)) Then
        ty = Me.txtInput.Text & "--" & Me.txtOutput.Text
        MsgBox ty
        ElseIf ((VarType(Me.txtInput.Text) = vbInteger) And ((VarType(Me.txtOutput.Text) = vbInteger))) Then
        ty = (Me.txtInput.Text) + Me.txtOutput.Text
        MsgBox ty
        Else
        MsgBox "Unknow Format or mismatch data"
        End If
        End Sub

        The VarType statements of all your Text properties will ALWAYS return vbString. If you hit F2, select the TextBox control in the left pane and then the Text property in the right pane, and look at property details at the bottom of the window, you'll see that it says

        Property Text As String
        Member of VB.TextBox
        Returns/sets the text contained in the control.

        On top of that, both the '&' and '+' operators, when used with strings (.Text properties) will always append one string to another. In your code, you essentially did the same thing twice. Both 'ty=' statements will append one string to another. RageInTheMachine9532

        L Offline
        L Offline
        Link2600
        wrote on last edited by
        #3

        Okay, if VB6 does not support true OOP; then, what about VB.Net? Does VB.Net support OOP. Well, I'm not familiar with the language BASIC. I am using+learning C++ and java all the time. And I am now exploring the VC++.Net, so I wonder what's the difference between VC++.Net and VB.Net, since they are both windows programming. Do I need to understand BASIC to use VB.net?

        D 1 Reply Last reply
        0
        • L Link2600

          Okay, if VB6 does not support true OOP; then, what about VB.Net? Does VB.Net support OOP. Well, I'm not familiar with the language BASIC. I am using+learning C++ and java all the time. And I am now exploring the VC++.Net, so I wonder what's the difference between VC++.Net and VB.Net, since they are both windows programming. Do I need to understand BASIC to use VB.net?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          VB.Net supports almost all OOP specifications. The only thing it can't do is operator overloading. In order to use VB.NET, you better have at least a minimal understanding of at least one flavor of the BASIC language. C++ and Java are closely related and more flexible than BASIC or VB.Net as far as OOP goes. But, you get that flexibility at the expense of complexity. The difference between VC++.Net and VB.Net? Apples and Oranges...The two languages are just that, two completely different languages. Which one you use is NOT dictated by what you want to do, but more HOW you want to do it. For instance, if your code design requires operator overloading, you'll have to use either C++ or C#. BASIC/VB doesn't support it. But that doesn't mean that VB is useless. Like I said, your DESIGN is what dictates which languages you can use. There are ways around operator overloading if your classes are designed properly. Your classes can be designed with Add, Subtract, or Whatever operator functions you choose, designing these as overloaded functions to handle different operand cases. RageInTheMachine9532

          N A 2 Replies Last reply
          0
          • D Dave Kreskowiak

            VB.Net supports almost all OOP specifications. The only thing it can't do is operator overloading. In order to use VB.NET, you better have at least a minimal understanding of at least one flavor of the BASIC language. C++ and Java are closely related and more flexible than BASIC or VB.Net as far as OOP goes. But, you get that flexibility at the expense of complexity. The difference between VC++.Net and VB.Net? Apples and Oranges...The two languages are just that, two completely different languages. Which one you use is NOT dictated by what you want to do, but more HOW you want to do it. For instance, if your code design requires operator overloading, you'll have to use either C++ or C#. BASIC/VB doesn't support it. But that doesn't mean that VB is useless. Like I said, your DESIGN is what dictates which languages you can use. There are ways around operator overloading if your classes are designed properly. Your classes can be designed with Add, Subtract, or Whatever operator functions you choose, designing these as overloaded functions to handle different operand cases. RageInTheMachine9532

            N Offline
            N Offline
            Nick Seng
            wrote on last edited by
            #5

            RageInTheMachine9532 wrote: Which one you use is NOT dictated by what you want to do I'm sorry but I disagree. If I want to write a hardware driver, I'm sure not going to use VB, nor am i going to use C++ to write a simple business app.


            Support Bone

            D 1 Reply Last reply
            0
            • N Nick Seng

              RageInTheMachine9532 wrote: Which one you use is NOT dictated by what you want to do I'm sorry but I disagree. If I want to write a hardware driver, I'm sure not going to use VB, nor am i going to use C++ to write a simple business app.


              Support Bone

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              OK. That's a special purpose example. But for the great majority of apps, the design is the dictator. You project requirements are laid out and you come up with a modular design framework. Various parts of the framework are then designed and coded. The designs you come up with are going to be based on the techniques and languages you are familiar with. Sure, some languages are better suited for certain tasks than others. You COULD write a simple business app in C++ but, your right, it is simpler in VB. Could you write a data fitting algorithm in either language? Sure! Again, it comes down to what your familiar with in languages and techniques. Anyway, the person who posted the original question doesn't sound like he is going to be writing device drivers anytime soon. He still needs to learn the differences in languages, what their capabilities and limitations are and how to get around them when appropriate. Just my humble opinion... But in the end, it's a combination of factors that determine the language(s) used for various parts of or for an entire project. RageInTheMachine9532

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                VB.Net supports almost all OOP specifications. The only thing it can't do is operator overloading. In order to use VB.NET, you better have at least a minimal understanding of at least one flavor of the BASIC language. C++ and Java are closely related and more flexible than BASIC or VB.Net as far as OOP goes. But, you get that flexibility at the expense of complexity. The difference between VC++.Net and VB.Net? Apples and Oranges...The two languages are just that, two completely different languages. Which one you use is NOT dictated by what you want to do, but more HOW you want to do it. For instance, if your code design requires operator overloading, you'll have to use either C++ or C#. BASIC/VB doesn't support it. But that doesn't mean that VB is useless. Like I said, your DESIGN is what dictates which languages you can use. There are ways around operator overloading if your classes are designed properly. Your classes can be designed with Add, Subtract, or Whatever operator functions you choose, designing these as overloaded functions to handle different operand cases. RageInTheMachine9532

                A Offline
                A Offline
                Anthony_Yio
                wrote on last edited by
                #7

                VB program is not as fast as C++ and our operating system like windows and linux are written in C and C++. When writing device driver or component. It is always better of using C++. For business application and RAD which program speed is not crucial. It is okay to use VB. Sonork 100.41263:Anthony_Yio

                A 1 Reply Last reply
                0
                • A Anthony_Yio

                  VB program is not as fast as C++ and our operating system like windows and linux are written in C and C++. When writing device driver or component. It is always better of using C++. For business application and RAD which program speed is not crucial. It is okay to use VB. Sonork 100.41263:Anthony_Yio

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  With .NET, the argument is no longer true since managed code is managed code - all .NET languages compile to IL and use the CLR - the only differences are any language-specific differences, which if the application is CLS compliant are not supposed to be used anyways.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups