[RESOLVED] [2008] Need help creating custom object (I think)
Is it possible to setup objects and it's custom attributes in this way:
CustomObject
FileName as string
FilePath as string
FileType as string
RanBefore as Boolean
CurrentRank as Integer
End CustomObject
then set/get attributes like this:
CustomObject.FileName = "Song.MP3"
If CustomObject.RanBefore = True then...
If CustomObject.CurrentRank > 5 then...
etc.
Is this even the right thing to do? If so, how do I create this?
Re: [2008] Need help creating custom object (I think)
Yes, that is how EVERYTHING works in object oriented languages... Everything is an object.
You would create a class, and expose your properties.
Code:
Public Class CustomObject
Public FileName as string
Public FilePath as string
End Class
'usage
Dim myCustomObject as new CustomObject
myCustomObject.FileName = "file.txt"
myCustomObject.FilePath = "c:\somedirectory\"
of course most will tell you this is bad design, and you should actually use properties and private variables for your members.
like this:
Code:
Public Class CustomObject
Private _FileName As String
Public Property FileName() As String
Get
Return _FileName
End Get
Set(ByVal value As String)
_FileName = value
End Set
End Property
Private _FilePath As String
Public Property FilePath() As String
Get
Return _FilePath
End Get
Set(ByVal value As String)
_FilePath = value
End Set
End Property
End Class
seems like more code (and it is) but the purpose is so that the properties act as gateways to your objects state, and you may want to run some code in your object when a given property is set. Those are the main reasons behind properties (that and some databinding scenarios which require properties).
A neat trick is to use the snippets in the IDE to make writing properties easier for you.
Once you have made you class
Code:
Public Class CustomObject
End Class
just type the word "property" in the class, and hit the tab key. It will generate all the code for you, and all you need to do is name the private variable, and name the public property, You do this by hitting tab and then typing. Play around with that, it will save you loads of time.
Re: [2008] Need help creating custom object (I think)
Hey thanks a ton. I knew I wasnt that stupid when with this idea. This is what happens when you grew up on thing like QBasic and Vb6.0, and then try teaching yourself VB.NET. I think this solves my problem. Thanks!
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Lots of people didn't make good use of them, and likely because VB6 was object based and not fully object oriented, but the ability to create classes very much existed in VB6...
It wasn't nearly as extensive or capable as .NET is being fully object oriented (For example you could not create overloads), but you certainly could create classes in VB6
QBasic is another story ;)
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Hey,
Sure you can.
Here is an example of such a CustomObject, or rather class:
Code:
Public Class CustomObject
Private m_FileName As String
Public Property FileName() As String
Get
Return m_FileName
End Get
Set(ByVal value As String)
m_FileName = value
End Set
End Property
Private m_FilePath As String
Public Property FilePath() As String
Get
Return m_FilePath
End Get
Set(ByVal value As String)
m_FilePath = value
End Set
End Property
Private m_FileType As String
Public Property FileType() As String
Get
Return m_FileType
End Get
Set(ByVal value As String)
m_FileType = value
End Set
End Property
Private m_RanBefore As Boolean
Public Property RanBefore() As Boolean
Get
Return m_RanBefore
End Get
Set(ByVal value As Boolean)
m_RanBefore = value
End Set
End Property
Private m_CurrentRank As Integer
Public Property CurrentRank() As Integer
Get
Return m_CurrentRank
End Get
Set(ByVal value As Integer)
m_CurrentRank = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal fileName As String, ByVal filePath As String, ByVal fileType As String, ByVal ranBefore As Boolean, ByVal currentRank As Integer)
Me.FileName = fileName
Me.FilePath = filePath
Me.FileType = fileType
Me.RanBefore = ranBefore
Me.CurrentRank = currentRank
End Sub
End Class
And you can use this elsewhere in your code using either:
Code:
Dim customObject2 As New CustomObject("test", "test1", "test2", True, 1)
or
Code:
Dim customObject As New CustomObject()
customObject.FileName = "test"
customObject.FilePath = "test1"
customObject.FileType = "test2"
customObject.RanBefore = True
customObject.CurrentRank = 1
Hope that helps!!
Gary
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Dammit, I was creating the class, and didn't refresh the page before posting!!
Never mind :)
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Gary, not sure which version of VB you are using, but its also pretty cool that you can use the With keyword, to specify properties of a class when you are creating an instance with the new keyword, which means you don't need to worry about created that overloaded constructor to accept all the params (unless of course the params are required for the class to function, but in that case I would think you would want to not have an empty param constructor)
Anyway, you can do code like this in VB9, which is sweet.
Code:
Dim myObject As New CustomObject With {.FileName = "Test", _
.FilePath = "test1", _
.FileType = "test2", _
.RanBefore = True, _
.CurrentRank = 1}
that is all based on your class, but with ONLY having the New() constructor that takes no params...
It will be even better in VB10, when the need for _ is mostly gone...
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Hey Kleinma,
No, I didn't know that!!
I primarily code in C#, but I like to hang out in the VB.Net world just so I can make sure I can read/convert/manipulate VB code when I need to. And that is defintely a tip worth knowing.
Thanks for that!!
Gary
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Ok this is going to be a stupid question, but what about arrays of this class?
I can have 1000s of files, and it would be nice to array them.
With strings it would just be:
Dim K(1000) as string (or start with 1 and redim later)
But I cant seem to make it work this the example provided
Re: [RESOLVED] [2008] Need help creating custom object (I think)
My suggestion would be to use a generic list to hold your newly custom made object.
Code:
'where CustomObject is the class name
Dim myList as New List(Of CustomObject)
Dim myCustomObject1 as New CustomObject
'set properties for myCustomObject1
Dim myCustomObject2 as New CustomObject
'set properties for myCustomObject2
'add to list
myList.Add(myCustomObject1)
myList.Add(myCustomObject2)
'iterate over list
For Each myCustomObject as CustomObject in myList
Messagebox.Show(myCustomObject.FileName)
Next
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Hey,
In this case, you are going to want to use a Generic List of Type CustomObject, i.e.
Code:
Dim objects As New List(Of CustomObject)
objects.Add(New CustomObject("test", "test1", "test2", True, 1))
Hope that helps!!
Gary
Re: [RESOLVED] [2008] Need help creating custom object (I think)
Dammit again!!
I should learn to type quicker!!! :)
Re: [RESOLVED] [2008] Need help creating custom object (I think)
at least we are on the same page with the answers ;)
Re: [RESOLVED] [2008] Need help creating custom object (I think)