Results 1 to 6 of 6

Thread: [RESOLVED] C# Casting Issues

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    121

    Resolved [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:
    1. (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:
    1. 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

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: C# Casting Issues

    Try:
    csharp Code:
    1. ((BonusBox)bonusBoxes[0]).getPosY()
    In fact, you should not use ArrayList. Use generic List instead. That way, casting would not be needed:
    csharp Code:
    1. List<BonusBox> bonusBoxes = new List<BonusBox>();
    2. //Now casting would not be needed and this should work
    3. 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#.
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    121

    Re: C# Casting Issues

    Thanks! I won't mark this resovled just yet as I havnt checked this but it looks right

    Greetz,
    Josh

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: C# Casting Issues

    You don't need parentheses around the List index access expression:
    Quote Originally Posted by Harsh Gupta View Post
    csharp Code:
    1. List<BonusBox> bonusBoxes = new List<BonusBox>();
    2. //Now casting would not be needed and this should work
    3. var y_pos = bonusBoxes[0].getPosY();
    Note also that you can iterate over the entire list with the foreach keyword:

    csharp Code:
    1. foreach(BonusBox box in bonusBoxes)
    2. {
    3.     var y_pos = box.getPosY();
    4.     // Do something with y_pos
    5. }

    Or just get an enumeration of the y pos values directly:

    csharp Code:
    1. foreach(var y_pos in bonusBoxes.Select(box => box.getPosY()))
    2. {
    3.     // Do something with y_pos
    4. }

    [Disclaimer: code written with the VBForums IDE - may or may not compile ]

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    121

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    121

    Resolved Custom Exceptions

    Oh yeah! Btw... can anyone point me in the write direction for custom exceptions. Some of the
    CSharp Code:
    1. try
    2. {
    3. // BLAH BLAH BLAH
    4. throw 1;
    5. }
    6. catch(int *)
    7. {
    8. //BLAH BLAH BLAH
    9. }
    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:
    1. [Serializable]
    2. public class MyCustomException: System.Exception
    3. {
    4. // We new to define the constructors because last time I checked we can't inherit it's constructors from a derived class.
    5. public MyCustomException()
    6. {
    7. }
    8.  
    9.    // 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!
    10. public MyCustomException(string message): base(message)
    11. {
    12. }
    13.  
    14. // Define the serialization construcor. No clue what it does, I just know we need it!
    15. protected MyCustomException(SerializationInfo info, StreamingContext context): base(info, context)
    16. {
    17. }
    18. }
    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:
    1. public MyCustomException(string message, Exception innerException): base(message, innerException)
    2. {
    3. }
    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
  •  



Click Here to Expand Forum to Full Width