![]() |
|
[.NET] Grouping functions ? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] Grouping functions ? (/showthread.php?tid=5946358) Pages: Pages
1
2
|
Grouping functions ? - System[X] - Mar 1, 2019 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 RE: Grouping functions ? - Vexallus - Mar 1, 2019 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 :) RE: Grouping functions ? - System[X] - Mar 1, 2019 (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 RE: Grouping functions ? - itcorp - Mar 1, 2019 (Mar 1, 2019 12:10 PM)System[X] Wrote: I have a class(Class1) and a few functions then need to call them like thisIf I understand you correctly,this is the solution for you Code Public Class Class1Usage: Code Class1.myfunction.test1()RE: Grouping functions ? - System[X] - Mar 1, 2019 (Mar 1, 2019 02:17 PM)itcorp Wrote: If I understand you correctly,this is the solution for you 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()Or is it that is is needed for the Shared instance ? Thanks RE: Grouping functions ? - itcorp - Mar 1, 2019 (Mar 1, 2019 02:29 PM)System[X] Wrote: Yes this is kinda what i needed and can work with it. It's necessary in order to have the correct singletron pattern and if you want something more to configure on instance creation RE: Grouping functions ? - System[X] - Mar 2, 2019 (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 RE: Grouping functions ? - itcorp - Mar 2, 2019 (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. 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()RE: Grouping functions ? - System[X] - Mar 2, 2019 Ok, so how would i go abount adding another function for example: if i add this to the above code Code Public Sub test6()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. RE: Grouping functions ? - itcorp - Mar 2, 2019 (Mar 2, 2019 06:25 AM)System[X] Wrote: Ok, so how would i go abount adding another function for example: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. RE: Grouping functions ? - System[X] - Mar 2, 2019 (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()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 RE: Grouping functions ? - itcorp - Mar 2, 2019 (Mar 2, 2019 07:00 AM)System[X] Wrote: Yes, i'll try to explain it a bit better.It would not be a violation of the pattern and for that I recommend creating factory pattern RE: Grouping functions ? - System[X] - Mar 2, 2019 (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 RE: Grouping functions ? - itcorp - Mar 2, 2019 (Mar 2, 2019 07:21 AM)System[X] Wrote: Ok, but how would this be done with the code you posted ? You can do something like this. Usage: Code FactoryPattern.Pattern().Init(TestType.Test1).Test1()Code: Code Public Enum TestTypeCode Public Class FactoryPatternCode Public Interface FactoryCoreCode Public Class Class1Code Public Class Class2RE: Grouping functions ? - 0xDEAD10CC - Mar 2, 2019 (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![]() * 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. |