Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Grouping functions ?

I have a class(Class1) and a few functions then need to call them like this

class1.test1()
class1.test2()
class1.test3()


This works fine but i want to kinda group the functions into 1 (myfunction)and to do something like this

class1.myfunction.test1
class1.myfunction.test2
class1.myfunction.test3

all 3 do the same thing but produce a different return value

Im not exactly sure what its called so i can look up some examples.

Any help would be great, Thanks
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
how about using a variable as a counter for each time u use that function ? that way will make it easier for you, or try being more specific on what you need so we can try helping you out :)
(Mar 1, 2019 01:07 PM)Vexallus Wrote:  how about using a variable as a counter for each time u use that function ? that way will make it easier for you, or try being more specific on what you need so we can try helping you out :)

I did not say anything about a variable counter and i think i made it quite clear on what i am wanting to achieve. This is not a question for any coder but a semi pro who has delt with creating advanced class libraries befor.

Thanks
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(Mar 1, 2019 12:10 PM)System[X] Wrote:  I have a class(Class1) and a few functions then need to call them like this

class1.test1()
class1.test2()
class1.test3()


This works fine but i want to kinda group the functions into 1 (myfunction)and to do something like this

class1.myfunction.test1
class1.myfunction.test2
class1.myfunction.test3

all 3 do the same thing but produce a different return value

Im not exactly sure what its called so i can look up some examples.

Any help would be great, Thanks
If I understand you correctly,this is the solution for you

Code
Public Class Class1

    Public Sub test1()
        'your code
    End Sub

    Public Sub test2()
        'your code
    End Sub

    Public Sub test3()
        'your code
    End Sub

    Private Shared instance As Class1
    Private Sub New()

    End Sub

    Public Shared Function myfunction() As Class1
        If instance Is Nothing Then instance = New Class1()
        Return instance
    End Function
End Class

Usage:
Code
Class1.myfunction.test1()
  Class1.myfunction.test2()
  Class1.myfunction.test3()
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Mar 1, 2019 02:17 PM)itcorp Wrote:  If I understand you correctly,this is the solution for you

Code
Public Class Class1

   Public Sub test1()
       'your code
   End Sub

   Public Sub test2()
       'your code
   End Sub

   Public Sub test3()
       'your code
   End Sub

   Private Shared instance As Class1
   Private Sub New()

   End Sub

   Public Shared Function myfunction() As Class1
       If instance Is Nothing Then instance = New Class1()
       Return instance
   End Function
End Class

Usage:
Code
 Class1.myfunction.test1()
 Class1.myfunction.test2()
 Class1.myfunction.test3()


Yes this is kinda what i needed and can work with it.

Just 1 question why is there a new constructor in there that's not used ?

Code
Private Sub New()

    End Sub

Or is it that is is needed for the Shared instance ?

Thanks
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(Mar 1, 2019 02:29 PM)System[X] Wrote:  Yes this is kinda what i needed and can work with it.

Just 1 question why is there a new constructor in there that's not used ?

Code
Private Sub New()

   End Sub

Or is it that is is needed for the  Shared instance ?

Thanks

It's necessary in order to have the correct singletron pattern and if you want something more to configure on instance creation
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Mar 1, 2019 02:57 PM)itcorp Wrote:  It's necessary in order to have the correct singletron pattern and if you want something more to configure on instance creation

Well it actually works without the contstructor, i have no use for them in this class unless i decide to add static variables for this class.

Thanks
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(Mar 2, 2019 02:30 AM)System[X] Wrote:  Well it actually works without the contstructor, i have no use for them in this class unless i decide to add static variables for this class.

Thanks

In this way you have only one instance,because singleton can have only one instance if you want to prevent second instance creation.if you throw out the constructor from code multiple instances will be allowed.

So while the constructor is present you will not be able to do this:
Code
Dim classone = New Class1()
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
Ok, so how would i go abount adding another function for example:

if i add this to the above code

Code
Public Sub test6()
        'your code
    End Sub

Public Shared Function newfunction() As Class1
        If instance Is Nothing Then instance = New Class1()
        Return instance
    End Function

then i can call newfunction.test6, this works however i can also do myfunction.test6 or newfunction.test1

I dont want to make a new class for this tho.
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(Mar 2, 2019 06:25 AM)System[X] Wrote:  Ok, so how would i go abount adding another function for example:

if i add this to the above code

Code
   Public Sub test6()
       'your code
   End Sub

Public Shared Function newfunction() As Class1
       If instance Is Nothing Then instance = New Class1()
       Return instance
   End Function

then i can call newfunction.test6, this works however i can also do myfunction.test6 or newfunction.test1

I dont want to make a new class for this tho.
you can add as much as you want functions in that class and call them,but you always will have one instance of that class you will not make a new instance you dont need to make new class i was just explaining to you which is the result when you throw out the constructor and why it is in the code to prevent creation of new instanse of class outside of pattern.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Mar 2, 2019 06:42 AM)itcorp Wrote:  you can add as much as you want functions in that class and call them,but you always will have one instance of that class you will not make a new instance you dont need to make new class i was just explaining to you which is the result when you throw out the constructor and why it is in the code to prevent creation of new instanse of class outside of pattern.

Yes, i'll try to explain it a bit better.

Ok, so with your code, if i add
Code
Public Sub test6()
       'your code
   End Sub

I then can all it with "Class1.myfunction.test6" correct

that works perfectly fine with the function of "myfunction" but...

What if i want to add a different function for "test6" like "Class1.MyNewFunction.test6" ?

so test1,test2,test3 works with myfunction but test6 works with MyNewFunction.

Thanks
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(Mar 2, 2019 07:00 AM)System[X] Wrote:  Yes, i'll try to explain it a bit better.

Ok, so with your code, if i add
Code
Public Sub test6()
      'your code
  End Sub

I then can all it with "Class1.myfunction.test6" correct

that works perfectly fine with the function of "myfunction" but...

What if i want to add a different function for "test6" like "Class1.MyNewFunction.test6" ?

so test1,test2,test3 works with myfunction but test6 works with MyNewFunction.

Thanks
It would not be a violation of the pattern and for that I recommend creating factory pattern
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Mar 2, 2019 07:18 AM)itcorp Wrote:  It would not be a violation of the pattern and for that I recommend creating factory pattern

Ok, but how would this be done with the code you posted ?

Thanks
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(Mar 2, 2019 07:21 AM)System[X] Wrote:  Ok, but how would this be done with the code you posted ?

Thanks

You can do something like this.

Usage:
Code
FactoryPattern.Pattern().Init(TestType.Test1).Test1()
FactoryPattern.Pattern().Init(TestType.Test2).Test1()

Code:
Code
Public Enum TestType
   Test1
   Test2
End Enum

Code
Public Class FactoryPattern
   Implements FactoryCore
   Private Property _testType As TestType

   Public Function Init(_testType As TestType) As Core Implements FactoryCore.Init
       Select Case _testType
           Case TestType.Test1
               Return Class1.myfunction
           Case TestType.Test2
               Return Class2.MyNewFunction
           Case Else
               Return Nothing
       End Select
   End Function


   Private Sub New()

   End Sub

   Private Shared instance As FactoryPattern

   Public Shared Function Pattern() As FactoryPattern
       If instance Is Nothing Then instance = New FactoryPattern()
       Return instance
   End Function

End Class

Code
Public Interface FactoryCore
   Function Init(ByVal _testType As TestType) As Core
End Interface

Public Interface Core
   Sub Test1()
   Sub Test2()
   Sub Test3()
End Interface

Code
Public Class Class1
   Implements Core

   Public Sub Test1() Implements Core.Test1
       Console.WriteLine("class1 test1")
   End Sub

   Public Sub Test2() Implements Core.Test2
       Console.WriteLine("class1 test2")
   End Sub

   Public Sub Test3() Implements Core.Test3
       Console.WriteLine("class1 test3")
   End Sub

   Private Shared instance As Class1
   Private Sub New()

   End Sub

   Public Shared Function myfunction() As Class1
       If instance Is Nothing Then instance = New Class1()
       Return instance
   End Function
End Class

Code
Public Class Class2
   Implements Core

   Public Sub Test1() Implements Core.Test1
       Console.WriteLine("calss2 test1")
   End Sub

   Public Sub Test2() Implements Core.Test2
       Console.WriteLine("class2 test2")
   End Sub

   Public Sub Test3() Implements Core.Test3
       Console.WriteLine("class2 test3")
   End Sub

   Private Shared instance As Class2

   Public Shared Function MyNewFunction() As Class2
       If instance Is Nothing Then instance = New Class2()
       Return instance
   End Function
End Class
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Mar 2, 2019 08:54 AM)itcorp Wrote:  You can do something like this.

...

Why use an enum for branching over a generic type constraint? That's usually a sign of a very poorly designed factory pattern implementation. I would also brush up on your .NET naming conventions.

I would question whether any of this even needs to be singletons, and if you just need a factory pattern that initializes a derived class with overriden functions from an abstract base class holding virtual methods. -- OR, classes that implement a common interface.

A factory class that attempts to return singletons? That branching doesn't make sense and seems more like an anti-pattern in the making... IMO it should be an abstract class, remove the private constructor since that's redundant, and use it as it was intended; a factory.

I think the best approach here would be to not try to solve what he's asking but get an idea of what he's trying to do before suggesting a solution to accomplish a very weird and uncommon paradigm...

You can create somewhat of a lazy initialization factory that also doubles as a pseudo-singleton by caching instances of a specific generic type in a dictionary. You may desire to add thread-safety to this depending on what the goal is but this is just for demonstration only...

Code
public interface ICoreInitialization
{
    void Init();
}

public sealed class MyClass1 : ICoreInitialization
{
    private int _counter = 0;
    public void Init() => Console.WriteLine("{0} -> {1}", nameof(MyClass1), ++_counter);
}

public sealed class MyClass2 : ICoreInitialization
{
    private int _counter = 0;
    public void Init() => Console.WriteLine("{0} -> {1}", nameof(MyClass2), ++_counter);
}

public abstract class CoreFactory<T>
    where T : ICoreInitialization, new()
{
    private static readonly Dictionary<int, T> _map = new Dictionary<int, T>();
    public static T CreateOrGet()
    {
        int hashcode = typeof(T).GetHashCode();
        if (!_map.TryGetValue(hashcode, out T obj))
        {
            obj = new T();
            _map[hashcode] = obj;
        }
        return obj;
    }
}


public static void Main()
{
    CoreFactory<MyClass1>.CreateOrGet().Init();
    CoreFactory<MyClass2>.CreateOrGet().Init();
    CoreFactory<MyClass1>.CreateOrGet().Init();
    CoreFactory<MyClass2>.CreateOrGet().Init();
    Console.ReadKey();
}

[Image: IN8Qosv.png]

* Probably better to use Lazy<T> or something here though.

I'm just guessing though but this seems like a singleton branching problem on the surface; or a polymorphism problem unknown to OP at this time because he's structuring his code in an odd way.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by CottleSilverDec 25, 2014 03:35 AM 0 49 Dec 25, 2014 03:35 AM
Last Post: CottleSilver
[.NET] by idb.Sep 24, 2014 10:00 AM 22 4,221 Sep 24, 2014 10:00 AM
Last Post: He B TeMy
[C] by BreakoutMar 30, 2013 07:11 AM 2 120 Mar 30, 2013 07:11 AM
Last Post: atom0s