Jump to content

C# - MSDN - C# for Sharp Kids

- - - - -

  • Please log in to reply
8 replies to this topic

#1
KevzJD

KevzJD

    Newbie

  • Members
  • Pip
  • 3 posts
Hi, I'm on the MDSN on c# for kids on the OOP bit (calling a method ).

class Person

    {

        public string firstName;

        public string lastName;


        public void showFullName()

        {

            Console.WriteLine("Name is " + this.firstName + " " + this.lastName);

        }

    }

When trying to call a instance of this object by doing

    class Person

    {

        public string firstName;

        public string lastName;


        public void showFullName()

        {

            Console.WriteLine("Name is " + this.firstName + " " + this.lastName);

        }

    }


    Person Kevin;

    Kevin = new Person();


}

It doesnt seem to recognize the Person object? It highlights in red as if it doesnt exist. Please help?

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Where did you put the "Person Kevin;" declaration? Is it in another source file in a main method, or is it literally under the class definition as depicted above? It should be in a main method somewhere.
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
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Your code looks to be outside of a class. You can't put code outside of a class.

#4
KevzJD

KevzJD

    Newbie

  • Members
  • Pip
  • 3 posts
Ah i see, so you need to declare the object within the class it was made?

#5
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
No, it doesn't have to be in the class that declares it, but it has to be within a class. For example
class MyClassA {

    public int PropertyA { get; set; }

    public int MethodA(int mul) {

        return mul * PropertyA;

    }

}


class MyClassB {

    MyClassA myFirstA = new MyClassA();


    public void MyBMethod() {

        MyClassA mySecondA = new MyClassA();


        myFirstA.PropertyA = 3;

        mySecondA.PropertyA = 12;

        Console.WriteLine("The value is {0}", myFirstA.MethodA(4));

        Console.WriteLine("The other value is {0}", mySecondA.Method(1));

    }

}

Notice that everything is inside a class. That's one of the 'rules' for object oriented programming, everything belongs to a class.

#6
KevzJD

KevzJD

    Newbie

  • Members
  • Pip
  • 3 posts
Ah right i see, that makes sence thanks Momerath. Do you have any good tutorials for beginners starting out with C#?

#7
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
what is different between class and structure???

#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

Tonchi said:

what is different between class and structure???

Refer to this thread for the answer:
http://forum.codecal...s-struct-c.html
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


#9
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
tnx




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users