Object cast error in runtime

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

Hi,

I have a project "GrabTritonData_HiFreq" that uses some functions that
are frequently used. Therefore, I've created a separate project
"LibraryCommon" that contains these functions. One of the functions in
"LibraryCommon" is "FindInitialLow".

I call the function "FindInitialLow" in "LibraryCommon" from
"GrabTritonData_HiFreq", and it seems that there is an issue with
object casting in "FindInitialLow".

The object type I pass is of type "Bucket". Since it's passed to the
method from "GrabTritonData_HiFreq", it's type is therefore,
"GrabTritonData_HiFreq.Bucket". An assigment is made to a variable
declared as type "Bucket" (interpreted as "LibraryCommon.Bucket") in
the method "FindInitialLow", and that cause an exception at runtime.

Obviously because these two projects ("GrabTritonData_HiFreq" and
"LibraryCommon") use different name spaces, the same object type
"Bucket" are interpreted as different types due to being in different
scopes (name spaces).

The details of this method is below:

// Each item in mHistBucketList is "Bucket" type.
// Since this is called from "GrabTritonData_HiFreq" project,
// it's therefore, "GrabTritonData_HiFreq.Bucket" type
private Bucket FindInitialLow (ArrayList mHistBucketList,
double mThreshold)
{
Bucket mInitialLow = null;

// Exception here -- dp is of type "LibraryCommon.Bucket"
in debugger, while each item
// in mHistBucketList is "GrabTritonData_HiFreq.Bucket"
type
foreach (Bucket dp in mHistBucketList)
{
// Code omitted here
}
return mInitialLow;
}
 
Hi Arnold,

Thanks for the answer! I've tried the method you suggested. However,
it failed to get the content of a generic object, because I didn't
have definition of "GrabTritonData_HiFreq.Bucket" in "LibraryCommon".

I should have added a reference of "GrabTritonData_HiFreq" in
"LibraryCommon". In that way, I was able to have the definition of
"GrabTritonData_HiFreq.Bucket" in "LibraryCommon".

I don't need to redefine "Bucket" class (therefore,
"LibraryCommon.Bucket") in "LibraryCommon". I only need to add a
reference of "GrabTritonData_HiFreq" to "LibraryCommon", in order to
use "GrabTritonData_HiFreq.Bucket".

As a matter of fact,added a reference to "GrabTritonData_HiFreq",
then I deleted the "Bucket" class definition in "LibraryCommon", and
replaced every single "Bucket" in "LibraryCommon" with
"GrabTritonData_HiFreq.Bucket", and it worked!

Thanks!
 
Back
Top