August 15, 2010
at
Sunday, August 15, 2010
Posted by
Billy
Unhappy with the lack of a sophisticated way to auto-generate public properties from private variables in Visual Studio, I turned to EMacs to implement my own version. The code at the end of this post is for VB.NET. It takes a buffer like this:
strLastName strFirstName intAmount dblPrice
and changes it to this:
Private strLastName As String Private strFirstName As String Private intAmount As Integer Private dblPrice As Double Public Property LastName() As String Get Return strLastName End Get Set(ByVal value As String) strLastName = value End Set End Property Public Property FirstName() As String Get Return strFirstName End Get Set(ByVal value As String) strFirstName = value End Set End Property Public Property Amount() As Integer Get Return intAmount End Get Set(ByVal value As Integer) intAmount = value End Set End Property Public Property Price() As Double Get Return dblPrice End Get Set(ByVal value As Double) dblPrice = value End Set End Property
You can easily configure the variable types it detects by modifying the alist *vb-types*. At work I have adjusted the template to expand into code complying with my company's coding guidelines. To use, visit a new buffer, type in the names of your private variables prefixed with a type designator, then type M-x props.
(defconst *vb-types* '(("str" . "String") ("dbl" . "Double") ("int" . "Integer"))) (defun var-name (var) (unless (< (length var) 4) (substring var 3))) (defun var-type (var) (unless (< (length var) 4) (let ((type (substring var 0 3))) (cdr (assoc type *vb-types*))))) (defun insert-private-vars (var) (let ((type (var-type var))) (when type (insert "Private " var " As " type) (newline)))) (defun insert-properties (var) (let ((type (var-type var)) (name (var-name var))) (when (and name type) (insert " Public Property " name "() As " type " Get Return " var " End Get Set(ByVal value As " type ") " var " = value End Set End Property ")))) (defun props () (interactive) (let ((vars (split-string (buffer-string)))) (erase-buffer) (mapc #'insert-private-vars vars) (newline) (newline) (mapc #'insert-properties vars)))
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment