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. C#
  4. Operator overloading

Operator overloading

Scheduled Pinned Locked Moved C#
csharpdebuggingtutorial
4 Posts 3 Posters 0 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.
  • A Offline
    A Offline
    amatbrewer
    wrote on last edited by
    #1

    I am working on a project in C# that involves some rather convoluted formulas to calculate values. The resulting code has been very difficult to write and even harder to read/debug. What I want to do is overload some operators to make the math a bit quicker to write and easer to read. (Especially since I know that some of these formulas will change in the future) For example; I would rather do something like “10^(somevariable* somethingelse)” than “math.pow(10, (somevariable* somethingelse)” since things like this are often buried deeper in a more complicated formula. [I have at least one formula involving 12 operators, 3 of them are raising part of the equation to the power of another part] I have looked up Operator Overloading and they all refer to using it on user defined classes or UDT’s. There must be an easer way.

    David Wilkes

    P D 2 Replies Last reply
    0
    • A amatbrewer

      I am working on a project in C# that involves some rather convoluted formulas to calculate values. The resulting code has been very difficult to write and even harder to read/debug. What I want to do is overload some operators to make the math a bit quicker to write and easer to read. (Especially since I know that some of these formulas will change in the future) For example; I would rather do something like “10^(somevariable* somethingelse)” than “math.pow(10, (somevariable* somethingelse)” since things like this are often buried deeper in a more complicated formula. [I have at least one formula involving 12 operators, 3 of them are raising part of the equation to the power of another part] I have looked up Operator Overloading and they all refer to using it on user defined classes or UDT’s. There must be an easer way.

      David Wilkes

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Write it in BASIC? Make a UDT... "just because"? (It's a worthwhile exercise.) In my http://www.codeproject.com/csharp/PIEBALDTypesRational.asp[^] I use ^ for Power, it just feels more natural to me.

      A 1 Reply Last reply
      0
      • A amatbrewer

        I am working on a project in C# that involves some rather convoluted formulas to calculate values. The resulting code has been very difficult to write and even harder to read/debug. What I want to do is overload some operators to make the math a bit quicker to write and easer to read. (Especially since I know that some of these formulas will change in the future) For example; I would rather do something like “10^(somevariable* somethingelse)” than “math.pow(10, (somevariable* somethingelse)” since things like this are often buried deeper in a more complicated formula. [I have at least one formula involving 12 operators, 3 of them are raising part of the equation to the power of another part] I have looked up Operator Overloading and they all refer to using it on user defined classes or UDT’s. There must be an easer way.

        David Wilkes

        D Offline
        D Offline
        Daniel Grunwald
        wrote on last edited by
        #3

        You could abuse the XOR operator ^ by wrapping your numbers in a struct with overloaded operators. You cannot introduce new operators, but you can do something similar with C# 3.0: you can go from the prefix notation "Math.Pow(b, e)" to the infix notation "b.Pow(e)" by defining an extension method.

        static class ExtensionMethods
        {
        public static double Pow(this double b, double e)
        {
        return Math.Pow(b, e);
        }
        }

        1 Reply Last reply
        0
        • P PIEBALDconsult

          Write it in BASIC? Make a UDT... "just because"? (It's a worthwhile exercise.) In my http://www.codeproject.com/csharp/PIEBALDTypesRational.asp[^] I use ^ for Power, it just feels more natural to me.

          A Offline
          A Offline
          amatbrewer
          wrote on last edited by
          #4

          Basic?!?! Wish I had, but far along enough now that I don't want to start over. Thanks for the link. I will see if it helps.

          David Wilkes

          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