473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1188
Shawn <Sh***@discussi ons.microsoft.c om> 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.co m>
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(strF ileName, FileMode.Open);
bfmrFormatter = new BinaryFormatter ();
hshReturnHash = new Hashtable();
try
{
while (fstrmBinaryRea d.Length > fstrmBinaryRead .Position)
{
cClassType=
(Company)bfmrFo rmatter.Deseria lize(fstrmBinar yRead);
hshReturnHash.A dd(cClassType.k ey, cClassType);
}
}
catch (SerializationE xception e1)
{
MessageBox.Show ("Failed to deserialize. Reason: " +
e1.Message, "Serializat ion Error", MessageBoxButto ns.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***@discussi ons.microsoft.c om> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Shawn <Sh***@discussi ons.microsoft.c om> 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)bfmrFor matter.Deserial ize(fstrmBinary Read);
hshReturnHash.A dd(keyed.Key, keyed);

--
Jon Skeet - <sk***@pobox.co m>
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***@discussi ons.microsoft.c om> 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)bfmrFor matter.Deserial ize(fstrmBinary Read);
hshReturnHash.A dd(keyed.Key, keyed);

--
Jon Skeet - <sk***@pobox.co m>
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
33605
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 quantity := 50; -- -- Create a subblock
1
4462
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 function created to do this task. The function I am trying to create is as follows: CREATE FUNCTION customer_bak_proc(integer) RETURNS boolean as 'INSERT INTO customer_bak (SELECT * from customer where id = $1 )' LANGUAGE 'SQL';
5
2855
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: SQL0104N An unexpected token "CREATE FUNCTION NALF_PREFIX(tattoo varchar" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "<values>". LINE NUMBER=8. SQLSTATE=42601
1
2201
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 function, which returns the age of a person. The parameters of the function or procedure are two variables (Type is Date). The type of the value, who gives the function back is long. I try it with these code-lines:
13
4394
by: invincible | last post by:
hi friends , how can I declare / create function during runtime similiar to lambda in lisp. thanks Mohan
4
1484
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
2462
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 TEXT AS 'DECLARE ret TEXT; begin SELECT INTO ret CAST(name AS text) FROM shop_goods
2
1602
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
0
3496
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 PARAMETER STYLE DB2SQL WLM ENVIRONMENT WLMENV LANGUAGE COBOL DETERMINISTIC NO SQL NO DBINFO
6
7401
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: SQL0104N An unexpected token ")" was found following "SULTADO DECIMAL(6, 3". Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=8. SQLSTATE=42601 this is my code
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.