Results 1 to 4 of 4

Thread: How to make a function work in a class. All help is appreciated!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014

    How to make a function work in a class. All help is appreciated!

    Here is what I am trying to do( I am totally lost):
    ĽA group box that collects the following information using appropriate controls: User's Name, Favorite Food (select from "Pizza", "Chinese Food", "Hamburgers"), and Age
    ĽA class named Person with the following functionality:
    ◦Members hidden by properties for the values Name, Food, and Age with the appropriate data types
    ◦A constructor that has three parameters for the Name, Food, and Age values
    ◦A function Greet which returns a String that says "Hi <User's Name>, my favorite food is <Favorite Food> too!"
    ◦A function Predict which returns a String that says "In <X> years, you will be <Age + X> years old and still like <Favorite Food> the most." X is a random integer between 10 and 40

    ĽA button "Greet Me" which shows in a label underneath the buttons that says displays the result of the Person's Greet function
    ĽA second modal form with the title "Prediction"
    ĽA button "Predict My Future" which loads the Prediction form and sets a label's text inside that form to the result of the Person's Predict function

    Here is my code so far and it doesn't work( I added a class to the code):
    Public Class Person

    'Person variables
    Public txtName As String
    Public lstFavFood As String
    Public txtAge As String
    Public lblGreet As String
    Dim Form2 As New frmPrediction
    Public lblPredict As String
    WithEvents btnPredict As Button
    WithEvents btnGreet As Button

    ' Constructor
    'Public Sub New()
    ' txtName = "(unknown)"
    ' lstFavFood = "(unknown)"
    ' txtAge = "(unknown)"
    ' End Sub

    'Function Greet that provides a string using the Name and Favorite Food

    Private Sub btnGreet_Click(sender As Object, e As EventArgs) Handles btnGreet.Click

    lblGreet = CStr("Hi " & txtName & ", my favorite food is " & lstFavFood & " too!!")

    ' Function predict

    ' Opens form2 and shows the prediction

    End Sub
    Private Sub btnPredict_Click(sender As Object, e As EventArgs) Handles btnPredict.Click

    Dim rand As New Random
    Dim intNum As Integer

    intNum = rand.Next(10 - 40)


    lblPredict = CStr("In " & intNum & " years, you will be" & txtAge + intNum & "years old and still like" & lstFavFood & "the most")

    End Sub
    End Class

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002

    Re: How to make a function work in a class. All help is appreciated!

    Saying that "it doesn't work" is a whole lot less informative than you might think. We get very few people posting questions about code that DOES work.

    So, what doesn't work? What happens that shouldn't be happening? What doesn't happen that should be happening? The code, as shown, isn't going to do much anyways. For example, you make use of a variable called lstFavFood, yet you never appear to set that variable to anything, so it is just "". You also make use of a variable called txtAge. That sounds a whole lot like it would be a textbox, but you declared it as a string variable (you shouldn't be adding a number and a string anyways). Were you expecting that the string variable would be given the value from a textbox?
    My usual boring signature: Nothing

  3. #3
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Manchester uk

    Re: How to make a function work in a class. All help is appreciated!

    If you actually post a question rather than your whole assignment you will likely get help, As that way you may actually learn form it.

    Currently it just sounds like you want someone to do it for you.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you

  4. #4
    Hyperactive Member
    Join Date
    Mar 2012

    Re: How to make a function work in a class. All help is appreciated!

    I think part of the problem that you may be having is that you are trying to do everything in one class / form in your project. Break this code up into various pieces according to the given requirements:

    I would start by having this project as a Windows form type of project that would serve as your main User Interface (UI). This would include a GroupBox that has other controls for entering in a Person's data, and outside of this GroupBox have "action" controls (ie a Greet Button with a Label beneath it to show the result of Greet, and a Predict Button that would open up a new form).

    Then you should add a Class module to your project for the "Person" Class. The requirement for this states that you are to hide its member (variables) behind Properties. That would mean that all of the member variables for the Person class (the Person's name, age, and favorite food) would be kept "hidden" by declaring them as Private, and have Public Properties to expose those hidden variables (if you really want to be fancy, put in some validation code in the Property Setter like ensure that the Person's age is > 0 (and maybe even > 5 to assume that anyone younger than 5 wouldn't be able to use your program) and < 100 (assuming you don't have anyone older than 100 using your program)). You'd also have a constructor for this class as well as the Greet and Predict functions that would be called by the main UI form.

    Finally you'd add a Form to your project for displaying your Predict result. This would be a very simple form containing only a label for displaying the Predict result and probably a Button (probably with the Text = "OK") that when clicked would close this form and put your main UI form back in focus.

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