Results 1 to 8 of 8

Thread: [2005] Partial Class

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Thumbs up [2005] Partial Class

    Hi all,
    I have a partial Class as given below

    Code:
    'File1
    Partial Public Class Employee
    
        Public Sub getdata(ByVal sname As String, ByVal sage As Integer)
            'What has to written
        End Sub
    
    
    End Class
    'File2
    Partial Public Class Employee
        Private m_name As String
        Private m_age As Integer
    
        Private Sub setdata(ByVal sname As String, ByVal sage As Integer)
            Me.m_name = sname
            Me.m_age = sage
        End Sub
    
    
    End Class
    And I am creating a instance for the class as
    Code:
     Dim clsEmployee As New Employee
            clsEmployee.setdata("Dam", 1)
    what method i have to write to get the data from class in getData method

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2005] Partial Class

    Think of this as one single class. All the Partial specifier/keyword does is allow you to spread the class' code over several files really, so think of this as "how would I retreive those values if this class was contained in 1 file":

    Code:
       Public Sub getdata(ByVal sname As String, ByVal sage As Integer)
            MessageBox.Show(m_name)
            MessageBox.Show(m_age)
        End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2005] Partial Class

    I don't know where your SetData call code is from your example above, but in order to test this from a separate class (form1 here), I needed to change the SetData scope from Private to similar to Public.

    You might also want to look at using the ByRef keyword to retreive your data, or defining a structure and returning this from a GetData method which is a function as opposed to a sub. One example is like this:

    File 1 Code:
    Code:
    Partial Public Class Employee
    
        Private m_name As String
        Private m_age As Integer
    
        Public Sub setdata(ByVal sname As String, ByVal sage As Integer)
            Me.m_name = sname
            Me.m_age = sage
        End Sub
    
    End Class
    File 2 code:
    Code:
    Partial Public Class Employee
    
        Public Sub getdata(ByRef sname As String, ByRef sage As Integer)
            sname = m_name
            sage = m_age
        End Sub
    
    End Class
    Startup Form1 file code:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
            Dim clsEmployee As New Employee
            clsEmployee.setdata("Dam", 1)
    
            Dim theSetSNameValue As String = String.Empty
            Dim theSetSAgeValue As Integer
            clsEmployee.getdata(theSetSNameValue, theSetSAgeValue)
    
            MessageBox.Show(theSetSNameValue)
            MessageBox.Show(theSetSAgeValue.ToString)
            Me.Dispose()
        End Sub
    
    End Class
    Last edited by alex_read; Jul 28th, 2008 at 07:56 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Partial Class

    Thanks for the code alex..

    How can retrive the value as a object and recast it ?

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2005] Partial Class

    I'm not too sure what you mean here, but I will guess at something like this perhaps:
    Code:
        Private m_age As Object
    
        Public Sub setdata(ByVal sname As String, ByVal sage As Object)
            ... 
            Me.m_age = sage
        End Sub
    
        ...
    
        Public Sub getdata(ByRef sname As String, ByRef sage As Object)
            sage = m_age
        End Sub
    However it is always far more efficient to use a pre-defined data type as the 1st example. If you then needed to cast this, you can use any of the following after the value has been retrieved:
    1. theSetSAgeValue.ToString()
    2. Convert.ToString(theSetSAgeValue)
    3. Ctype(theSetSAgeValue, String)
    4. DirectCast(theSetSAgeValue, String)

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Partial Class

    Any other suggestions ?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Partial Class

    What's the problem? You declare a couple of variables, call GetData, pass the variables in and when the method returns those variables contain the values from the object.

    That said, I can't see why you'd be using a method to get that data. Why wouldn't you just declare two properties and get each value individually?

    As for partial classes, why would you use them here? I've only ever found two reasons to use partial classes:

    1. I was extending a TableAdapter class that was auto-generated by the IDE, so I couldn't edit the main code file for the class.

    2. I needed to use late-binding so I turned Option Strict On for the project, as I always do, then create a partial class and turned Option Strict Off for that file only. I then put all code that required late-binding in that file.

    It's usually said that partial classes are good for allowing more than one person to work on the same class at the same time but I'd say that that is generally a bad idea anyway.

    As for how to use them, you really don't need any practice because there's really nothing to do. You just have two or more class definitions with the same name and all, or all but one, of them must include the Partial key word in the declaration. That's it. In every other way they are exactly the same as any other class. Think about it. When you create a form the controls are all declared in a partial class. Do you have to do anything special to access them in code? No, you don't. A class is a class, whether it's declared in multiple parts or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2005] Partial Class

    Thanks for posting that JMC! I attended some sort of "what's new in 2005" event ages ago where MS really plugged this feature but have never found a use for it myself. It's pretty cool to hear of some real life situation usages of the partial classes after all this time!

    @danasegarane: I'd second that, not sure what you really mean by "Any other suggestions ?" here. The property suggestion is the best, or rather most professional, OOP design to go for in relation to your original sample code.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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