Create function from Code

In the following StackOverFlow Question

I have been asked to edit this code…..

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If DateTime.Now.Hour >= 6 And DateTime.Now.Hour < 12 Then
        Message.Text = "Good Morning"
    End If

    If DateTime.Now.Hour >= 12 And DateTime.Now.Hour <= 17 Then
        Message.Text = "Good Afternoon"
    End If

    If DateTime.Now.Hour > 17 Or DateTime.Now.Hour < 6 Then
        Message.Text = "Good Evening"
    End If
End Sub

and change it into a function. the original code basically displays one of the messages on a webpage when opened depending what time of day it is. I was looking for some help on how or what i need to change to turn it into a function.

The code below i have edit but only slightly so anything that looks completely wrong will be because i have only edited a small part of it so the ByVal in the function i have to not tried to implement into the function as i am a bit stuck.

Private Function Greetings(goodMorning as string, goodAfternoon as String, goodEvening as String) as String

Dim outPut As String

If DateTime.Now.Hour >= 6 And DateTime.Now.Hour < 12 Then
            Message.Text = "Good Morning."
    ElseIf
        DateTime.Now.Hour >= 12 And DateTime.Now.Hour <= 17 Then
        Message.Text = "Good Afternoon."
    ElseIf 
        DateTime.Now.Hour >= 12 And DateTime.Now.Hour <= 17 Then
        Message.Text = "Good Afternoon."

    Return outPut
  End Function

When you write a Method, you need to take the following into consideration:

1.) Access Level
2.) Method Type(Function? or Sub?)
*A function will return a value, a sub will perform an operation without returning a value.

3.) Method Naming. Example, a function that returns someones age could be named “GetAge”
4.) Any parameters you may wish to pass.
5.) If you create a function, you need to declare the function “As” it’s expected return type
example:

A function that will return a string may be created like this:

Public Function GetString(param As String) As String
    Return "Hello" & param
End Function

The usage of that function may look like this:

Me.Text = GetString(" World!")

So inside your function that you are expected to create, instead of directly modifying an object’s text property, you will return the message using the “Return” keyword. When that function is used later, you will set the object’s text property with the direct output of your function.

You should create a function similar to this:

    Private Function GetGreeting() As String
        If DateTime.Now.Hour >= 6 And DateTime.Now.Hour < 12 Then
            Return "Good Morning"
        ElseIf DateTime.Now.Hour >= 12 And DateTime.Now.Hour <= 17 Then
            Return "Good Afternoon"
        Else
            Return "Good Evening"
        End If
    End Function
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s