-1

I want to develop a class library in C# whose methods I would call in a hierarchical way, like this:

using MyProduct.Common;

protected void Page_Load(object sender, EventArgs e)
{
    Utils myUtils = new Utils();

    int res = myUtils.Numbers.Add(1, 2);
    string text = myUtils.Text.ToUpper("test"); //***
}

Is it possible to do, using Namespaces, classes, interfaces, whatever?

UPDATE: I have my library like this, but it doesn't work, as I can't "reference a type through an expression" (line with *** above)

namespace MyProduct.Common
{
    public class Utils
    {
        public static class Text
        {
            public static string ToUpper(string s)
            {
                return s.ToUpper();
            }
        }

    }
}

UPDATE 2: Ok, it seems I need to clarify... I have this MyProduct.Common.dll:

namespace MyProduct.Common
{
    public static class Utils
    {
        public static class Text
        {
            public static void ToUpper(string s)
            {
                return s.ToUpper();
            }
        }

        public static class Number
        {
            public static void Add(int a, int b)
            {
                return (a + b);
            }
        }
    }
}

and I have my project:

using MAD.Comum;
...
protected void Page_Load(object sender, EventArgs e)
{
   string x = Utils.Text.ToUpper("aa"); //this works
   string res = Utils.Number.Add(1, 2); //this works
}

and now 2 questions: - is it possible to separate classes Number and Text in 2 files? - is this a good design?

paezinc
  • 339
  • 1
  • 3
  • 13
  • Yes, very much so. Do you have a specific problem? – J. Steen Dec 02 '14 at 11:04
  • Yes of course it is possible, except in c# `imports MyProduct.Common` would be `using MyProduct.Common`. What have you tried, did you get stuck? – Ben Robinson Dec 02 '14 at 11:04
  • Do you need to remember "state" in your Utils? If not, then you could make it a static class. – Hans Kesting Dec 02 '14 at 11:07
  • Utils would have general stuff, like myUtils.GenericFunction() and if I wnat somthing specific like number or text functions, I would call myUtils.Text.ToUpper() or myUtils.Numbers.Add()... so, where and how would stand Utils? – paezinc Dec 02 '14 at 11:43
  • Ok, I've clarified my question and I have my own answer... how can I post it? It's still on hold.. – paezinc Dec 02 '14 at 12:25

1 Answers1

0

Of course, you can do something like this:

namespace MyProduct.Common.Utils
{
    public static class Numbers
    {
        public static int Add(int n, int m)
        {
            return n + m;
        }
    }

    public static class Text
    {
        public static string ToUpper(string str)
        {
            return str.ToUpper();
        }
    }
}

And you use it like this:

MyProduct.Common.Utils.Numbers.Add(1, 2);

Hope this helps.

Cheers

dario
  • 5,149
  • 12
  • 28
  • 32
  • Thanks @dario.pro, but what about "Utils"? – paezinc Dec 02 '14 at 11:41
  • I have edited my answer. Is that what you want or am I missing something? – dario Dec 02 '14 at 11:52
  • see my update too, I would like to use a variable, instead of MyProduct.Common.Utils.Numbers.Add ... isn't it possible? – paezinc Dec 02 '14 at 11:54
  • Nope, that's not possible mate. I would go with what I've suggested to you, because it's a good design I think and it does what you need. Or there's other reasons why you would do that? – dario Dec 02 '14 at 11:58
  • I have clarified my questions.. thanks. – paezinc Dec 02 '14 at 12:11
  • I answer here because I can't comment anywhere. Sure you can put the class definitions in different files with the same namespace. And yes, I think its a good design. – dario Dec 02 '14 at 14:03