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