George
George

Reputation: 36794

Problems learning C# in Visual Studio already

I've just started reading Microsoft .NET development. It includes lessons/labs using VB and/or C#. Now I got through the first lab using VB and am going to now do it in C#. I have copied everything out exactly (I'm pretty sure) but I get

Expected class, delegate, enum, interface, or struct

I think I should be getting a CMD saying

Tony Allen, age 32

Could anybody point out my problem here? I'm a back-end web developer (PHP) so I know about coding, I'm just new to this language (:

If you are going to edit the code below, could you please let me know what changes you made and why you made them? The more explaination the better!

Thanks!

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    struct Person{
        public string firstName;
        public string lastName;
        public int age;
    }
    public Person(string _firstName, string _lastName, int _age){
        firstName = _firstName;
        lastName = _lastName;
        age = _age;
    }
    public override string toString(){
        return firstName + " " + lastName + ", age " + age;
    }
    class Program{
        static void Main(string[] args){
            Person p = new Person("Tony", "Allen", 32);
            Console.WriteLine(p);
        }
    }
}

Upvotes: 1

Views: 156

Answers (4)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236318

Move struct initializer into struct definition. Same with other members.

  struct Person{
        public string firstName;
        public string lastName;
        public int age;

        public Person(string _firstName, string _lastName, int _age)
        {
            firstName = _firstName;
            lastName = _lastName;
            age = _age;
        }

        public override string ToString(){
            return firstName + " " + lastName + ", age " + age;
        }
    }

In C# we declare members inside class/struct definition, not like this done in C++. Please read this msdn guide on classes and structs in C#.

Upvotes: 6

ie.
ie.

Reputation: 6101

  1. Constructor and Methods should be declared within the class/struct:

    struct Person
    {
        public string firstName;
        public string lastName;
        public int age;
    
        public Person(string _firstName, string _lastName, int _age)
        {
            firstName = _firstName;
            lastName = _lastName;
            age = _age;
        }
    
        public override string toString()
        {
            return firstName + " " + lastName + ", age " + age;
        }
    }
    
  2. There is no method toString to override, but ToString

    struct Person
    {
        //...
    
        public override string ToString()
        {
            return firstName + " " + lastName + ", age " + age;
        }
    }
    

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98858

using System;

namespace Programs
{
    struct Person{
        public string firstName;
        public string lastName;
        public int age;

        public Person(string _firstName, string _lastName, int _age){
        firstName = _firstName;
        lastName = _lastName;
        age = _age;
    }

        public  string toString()
        {
            return firstName + " " + lastName + ", age " + age;
        }
    }


    class Program{
        static void Main(string[] args){
            Person p = new Person("Tony", "Allen", 32);
            Console.WriteLine(p.toString());
        }
    }
}
  • Use toString() method in your struct. You don't have to override it.
  • Use Console.WriteLine(p.toString()); not Console.WriteLine(p);
  • Get a good C# book that you can learn class and struct types basicly.

Upvotes: 0

Jules
Jules

Reputation: 559

Also, ToString is capitalized the wrong way, it should be

public override string ToString()

Upvotes: 1

Related Questions