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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Shawn said:
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.
 
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
 
Shawn said:
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);
 
Back
Top