SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    oop sub/function question

    Ok I'm not so good at oop but I know how it works and everything but recently my teacher said something and I thought that it was false but I'm not sure. My teacher isn't good at programming so that's why I'm doubting.

    My teacher said this can't be used when programming in oop.
    Code VBNET:
    Public Function test(Byval var As String) As String
    ....
    End function

    is Byval forbidden in oop or not?

  2. #2
    SitePoint Guru Wullie's Avatar
    Join Date
    Oct 2002
    Location
    Greenock, Scotland
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im not sure in what context your teacher was talking but there is no problem with the code snippet you posted. Byval is perfectly legal syntax in vb, and is infact the default behaviour for method parameters in vb.net
    ASP.NET Freelance Web Developer
    Bored? Check out my photography folio at Flickr

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know it's valid and legal in vb.net syntax but is it allowed in oop. I'll give an example

    I did it like this
    Code VBNET:
    Class test
    Public Function test(Byval strTest As STring) As String
    strTest = strTest.ToUpper
     
    return strTest
    End Function
     
    End Class
     
    Class Test2
    Sub main()
    Dim objTest As New test
    MessageBox(objTest.test("lalala"))
    End Sub
    End Class

    Teacher says this is how it should be in oop
    Code VBNET:
    Class test
    Private strTestString As String
     
    Public Property TestString() As String
    Get
    return strTestString 
    End Get
    Set(Byval value As String)
    strTestString  = value
    End Set
    End Property
     
    Public Function test() As String
    TestString = TestString.ToUpper
     
    return TestString 
    End Function
     
    End Class
     
    Class Test2
    Sub main()
    Dim objTest As New test
    objTest.TestString = "lalala"
    MessageBox(objTest.test)
    End Sub
    End Class

  4. #4
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your example isn't great, but I would agree with your teacher. His implementation has clearly defined the responsibility of the test class. If it's right or wrong however is a different story. It all depends on context and what you are trying to achieve.

    Proper OOP/D is tricker than most people think, and the majority of people that think they are doing OOP/D are infact not.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I know but really didn't have a better example in my head.

    So not declaring values in my function's head but getting values from properties in the function is better than the byval thingy. If we are talking in strict oop terms.

  6. #6
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends what the classes resposibility is. Say the class was like this (again, not a great example but it you should get the idea):
    Code Csharp:
    public class UpperString
    {
       string stringToUpper;
     
       public UpperString(string stringToUpper)
       {
          this.stringToUpper = stringToUpper
       }
     
       public string ToUpper()
       {
          return stringToUpper.ToUpper();
       }
    }
    The above classes responsibilty is clearly defined, and the class only does one thing. Here are some links that you should read:

    Single Responsibility Principle
    http://en.wikipedia.org/wiki/Single_...lity_principle

    Seperation Of Concerns
    http://en.wikipedia.org/wiki/Separation_of_concerns

    Liskov Substitution Principle
    http://www.objectmentor.com/resources/articles/lsp.pdf

    Law of Demeter
    http://en.wikipedia.org/wiki/Law_of_Demeter

    Open/Close Principle
    http://en.wikipedia.org/wiki/Open/closed_principle

    Interface Segregation Principle
    http://www.objectmentor.com/resources/articles/isp.pdf

    Dependency Inversion Principle
    http://www.objectmentor.com/resources/articles/dip.pdf

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dhtmlgod View Post
    It depends what the classes resposibility is. Say the class was like this (again, not a great example but it you should get the idea):
    Code Csharp:
    public class UpperString
    {
       string stringToUpper;
     
       public UpperString(string stringToUpper)
       {
          this.stringToUpper = stringToUpper
       }
     
       public string ToUpper()
       {
          return stringToUpper.ToUpper();
       }
    }
    The above classes responsibilty is clearly defined, and the class only does one thing. Here are some links that you should read:

    Single Responsibility Principle
    http://en.wikipedia.org/wiki/Single_...lity_principle

    Seperation Of Concerns
    http://en.wikipedia.org/wiki/Separation_of_concerns

    Liskov Substitution Principle
    http://www.objectmentor.com/resources/articles/lsp.pdf

    Law of Demeter
    http://en.wikipedia.org/wiki/Law_of_Demeter

    Open/Close Principle
    http://en.wikipedia.org/wiki/Open/closed_principle

    Interface Segregation Principle
    http://www.objectmentor.com/resources/articles/isp.pdf

    Dependency Inversion Principle
    http://www.objectmentor.com/resources/articles/dip.pdf
    Ok thanks, I get what you mean by this but in your example you are using a parameter in the function so that means it's not bad? So in oop it's still alowed to use parameters in a function instead of assigning a value to a property.

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see how your teacher is saying byval is bad. The example your teacher gave would be an example of how an object might better represent it's purpose using properties and methods without parameters. Your example of passing a parameter is best used in an helper class like a string helper object with shared methods for manipulating strings. It's a question of suitability in either case. I don't really think bad is the right word for either way but if you are learning OOP then your teacher is showing the best way to do it in an object orientated way which is fine.

  9. #9
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parameters on methods are fully allowed in OOP! Without them, it would be near impossible to develop software! But, like everything else, you should following some rules on their usage. Mine are:

    - Prefer 3 or less parameter
    - 3+ introduce a parameter object or break up method
    - Rememeber the Law of Demeter
    - Work at the correct level of abstraction

    Hope that helps


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •