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. Libraries of functions

Libraries of functions

Scheduled Pinned Locked Moved C#
csharpc++question
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.
  • R Offline
    R Offline
    Roger Jane
    wrote on last edited by
    #1

    Hi All, This might stem back to 'C' days and I probably ought to change my 'mindset' and other such phrases but old habits die hard. Call me Cliché Man... When I write an applicaton, I tend to have a source file or two put aside to put all 'those little useful functions' in. You know, generic things like formatting a postal code, validating things etc. that don't fit in with a specific class anywhere. Well, using C#, I've implementing this as follows: In library.cs: namespace mylib { public class rog { static public int DoSomething(int arg) { return arg+1; } } } To call this function, I'm putting this in app1.cs: using mylib; ... b = rog.DoSomething(3); This seems like using a class where there is really no need. I'd like to just call 'DoSomething(3)' without the 'rog.' part. I'm new to C# so be gentle, I'm old to C and C++ so feel free to be harsh. Am I being stupid? Thanks for any confirmation or denial... Rog

    C W 2 Replies Last reply
    0
    • R Roger Jane

      Hi All, This might stem back to 'C' days and I probably ought to change my 'mindset' and other such phrases but old habits die hard. Call me Cliché Man... When I write an applicaton, I tend to have a source file or two put aside to put all 'those little useful functions' in. You know, generic things like formatting a postal code, validating things etc. that don't fit in with a specific class anywhere. Well, using C#, I've implementing this as follows: In library.cs: namespace mylib { public class rog { static public int DoSomething(int arg) { return arg+1; } } } To call this function, I'm putting this in app1.cs: using mylib; ... b = rog.DoSomething(3); This seems like using a class where there is really no need. I'd like to just call 'DoSomething(3)' without the 'rog.' part. I'm new to C# so be gentle, I'm old to C and C++ so feel free to be harsh. Am I being stupid? Thanks for any confirmation or denial... Rog

      C Offline
      C Offline
      CWIZO
      wrote on last edited by
      #2

      You can't have methods without a class in C#... So considering that your aproach is ok, but maybe not so OOish:) Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

      1 Reply Last reply
      0
      • R Roger Jane

        Hi All, This might stem back to 'C' days and I probably ought to change my 'mindset' and other such phrases but old habits die hard. Call me Cliché Man... When I write an applicaton, I tend to have a source file or two put aside to put all 'those little useful functions' in. You know, generic things like formatting a postal code, validating things etc. that don't fit in with a specific class anywhere. Well, using C#, I've implementing this as follows: In library.cs: namespace mylib { public class rog { static public int DoSomething(int arg) { return arg+1; } } } To call this function, I'm putting this in app1.cs: using mylib; ... b = rog.DoSomething(3); This seems like using a class where there is really no need. I'd like to just call 'DoSomething(3)' without the 'rog.' part. I'm new to C# so be gentle, I'm old to C and C++ so feel free to be harsh. Am I being stupid? Thanks for any confirmation or denial... Rog

        W Offline
        W Offline
        Werdna
        wrote on last edited by
        #3

        You can't do that in C# In latest java (1.5) you can do static import and in C# 2.0 you can have static class that will force you to have only static members and can't instentiate that class. One possible (but not recommended) solution would be to have class rog { ... static methods here... } class myclass : rog { ... now you can call methods of rog without rog. }

        R 1 Reply Last reply
        0
        • W Werdna

          You can't do that in C# In latest java (1.5) you can do static import and in C# 2.0 you can have static class that will force you to have only static members and can't instentiate that class. One possible (but not recommended) solution would be to have class rog { ... static methods here... } class myclass : rog { ... now you can call methods of rog without rog. }

          R Offline
          R Offline
          Roger Jane
          wrote on last edited by
          #4

          That's sort of what I'm after - I didn't think of deriving my class from the library - seems a roundabout way of resolving the function names though. What I'm after really is a way of defining functions in the same way as you would, for example, the 'sin()' function as I'd much rather use: result = sin(pi*4); then result = rog.sin(pi*4); The library doesn't have any actual data (or if it is, it'll be static) and I don't intend to instantiate an instance of it. I still have this nagging feeling that I just haven't 'got the hang' of C# yet...! Rog

          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