473,403 Members | 2,366 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,403 software developers and data experts.

can you create a function handling dif objs w/o overloading?

Hi All;
i have created a class that handles file input and output. one of the
methods loads data from a file then returns a hashtable filled with data
class. it takes in the file name and the class of the data stored in the
file. what i would like to know is:
is there a way to make the function take a generic class without having to
create a ton of overloaded versions of the method or using a switch statement
and replicating the code? (about 15 different classes would access the
function)

thanks for any help

Nov 17 '05 #1
4 1172
Shawn <Sh***@discussions.microsoft.com> wrote:
i have created a class that handles file input and output. one of the
methods loads data from a file then returns a hashtable filled with data
class. it takes in the file name and the class of the data stored in the
file. what i would like to know is:
is there a way to make the function take a generic class without having to
create a ton of overloaded versions of the method or using a switch statement
and replicating the code? (about 15 different classes would access the
function)


Well, what do you do with the methd parameters? What do they have in
common? If the answer to both questions is "only what's in
System.Object" then declare the parameters to be of type System.Object.
Otherwise, create an interface which all the classes implement and use
that as the parameter type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
here is the code for the File loader. the only difference in the code per
the diferent classes is when the output from the binary formatter is cast to
the specific class. there really is not mich incommon between the classes.

public Hashtable LoadBinaryFile(String strFileName, Company cClassType)
{
FileStream fstrmBinaryRead;
BinaryFormatter bfmrFormatter;
Hashtable hshReturnHash;
fstrmBinaryRead = new FileStream(strFileName, FileMode.Open);
bfmrFormatter = new BinaryFormatter();
hshReturnHash = new Hashtable();
try
{
while (fstrmBinaryRead.Length > fstrmBinaryRead.Position)
{
cClassType=
(Company)bfmrFormatter.Deserialize(fstrmBinaryRead );
hshReturnHash.Add(cClassType.key, cClassType);
}
}
catch (SerializationException e1)
{
MessageBox.Show("Failed to deserialize. Reason: " +
e1.Message, "Serialization Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
fstrmBinaryRead.Close();
}
return (hshReturnHash);
}

i know in C++ i could use a template class to handle the different classes.
does C# have such a function? i thought i saw something like that with
VS2005. but no examples of how to use it.
Thanks

"Jon Skeet [C# MVP]" wrote:
Shawn <Sh***@discussions.microsoft.com> wrote:
i have created a class that handles file input and output. one of the
methods loads data from a file then returns a hashtable filled with data
class. it takes in the file name and the class of the data stored in the
file. what i would like to know is:
is there a way to make the function take a generic class without having to
create a ton of overloaded versions of the method or using a switch statement
and replicating the code? (about 15 different classes would access the
function)


Well, what do you do with the methd parameters? What do they have in
common? If the answer to both questions is "only what's in
System.Object" then declare the parameters to be of type System.Object.
Otherwise, create an interface which all the classes implement and use
that as the parameter type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Shawn <Sh***@discussions.microsoft.com> wrote:
here is the code for the File loader. the only difference in the code per
the diferent classes is when the output from the binary formatter is cast to
the specific class. there really is not mich incommon between the classes.
<snip>

There's enough - they've all got a key property, by the looks of it.
i know in C++ i could use a template class to handle the different classes.
does C# have such a function? i thought i saw something like that with
VS2005. but no examples of how to use it.


It looks to me like you don't need that parameter at all. Just define
an interface with the Key property, and make all the serialized classes
implement it. Then just change your code to:

IKeyed keyed = (IKeyed)bfmrFormatter.Deserialize(fstrmBinaryRead) ;
hshReturnHash.Add(keyed.Key, keyed);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Excellent idea i wouldn't have thought of that. thanks

"Jon Skeet [C# MVP]" wrote:
Shawn <Sh***@discussions.microsoft.com> wrote:
here is the code for the File loader. the only difference in the code per
the diferent classes is when the output from the binary formatter is cast to
the specific class. there really is not mich incommon between the classes.


<snip>

There's enough - they've all got a key property, by the looks of it.
i know in C++ i could use a template class to handle the different classes.
does C# have such a function? i thought i saw something like that with
VS2005. but no examples of how to use it.


It looks to me like you don't need that parameter at all. Just define
an interface with the Key property, and make all the serialized classes
implement it. Then just change your code to:

IKeyed keyed = (IKeyed)bfmrFormatter.Deserialize(fstrmBinaryRead) ;
hshReturnHash.Add(keyed.Key, keyed);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

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

Similar topics

5
by: Thomas LeBlanc | last post by:
I copied an example from the help: CREATE FUNCTION somefunc() RETURNS integer AS ' DECLARE quantity integer := 30; BEGIN RAISE NOTICE ''Quantity here is %'', quantity; -- Quantity here is 30...
1
by: Barbara Lindsey | last post by:
I am a postgres newbie. I am trying to create a trigger that will put a copy of a record into a backup table before update or delete. As I understand it, in order to do this I must have a...
5
by: Bob Stearns | last post by:
I don't understand the following error: DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned:...
1
by: Frank Neubert | last post by:
Hello! I work with "IBM DB 2 Universal Database for Windows NT" Version 6. I want to create Function, which are using in a Queries. I don't want to use external function. I want to create a...
13
by: invincible | last post by:
hi friends , how can I declare / create function during runtime similiar to lambda in lisp. thanks Mohan
4
by: Markus Bertheau | last post by:
Hi, why does everyone write CREATE FUNCTION foo() RETURNS INTEGER AS ' blah blah ' LANGUAGE 'plpgsql'; I've never seen for example:
2
by: Jiri Nemec | last post by:
Hello all, sorry about beginner question, but I'm sure function has correct structure, buw PostgreSQL reports error. (This function is only on approbation.) CREATE FUNCTION foo(int2) RETURNS...
2
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
0
by: bog39 | last post by:
We have z/os and DB/2 V. 8 running. I try to create a new UDF using the command CREATE FUNCTION: CREATE FUNCTION CNGETADR (INTEGER) RETURNS CHAR(50) EXTERNAL NAME CNADR001 ...
6
japuentem
by: japuentem | last post by:
throwing error when creating function DB21034E DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned:...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.