OT But really cool - Partial Methods In VB.NET 9

OT But really cool - Partial Methods In VB.NET 9

Old forum URL: forums.lhotka.net/forums/t/3282.aspx


pelinville posted on Friday, July 27, 2007

http://blogs.msdn.com/vbteam/archive/2007/03/27/partial-methods.aspx

These are methods that allow for much better code generation.  This is something I have dreamed of for some time now.

And while I am on the subject of Partial and code generation I figured out something just a couple of days ago. 

Ok, this is probably obvious to most everybody else but it was not for me. So I figure I would share and maybe one or two others might benifit.

I was always under the impression that when doing code generation that I had to pick either inheritance or partial classes. But actually you can combine them. 

This is really important.usefule when the templates by default hide certain methods/fields but you find you really need to expose them.

'This is the generated class and I put it in a sub folder.
Public Class CustomerBase

       Inherits Csla.Core.BusinessBase

       'I like to make all fields private when generating code.
 
      Private lvSomeProtectedVar As String

End Class

 

Public Class Customer
      Inherits CustomerBase

      'But somtime I Need to get at or expose lvSomeProtectedVar but can't
      'Also don't want to go and modify a tmeplate just to expose this one field this one time.

End Class

Partial Class CustomerBase

     'So make a Partial Class but only when needed to expose the private fields however you need.
     Public
Property SomeProtectedVar() As String
         Get
             
Return lvSomeProtectedVar
        End Get
       
Protected Set(ByVal value As String)
                lvSomeProtectedVar = value
        End Set
    
End Property

End Class

 

Like I said I am probably the last one to realize this but still, really cool.


 

MikeHamilton replied on Wednesday, August 01, 2007

Actually I have not done much with partial classes and writing objects, Really just getting into more OO abstraction, so it is something I had not thought of or ran into yet so you are nto the only one.

Thanks for posting for that reason,

Mike

mr_lasseter replied on Friday, December 21, 2007

One thing I don't understand is why VB   implemented partial methods the way they did.  Look at the following:

Partial Public Class Person

    Private _name As String

    Partial Private Sub OnNameChanging(ByVal oldValue As String, ByVal newValue As String)
    End Sub

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            OnNameChanging(_name, value)
            _name = value
        End Set
    End Property

End Class

Partial Public Class Person

    Private Sub OnNameChanging(ByVal oldValue As String, ByVal newValue As String)
        Console.WriteLine(String.Format("Old Value: {0} {2} New Value: {1}", oldValue, newValue, Environment.NewLine))
    End Sub

End Class


The non generated class has no way of knowing the OnNameChanging is actually a partial method.  To me this is a huge mistake, since someone can rename the method and change the behavior of the class.  Looks like the C# team got this right:

public partial class Person
        {
            partial void OnNameChanging(string oldValue, string newValue);
            private string _name;

            public string Name
            {
                get { return _name; }
                set
                {
                    OnNameChanging(_name, value);
                    _name = value;
                }
            }
        }

        public partial class Person
        {
            partial void OnNameChanging(string oldValue, string newValue)
            {
                Console.WriteLine(string.Format("Old Value: {0} \n New Value: {1}", oldValue, newValue));
            }
        }

To bad I can't switch to C#...

Copyright (c) Marimer LLC