dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: extend on class size

  1. #1
    Join Date
    Oct 2009
    Posts
    28

    extend on class size

    hi all, i've tried to extend on the default class size, but couldn't do so. example

    public class psize
    inherits size
    end class

    why is it that there is no such thing as size when im sure that i could create a new size(x, y)

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: extend on class size

    What are you TRYING to do?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2009
    Posts
    28

    Re: extend on class size

    well im trying to create a new class psize that could have a method which multiply itself.

    so
    Code:
    dim test = new psize(50, 50)
    test.multiply()
    'effectively i should get a size (100, 100)

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: extend on class size

    This is a VB.NET question. I am moving this thread to VB.NET forum.

    Size is a structure and you cannot inherit from a structure. You should also look at the documentation when you face such kind of issues. Tomorrow you may find a class that is not inheritable just by reading MSDN for that particular class. Take a look at MSDN documentation for the Size.

    If you want to have an additional method for an existing class or structure, you can try using extension methods. Extension methods give you a capability of adding additional methods to an existing class. Something like this should work for you, add a module to your application and then write this code
    Code:
    Imports System.Runtime.CompilerServices
    Module SizeExtensions
        <Extension()> _
        Public Function Multiply(ByRef pointSize As Size)
            Return pointSize.Height * pointSize.Width
        End Function
    End Module

  5. #5
    Join Date
    Oct 2009
    Posts
    28

    Re: extend on class size

    hey thanks, but for my case its not just multiply and i have to explicitly name it as Psize and not Size, is there a way to extend a class and at the same time making it only accessible to this extension by a new name?

    so basically all i want is to be able to do this
    [code]
    dim test = new Psize(5, 5)
    test.dosomething()
    dim test2 = new Size(5, 5)
    test2.dosomething() ' should give me an error

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: extend on class size

    As I mentioned earlier, Size is a struct and you cannot inherit a structure.

  7. #7
    Join Date
    Oct 2009
    Posts
    28

    Re: extend on class size

    so is there a solution to be able to do something like this:

    Code:
    dim test = new Psize(5, 5)
    test.dosomething()
    dim test2 = new Size(5, 5)
    test2.dosomething() ' should give me an error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)