Results 1 to 5 of 5

Thread: [RESOLVED] assign to variable if inherits from class .NET 2.0

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Resolved [RESOLVED] assign to variable if inherits from class .NET 2.0

    Hello,

    I think I remember reading somewhere that it was possible to assign items to a variable if the item inherited from a base class, but I can't remember how.

    I want to have a class that gets properties set and one of the properties is an error code property. I want to assign any exceptions that occur to the property, but it could be any type of exception. I remember that all exceptions inherit from the Exception class.

    How can I assign an exception to a property based on the class it inherits from?

    Thank you for your help.
    Ben


    Using Visual Basic 2005/2008

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: assign to variable if inherits from class .NET 2.0

    As you say, all exceptions inherit the Exception class so you simply declare a variable or property of type Exception and then you can assign any exception to it. Of course, you'll only be able to access members of the Exception class because those are the only members that you know for sure that the object will have.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: assign to variable if inherits from class .NET 2.0

    So when, say, a webexception is assigned to a property of type exception it does the same or similar to when an item is assigned to a property of type object? Does it lose the trace information?

    How can I do something like this?

    Code:
        Private Sub X()
            Dim genericexception As Exception
            Dim webExc As System.Net.WebException
    
            genericexception = webExc
            Dim genericExceptionType As Type = genericexception.GetType
    
            Dim newWebException As System.Net.WebException = CType(genericexception, genericExceptionType<--Type genericExceptionType is not defined)
    
            messagebox.show(newWebException.ToString)
    
        End Sub
    I know that is kind of a cracked out example, but I hope it conveys my intent.
    Ben


    Using Visual Basic 2005/2008

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: assign to variable if inherits from class .NET 2.0

    You know what? Scratch my last post. That was retarted. I know what I am trying to do, but that clearly isn't going to get me there. I need to rethink this.
    Ben


    Using Visual Basic 2005/2008

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: assign to variable if inherits from class .NET 2.0

    Well, I can't do exactly what I wanted to do in .NET 2.0. If I wanted to switch to .NET 3.5 I could possibly try letting it infer, but no thank you. I will just do this and move on.

    Thank you for your help, jmcilhinney.
    Ben


    Using Visual Basic 2005/2008

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