Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Error 1 Invalid token '=' in class, struct, or interface member declaration, Error

Error method object class C#

  • Please log in to reply
5 replies to this topic

#1 QuantHFTer

QuantHFTer

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C#

Posted 06 August 2012 - 08:22 AM

Hi, i have started to learn C# basics and this is my first program but i got many errors

Error 1 Invalid token '=' in class, struct, or interface member declaration

Would you plz help



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstProgram
// i want to just add a class called person then make an object and then have this simple method. but i got all these errors. I am not sure why
{
class person
{
	 public string firstName;
	 public string lastName;
// i want down to instantiate an opject called myself
	 person myself;
	 myself = new person ();
	 myself.firstName = "Tomy";
	 myself.lastName = "Hass";
// then i want to use the following method
	 static void ShowMyFullName (string[] args)
	 {
		 Console.WriteLine (" My name is " + myself.firstName + " " + myself.lastName );
		 Console.ReadLine ();
	 }
}
}

  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 06 August 2012 - 08:43 AM

myself = new person ();
myself.firstName = "Tomy";
myself.lastName = "Hass";

You cannot instantiate a new person object here. You wrote these lines in the member declaration section, which is for declaring your variables only. You need to place these lines in a constructor.

static void ShowMyFullName (string[] args)
         {
                 Console.WriteLine (" My name is " + myself.firstName + " " + myself.lastName );
                 Console.ReadLine ();
         }

Also, if you want to reference 'myself' from a static context, you'll need to declare 'myself' static also.

First thing I would recommend is you write a constructor for your 'person' class that takes two parameters: a first name and a last name, and assigns those values to its members. Then, you can declare a static variable of type 'person' and initialize it inline by calling this constructor.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 QuantHFTer

QuantHFTer

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C#

Posted 06 August 2012 - 08:49 AM

Thank you for your immediate response.
So, please could you rewrite it so i could learn what exactly i did wrong by comparing.

I am still very newbie here and in programming.

So, my apology

Thanks in advance
  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 06 August 2012 - 09:01 AM

Here at Code Call, we don't often write the code for you, as we would rather you learn how to do it yourself. Do you know how to write a constructor? Here's an example of one, in its most basic form:

class Foo {

    public Foo() {
        // initialize Foo here
        Console.WriteLine("Initializing Foo!");
    }
}

If I were to instantiate a new Foo object, it would run the code in the constructor above, like so:

This:
Foo foo = new Foo();
would result in this output on the console:
Initializing Foo!

That was an example of a constructor with no parameters. Here is an example of a class with one property and a constructor built to initialize that property:
class Bar {

    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    // Parameterized constructor for Bar:
    public Bar(string newname) {
        // Set the name that was given to this object.
        name = newname;
    }
}

One would call this constructor like so:
Bar bar1 = new Bar("George"); // Instantiates a Bar object named "George".
Bar bar2 = new Bar("Fred"); // Instantiates a Bar object named "Fred".

You'll need to create a constructor for your class which takes two parameters: First name and last name, and assigns those to their appropriate class variables. Then, you can call this constructor from a variable declaration like I've done above. You can declare one in the static context if you wish for it to be statically accessible.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 06 August 2012 - 09:36 AM

Any initialization code should be in a constructor.

class person
{
		 public string firstName;
		 public string lastName;
// i want down to instantiate an opject called myself

	 public person( string firstName, string lastName)
	 {
		 this.firstName = firstName;
		 this.lastName = lastName;
		 }
// then i want to use the following method
		 public void ShowMyFullName ()
		 {
				 Console.WriteLine (" My name is " + myself.firstName + " " + myself.lastName );
				 Console.ReadLine ();
		 }
}

Then you can create an application to use it.

static void Main(string[] args)
{
person person1 = new person("John", "Smith");
person1.ShowFullName();
}

  • 1
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#6 QuantHFTer

QuantHFTer

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C#

Posted 06 August 2012 - 10:51 AM

Thank you very much. I got it. :c-grin:
  • 0





Also tagged with one or more of these keywords: Error, method, object, class, C#

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download