|
-
Jan 16th, 2004, 04:43 AM
#1
Thread Starter
New Member
A question about classes [RESOLVED]
I have a class, it looks like this(ill keep it simple):
Code:
Public Class Person
Private m_Name as String
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
End Class
now i create instance of this like so:
Code:
Dim OldMan As New Person
But what i want to do is set his name right away when i create the instance like this:
Code:
Dim OldMan As New Person("Mr. Smith")
What do i have to add to my class to make this possible? I thought i could add a Sub New to my class like this to make it possible but it does not seem to work:
Code:
Sub New(ByVal n as String)
m_Name = n
End Sub
but i just get an error saying "Argument not specified for parameter n"
Can someone help me out please, i just need to know how to set a property of a class right when i instance it.
Last edited by ZoomyX; Jan 16th, 2004 at 06:21 AM.
-
Jan 16th, 2004, 05:22 AM
#2
PowerPoster
Hi
"What do i have to add to my class to make this possible? I thought i could add a Sub New to my class like this to make it possible but it does not seem to work:
code:--------------------------------------------------------------------------------
Sub New(ByVal n as String)
m_Name = n
End Sub
"
try
Sub New(ByVal n As String) As String
m_Name = n
End Sub
Have a good look at the Help index for full instruction on Classes. It is very good, although you have to look at about 25 pages, much instruction is duplicated.
Regards
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 16th, 2004, 05:32 AM
#3
Thread Starter
New Member
didnt work taxes, that still gives me same error
-
Jan 16th, 2004, 06:15 AM
#4
Fanatic Member
this works for me
Code:
Public Class tester
Dim str As String
Public Sub New(ByVal value As String)
str = value
End Sub
Public ReadOnly Property rstr()
Get
Return str
End Get
End Property
End Class
'put this for a click event on your form
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim new1 As New tester("nick")
MsgBox(new1.rstr)
End Sub
hope this helps
Nick
-
Jan 16th, 2004, 06:22 AM
#5
Thread Starter
New Member
Thanks guys, i was mistaken my error was elsewhere.
-
Jan 16th, 2004, 06:29 AM
#6
PowerPoster
Hi,
Sorry, I was looking at something else in my reference book.
My literature says your construction:
"Public Class Person
Private m_Name as String
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
End Class"
Should be:
Public Class Person
Private m_Name as String
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
End Class
Try that.
Regards,
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
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
|