Results 1 to 6 of 6

Thread: A question about classes [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Location
    Kirkland, WA
    Posts
    7

    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.

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Location
    Kirkland, WA
    Posts
    7
    didnt work taxes, that still gives me same error

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Location
    Kirkland, WA
    Posts
    7
    Thanks guys, i was mistaken my error was elsewhere.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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
  •  



Click Here to Expand Forum to Full Width