10 Replies - 662 Views - Last Post: 02 December 2013 - 08:11 PM Rate Topic: -----

#1 jeremyjones123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-October 13

is a field but used as a type ?

Posted 01 December 2013 - 10:45 AM

class Pet
        {
            private void name;
            
            name = textBox1.Text;


        }


Okay so I'm most indfeinatly a noob when it comes to C#. I'm trying to call this but it keeps telling me that it's a feild but used like a type, any way around this ?

any help is greatly appreciated!!
Is This A Good Question/Topic? 0
  • +

Replies To: is a field but used as a type ?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9363
  • View blog
  • Posts: 35,172
  • Joined: 12-June 08

Re: is a field but used as a type ?

Posted 01 December 2013 - 10:51 AM

3	            private void name;


Voids are typically used with a method declaration.. you know.. to signify there is nothing being returned.

If that is a variable then you need to replace the 'void' with a string, int, boolena, etc.. what ever it should be (though it looks like it should be a string per line 5).

5	            name = textBox1.Text;

Typically this is inside a method or a function.
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 723
  • View blog
  • Posts: 3,154
  • Joined: 03-December 12

Re: is a field but used as a type ?

Posted 01 December 2013 - 10:53 AM

To start with look at get and set functions. And don't you mean name should be of type string, not void?
Was This Post Helpful? 0
  • +
  • -

#4 jeremyjones123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-October 13

Re: is a field but used as a type ?

Posted 01 December 2013 - 10:58 AM

class Pet
        {
            private string name;
      
            name = textBox1.Text;

        }


I changed it up but I'm still getting the error, is it that this doesn't belong within a class?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9363
  • View blog
  • Posts: 35,172
  • Joined: 12-June 08

Re: is a field but used as a type ?

Posted 01 December 2013 - 11:00 AM

As I said - you need to put that line five in side a method or a function. Class level concepts are variable declarations, property declarations, and method/functions.
Was This Post Helpful? 2
  • +
  • -

#6 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,534
  • Joined: 08-April 09

Re: is a field but used as a type ?

Posted 01 December 2013 - 11:05 AM

Textbox is winforms control and also a class. Where did you declare your textbox instance.
I still don't understand what are you trying to achieve.

I think if you want to get text from textbox, properties are better.

This post has been edited by Michael26: 01 December 2013 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 723
  • View blog
  • Posts: 3,154
  • Joined: 03-December 12

Re: is a field but used as a type ?

Posted 01 December 2013 - 11:10 AM

If class Pet refers to a class and not a form: it looks like you are trying to set the name. Get and set are used for this purpose. You need a set method that takes petName as a parameter to 'set' the name in the class field.
Was This Post Helpful? 0
  • +
  • -

#8 jeremyjones123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-October 13

Re: is a field but used as a type ?

Posted 01 December 2013 - 11:14 AM

Create a class named Pet (similar to the one discussed in this chapter), which has the following properties:
• Name—The Name property holds the name of a pet.
• Type—The Type property holds the type of animal that a pet is. Example values are “Dog”, “Cat”, and “Bird”.
• Age—The Age property holds the pet’s age.

Demonstrate the class in an application that creates an object of the class and lets the user enter the name, type, and age of his or her pet. This data should be stored in the object. Retrieve the pet’s Name, Type, and Age properties and display their values on the screen.

I don't wish for you fellas to do my homework but to show you what is the nature of my intent i'm posting the assignment.

I'm trying to make a class that will grab the information from the three textbox's displaying name, type, and age and display them in a message box.
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 723
  • View blog
  • Posts: 3,154
  • Joined: 03-December 12

Re: is a field but used as a type ?

Posted 01 December 2013 - 11:22 AM

Class Pet
{

  private string name, type;
  private int age; //or double depending on how specific you need to be.

  Set type;
  Set name;
  Set age;

  Get type;
  Get name;
  Get age;

}


Example

This post has been edited by astonecipher: 01 December 2013 - 11:22 AM

Was This Post Helpful? 1
  • +
  • -

#10 jeremyjones123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-October 13

Re: is a field but used as a type ?

Posted 01 December 2013 - 11:25 AM

Thank you very much, this solved my errors and see where I need to go to takle this assignment. Thanks for putting up with my ignorance fellas!

+REP
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5566
  • View blog
  • Posts: 11,903
  • Joined: 02-June 10

Re: is a field but used as a type ?

Posted 02 December 2013 - 08:11 PM

View Postjeremyjones123, on 01 December 2013 - 11:45 AM, said:

class Pet
        {
            private void name;
            
            name = textBox1.Text;


        }


You obviously need to go back to page one of your textbook and start over. You've failed to learn some of the most introductory of facts about the syntax of the language. There is little point trying to continue into chapter 6 or whatever if you don't have the knowledge you should have from chapter 1. This is not history class where you can fail the 1800's and get a fresh start when the new chapter is on World War 2. Everything in coding is built on the previous lesson. If you don't fully grasp that lesson but move forward anyway: You're hosed.

We also have a nice learning series with tutorials on classes, and properties and lots else that should help.
http://www.dreaminco...%23-start-here/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1