Im learning as I go with a program I am writing. Im receiving and unexpected error and hoped someone could shine some light on as to why.

I am making a list of objects but when I try to add to the list I receive
(Invalid token '(' in class, struct, or interface member declaration)

My code follows...

protected List<Weapon> weap = new List<Weapon>();
weap.Add(new Weapon("Long Sword", 5));

Also the code for the class Weapon...

class Weapon
    {
        private string name = "Typed Name";
        private int damage = 1;

        public string Name { get { return name; } set { name = value; } }
        public int Damage { get { return damage; } set { damage = value; } }


        public void Weapon(string name, int damage)
        {
            this.name = name;
            this.damage = damage;
        }
    }

as I see you are declaring your class Constructor as void which is totally wrong. Your class constructor should be like this

public Weapon(string name,int damage)
{
     this.name = name;
     this.damage = damage
}

Please use code tags when posting code on daniweb

To answer your question you have a few problems. First is the constructor for the Weapons class. The method having the same name as the class is called the constructor and returns an instance of the class. You do not declare a return type on it. It should look like this:

public Weapon(string name, int damage)
      {
        this.name = name;
        this.damage = damage;
      }

Next for your other code:

protected List<Weapon> weap = new List<Weapon>();
weap.Add(new Weapon("Long Sword", 5));

Access modifiers are not valid inside of a method like that. I don't know what you are trying to accomplish but there are two ways you could do it.
First:

private void Test123()
    {
      List<Weapon> weap = new List<Weapon>();
       weap.Add(new Weapon("Long Sword", 5));
    }

Second:

protected List<Weapon> weap = new List<Weapon>();
    private void Test123()
    {
       weap.Add(new Weapon("Long Sword", 5));
    }

    protected class Weapon
    {
      private string name = "Typed Name";
      private int damage = 1;

      public string Name { get { return name; } set { name = value; } }
      public int Damage { get { return damage; } set { damage = value; } }


      public Weapon(string name, int damage)
      {
        this.name = name;
        this.damage = damage;
      }
    } 

Notice in the second I changed the access modifier on your weapon class. The class definition needs to be as-available as any members exposing the type.

Sorry about the code not being tagged. BTW, before I continue, im also sorry if I seem novice, I am, and these questions might seem stupid.

Initially I did not have void as a return type. I did add it after receiving two errors, after removing it these are the errors I get along with the original.

(Method must have a return type) with Weapon highlighted here...

weap.Add(new Weapon("Long Sword", 5));

(Type expected) with "Long Sword" highlighted here...

weap.Add(new Weapon("Long Sword", 5));

It might help to know that I am calling the Weapon objects in my list from outside the Weapon class. So the code..

List<Weapon> weap = new List<Weapon>();
        weap.Add(new Weapon("Long Sword", 5));

.. is not in the same file as the class weapon.

The two classes are in the same namespace.

How about uploading your project so we can see whats going on then. You're still not providing enough information.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace daniweb
{
  public partial class FormWeapon : Form
  {
    public FormWeapon()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      List<Weapon> lst = new List<Weapon>();
      lst.Add(new Weapon("Spoon", 1));
      lst.Add(new Weapon("Fork", 9));
      lst.Add(new Weapon("Knife", 3));
    }
  }
}
namespace daniweb
{
  public class Weapon
  {
    private string name;
    private int damage;

    public string Name 
    { 
      get { return name; } 
      set { name = value; } 
    }
    public int Damage 
    { 
      get { return damage; } 
      set { damage = value; } 
    }

    private Weapon()
    {
    }
    public Weapon(string name, int damage)
      : this()
    {
      this.name = name;
      this.damage = damage;
    }
  }
}

How do I upload my code?

If you hit "Go Advanced" you will see an option to "Manage Attachments". Is the code I posted not enough to answer your question?

actually says invalid file when I try to add the project or any of the class files as an attachement.

I did see your code, youve been great help. It did not solve my errors though, my code is almost identical to your own, the only difference is I am writing in console instead of form application.

I will just have to give up on this program for now and try to learn other things then come back.

Although, I have a hunch the problem lies within the 'Weapon' class where I have my method name the same as my class name. Do methods not need a return value wether it be void or int? (int needing the 'return' ofcourse)

Heh maybe the Add command just doesnt work for console apps, idk. The first example for lists I found was in a starter kit I downloaded that used a form app, I spent hours just looking over the code learning.

Again, thanks for trying.

You need to .zip your project up and upload it. The problem is you didn't copy/paste the code from here as it will work in a console application.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.