473,382 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to pass class as parameter?

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?

Thanks,
Brett
Nov 21 '05 #1
3 5447

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.

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

Nov 21 '05 #2

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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 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

Nov 21 '05 #3

Brett wrote:
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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?


Sure thing. However... as a general principle, you should only do this
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

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by...
2
by: Tony Johansson | last post by:
Hello! I use method forName and newInstance in class Class like this instance = (GameFactory)(Class.forName(name).newInstance() ); Now to my question: Assume that name is GameFactory then I...
1
by: Dion Heskett | last post by:
How can I pass a Class as a parameter to in a method ? i.e. Private myMethod( string pram1, Classobject as pram2) { Classobject.DataSource = reader; Classobject.DataBind(); }
9
by: Jay Douglas | last post by:
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error (Cannot pass '<this>' as a ref or out argument because...
9
by: Martoon | last post by:
I want to instantiate an STL map with my own compare function, and I want to pass a parameter to the compare function that will be stored and used for all comparisons in that map instance. As an...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
6
by: =?Utf-8?B?emlubw==?= | last post by:
I'm trying to pass a delegate as parameter but the code below does not compile. It display an error: 'AddressOf operand must the name of a method(without parantheses)' How can I make it work. ...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.