|
-
October 24th, 2009, 10:37 AM
#1
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)
-
October 24th, 2009, 12:16 PM
#2
Re: extend on class size
What are you TRYING to do?
-
October 25th, 2009, 10:05 AM
#3
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)
-
October 25th, 2009, 12:10 PM
#4
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
-
October 27th, 2009, 09:13 AM
#5
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
-
October 27th, 2009, 12:34 PM
#6
Re: extend on class size
As I mentioned earlier, Size is a struct and you cannot inherit a structure.
-
October 29th, 2009, 03:06 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|