Jay1b Posted December 9, 2003 Posted December 9, 2003 Could someone please post a small example of a working class? I am trying to do one at the moment, but i really dont know enough about them. Thanks. Quote
Grimfort Posted December 9, 2003 Posted December 9, 2003 This is a simple class that holds data, and has one function to show you use. The function just returns the name of the object (its a document) in uppercase. There are LOADS of excellent reasons to use classes, but just think of each class as a new box. You stick stuff in it (the private variables normally) and then ask it for data back. You can tell the box to do something to the data, like return in upper case, or run functions based on the data. Its also good for passing it data in one format (ie a string) and it can convert to another, or using properties, you can allow an integer to be added to the class, but only if its greater then zero. If your just getting into how classes work, then keep away from inheritance, overriding, overloading and scope of things until you know how (and where) to use them. Another thing to note, is that everything is really a class. A form is a class (it inherits all its functions), a module is a class used once (singleton), controls are instances of classes. Friend Class clsDocument 'Unique ID Private pUniqueID As Integer 'Name of document Private pName As String 'Owner of this document Private pOwner As Integer Friend Sub New() 'Set the defaults pUniqueID = -1 pName = "" pOwner = -1 End Sub Friend Function funcGetNameUpper() as String return pName.ToUpper() End Function End Class Quote
Jay1b Posted December 9, 2003 Author Posted December 9, 2003 Thanks.......Good Example of the actual class - but could you please give me an example of the bit of code calling it? Quote
Mehyar Posted December 10, 2003 Posted December 10, 2003 This class is still missing a lot of features that should be present though.... Users of a class should be able to set or get the values of its members. In this case (they are private) and the constructor sets them to empty strings and -1. Anyway, I really think you should go for MSDN, they have lots of samples because Classes is a major subject to understanding Object Oriented Programming and you should learn them the right way. But just for demo purposes: To use this class : 'declare a new instance of the class Dim SampleObject as New clsDocument() 'Display the name of the document in a message box (which would show empty because the member pname is empty!!) MessageBox.Show(SampleObject.funcGetNameUpper()) Hope this helps, Quote Dream as if you'll live forever, live as if you'll die today
Jay1b Posted December 11, 2003 Author Posted December 11, 2003 That was actually how i was trying to call it, but i didnt notice that the pname field was empty. Hence it was comming up blank and i thought i was just doing it wrong. By MSDN, do u mean the website? http://msdn.microsoft.com/ Thanks. Quote
Mehyar Posted December 11, 2003 Posted December 11, 2003 Check this out titled :Understanding Classes in .NET, got everyhting you need, theory and practice URL: http://msdn.microsoft.com/library/en-us/vbcn7/html/vbconCreatingYourOwnClasses.asp Quote Dream as if you'll live forever, live as if you'll die today
Jay1b Posted December 11, 2003 Author Posted December 11, 2003 Brilliant. Thank you. Oh by the way, i like the James Dean quote - although there seems to be some confusion btw whether its 'die today' or 'die tomorrow'. I always thought it was 'die tomorrow' but a google search brings up mixed results. What a lovely car he wrote off when he did die as well. Porsche 550 Spider i believe. Anyway, thanks again for your help :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.