|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to pass class as parameter?properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an array of data. All four classes are the same except for the number of elements in their arrays and the data. I have a MainClass (the form), which processes all of these arrays. The MainClass makes use of third party objects to do this. There are three methods in the MainClass. Currently, I pass a parameter to these three methods such as "A as string". This means to process class A. I have case statements that take this parameter in the MainClass methods to know know which class needs processing. There is much redundant code here since the same processing is done for each class. It would be nice if I could just pass in the class name as a parameter and run the code. Problem is I have four classes instead of one and can't just pass them in as type class. Should I place these four classes in a module? How else can I get around using the case statements in the MainClass? Thanks, Brett Brett wrote:
Show quoteHide quote > I have several classes that create arrays of data and have certain If you mean that A B C D all implement the 'same' methods, define an> properties. Call them A thru D classes, which means there are four. I can > call certain methods in each class and get back an array of data. All four > classes are the same except for the number of elements in their arrays and > the data. > > I have a MainClass (the form), which processes all of these arrays. The > MainClass makes use of third party objects to do this. There are three > methods in the MainClass. Currently, I pass a parameter to these three > methods such as "A as string". This means to process class A. I have case > statements that take this parameter in the MainClass methods to know know > which class needs processing. There is much redundant code here since the > same processing is done for each class. It would be nice if I could just > pass in the class name as a parameter and run the code. Problem is I have > four classes instead of one and can't just pass them in as type class. > > Should I place these four classes in a module? How else can I get around > using the case statements in the MainClass? interface IMyMethods containing these methods, and have A B C D all Implement IMyMethods. Then where you currently pass "A" As String, instead pass MyABCD As IMyMethods, and polymorphically invoke the methods. If you mean that A B C D don't all implement the 'same' methods, and you're just looking for a more elegant syntax than passing "A" and deducing you have an A, instead just pass o As Object, then find out what you have been passed with lines like If TypeOf o Is A Then Dim MyA As A = DirectCast(o, A) ' do the A stuff with MyA ElseIf TypeOf o Is B Then Dim MyB As B = DirectCast(o, B) ' do the B stuff with MyB .... -- Larry Lard Replies to group please
Show quote
Hide quote
"Larry Lard" <larryl***@hotmail.com> wrote in message I think this approach will work nicely. The method interfaces for the four news:1113407173.231972.289250@o13g2000cwo.googlegroups.com... > > Brett wrote: >> I have several classes that create arrays of data and have certain >> properties. Call them A thru D classes, which means there are four. > I can >> call certain methods in each class and get back an array of data. > All four >> classes are the same except for the number of elements in their > arrays and >> the data. >> >> I have a MainClass (the form), which processes all of these arrays. > The >> MainClass makes use of third party objects to do this. There are > three >> methods in the MainClass. Currently, I pass a parameter to these > three >> methods such as "A as string". This means to process class A. I > have case >> statements that take this parameter in the MainClass methods to know > know >> which class needs processing. There is much redundant code here > since the >> same processing is done for each class. It would be nice if I could > just >> pass in the class name as a parameter and run the code. Problem is I > have >> four classes instead of one and can't just pass them in as type > class. >> >> Should I place these four classes in a module? How else can I get > around >> using the case statements in the MainClass? > > If you mean that A B C D all implement the 'same' methods, define an > interface IMyMethods containing these methods, and have A B C D all > Implement IMyMethods. Then where you currently pass "A" As String, > instead pass MyABCD As IMyMethods, and polymorphically invoke the > methods. classes are the same. Any functions returning something are all the same type. The only differences are inside of the methods, which is completely encapsulated. Given this, can I still use the above technique? Show quoteHide quote > > If you mean that A B C D don't all implement the 'same' methods, and > you're just looking for a more elegant syntax than passing "A" and > deducing you have an A, instead just pass o As Object, then find out > what you have been passed with lines like > > If TypeOf o Is A Then > Dim MyA As A = DirectCast(o, A) > ' do the A stuff with MyA > ElseIf TypeOf o Is B Then > Dim MyB As B = DirectCast(o, B) > ' do the B stuff with MyB > ... > > -- > Larry Lard > Replies to group please > Brett wrote:
Show quoteHide quote > "Larry Lard" <larryl***@hotmail.com> wrote in message Sure thing. However... as a general principle, you should only do this> news:1113407173.231972.289250@o13g2000cwo.googlegroups.com... > > > > Brett wrote: > >> I have several classes that create arrays of data and have certain > >> properties. Call them A thru D classes, which means there are four. > > I can > >> call certain methods in each class and get back an array of data. > > All four > >> classes are the same except for the number of elements in their > > arrays and > >> the data. > >> > >> I have a MainClass (the form), which processes all of these arrays. > > The > >> MainClass makes use of third party objects to do this. There are > > three > >> methods in the MainClass. Currently, I pass a parameter to these > > three > >> methods such as "A as string". This means to process class A. I > > have case > >> statements that take this parameter in the MainClass methods to know > > know > >> which class needs processing. There is much redundant code here > > since the > >> same processing is done for each class. It would be nice if I could > > just > >> pass in the class name as a parameter and run the code. Problem is I > > have > >> four classes instead of one and can't just pass them in as type > > class. > >> > >> Should I place these four classes in a module? How else can I get > > around > >> using the case statements in the MainClass? > > > > If you mean that A B C D all implement the 'same' methods, define an > > interface IMyMethods containing these methods, and have A B C D all > > Implement IMyMethods. Then where you currently pass "A" As String, > > instead pass MyABCD As IMyMethods, and polymorphically invoke the > > methods. > > I think this approach will work nicely. The method interfaces for the four > classes are the same. Any functions returning something are all the same > type. The only differences are inside of the methods, which is completely > encapsulated. Given this, can I still use the above technique? if the *meaning* of the method called and data returned are the same across all the classes that will be implementing the interface. Example: if we have CSupplier, CCustomer, CEmployee that all implement IHaveAnAddress, and one of the interface methods is GetAddress returning an array of String, then it would be misleading if, say, CSupplier.GetAddress actually returned the last five order headers for that supplier, even though the type would be correct. If the semantics of the calls are different, I would say you're better off checking the type with TypeOf ... Is and calling the different methods with different names. -- Larry Lard Replies to group please
How are you handling EUL agreement?
Loop ? How to update an application Panel-height greater Int16.MaxValue? Regex. Split or Split Finding Version of All Asemblies Required by Application Category does not exist? How to Publish a Project to Production Server special character inside a string MsgBox Problem |
|||||||||||||||||||||||