|
-
Jun 22nd, 2011, 05:09 AM
#1
Thread Starter
Lively Member
[RESOLVED] C# Casting Issues
Hey,
I have a few casting issues. I have an ArrayList called bonusBoxes that holds elements of the type BonusBoxes. Sooo...
bonusBoxes is an ArrayList filled with elements of type 'BonusBox'. The class BonusBox has a method in it called getPosY() which returns a double (I think). When I cast the array and then try and call the function using:
CSharp Code:
(BonusBox)(bonusBoxes[0]).getPosY()
I get an error that states that type 'object' dosn't have a method or extension named getPosY. I usually cast things in C++ using this code:
C++ Code:
static_cast<BonusBox^>(bonusBoxes[0])->getPosY()
And this works perfectly. Anyone know how I could do this in c# and I really don't want to create a new variable and then store the casted class in that as it gets way too messy! Thanks,
Josh
-
Jun 22nd, 2011, 05:58 AM
#2
Re: C# Casting Issues
Try:
csharp Code:
((BonusBox)bonusBoxes[0]).getPosY()
In fact, you should not use ArrayList. Use generic List instead. That way, casting would not be needed:
csharp Code:
List<BonusBox> bonusBoxes = new List<BonusBox>(); //Now casting would not be needed and this should work var y_pos = (bonusBoxes[0]).getPosY();
I am writing this from the top of my head. It has been 2 years since I coded in C#.
-
Jun 22nd, 2011, 04:47 PM
#3
Thread Starter
Lively Member
Re: C# Casting Issues
Thanks! I won't mark this resovled just yet as I havnt checked this but it looks right 
Greetz,
Josh
-
Jun 22nd, 2011, 06:38 PM
#4
Re: C# Casting Issues
You don't need parentheses around the List index access expression:
 Originally Posted by Harsh Gupta
csharp Code:
List<BonusBox> bonusBoxes = new List<BonusBox>();
//Now casting would not be needed and this should work
var y_pos = bonusBoxes[0].getPosY();
Note also that you can iterate over the entire list with the foreach keyword:
csharp Code:
foreach(BonusBox box in bonusBoxes)
{
var y_pos = box.getPosY();
// Do something with y_pos
}
Or just get an enumeration of the y pos values directly:
csharp Code:
foreach(var y_pos in bonusBoxes.Select(box => box.getPosY()))
{
// Do something with y_pos
}
[Disclaimer: code written with the VBForums IDE - may or may not compile ]
-
Jun 23rd, 2011, 02:15 AM
#5
Thread Starter
Lively Member
Re: C# Casting Issues
Thanks Guys,
I have to admit, what I did was take an entire program (by another person) written in C++ and convert it to C#. After the conversion there were a few issues due to casting but the first response seemed to fix it! I may go back and re-write the program someday, but if this program actually compiles I am very lucky xD Thanks for all the help guys!
-JD
-
Jun 23rd, 2011, 02:17 AM
#6
Thread Starter
Lively Member
Custom Exceptions
Oh yeah! Btw... can anyone point me in the write direction for custom exceptions. Some of the
CSharp Code:
try
{
// BLAH BLAH BLAH
throw 1;
}
catch(int *)
{
//BLAH BLAH BLAH
}
don't work in C#!
EDIT: Nevermind... After some exploration and about 1 hour trying to find out why it wasn't inheriting the constructor I settled on this code:
CSharp Code:
[Serializable]
public class MyCustomException: System.Exception
{
// We new to define the constructors because last time I checked we can't inherit it's constructors from a derived class.
public MyCustomException()
{
}
// This defines the Exception("Blah") thingy which on reverse can be retrieved using exception.Message. Without this it wouldn't be able to do much!
public MyCustomException(string message): base(message)
{
}
// Define the serialization construcor. No clue what it does, I just know we need it!
protected MyCustomException(SerializationInfo info, StreamingContext context): base(info, context)
{
}
}
This simply declares an exception that can be either thrown parameterless or with a message. If you, for example, wanted to also throw a InnerException you would define this ADDITIONAL constructor:
CSharp Code:
public MyCustomException(string message, Exception innerException): base(message, innerException)
{
}
Just remember that Constructors are overload based meaning it will pick the best constructor for the parameters that were sent.
If you have any questions just reply to this thread!
Greetz,
Josh
Last edited by jduncanator; Jun 23rd, 2011 at 04:57 AM.
Reason: Solution :)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|