Conversation started Jun 12, 2020 at 6:06.
Jun 12, 2020 06:06
I had doubts on classes and objects
OK ... ?
using System;
namespace CsharpFundamentals
{
    public class Person
    {
        public string FirstName;
        public string LastName;

        public void Introduce()
        {
            Console.WriteLine("My name is " + FirstName+" "   + LastName);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Person Aladdin = new Person();
            Aladdin.FirstName = "ash";
            Aladdin.LastName = "ketchum";
            Aladdin.Introduce();
can u tell how this code works
i am lost..
Suppose this was C and Person was a struct not a class. Something like:
struct Person
{
  char FirstName[256];
  char LastName[256];
};
That would be straightforward wouldn't it? You know all about structs. Yes?
Well a class is like a struct with extra features. One of those features is that a class can contain functions as well as data.
Jun 12, 2020 06:10
ok
public class Person
{
    public string FirstName; // like char FirstName[256];
    public string LastName; // like char LastName[256];

    public void Introduce() // this is a member function - structs can't do this
    {
        Console.WriteLine("My name is " + FirstName+" "   + LastName);
    }
}
so effectively Person works like struct
The new bit is that the class can contain the function Introduce.
@Aladdin yes
and we are accesing different members of it
that makes sense then
 
1 hour later…
Jun 12, 2020 07:43
@JohnRennie Do u know about public and static in c#
@Aladdin yes
can u tell me how they work
Let's take public first because it's quite simple.
You remember I said that classes can contain both variables and functions?
Well by default class variables can only be used by the functions in the class. This may seem a slightly strange thing to do, but it's actually a very important feature of classes.
Let's try and illustrate this with an example.
Jun 12, 2020 07:46
ok
Give me a moment and I'll hack together some code.
@Aladdin OK, try this code:
using System;
namespace CsharpFundamentals
{
  public class Person
  {
    int age;

    public void SetAge(int a)
    {
      if (a > 0)
        age = a;
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      Person Aladdin = new Person();
      Aladdin.age = -99;
    }
  }
}
ok give me 1 min
person.age is inaccesible
Yes, by default the member variables like age cannot be used excpet by functions in the class.
Jun 12, 2020 07:56
ah ok
The reason for this is that if the variables are accessible then other code can set them to silly values e.g. my code tries to set the age to -99 and that's silly.
By making the variables inaccessible we force any code using the class to use functions provided by the class to set the variables e.g. like this:
using System;
namespace CsharpFundamentals
{
  public class Person
  {
    int age;

    public void SetAge(int a)
    {
      if (a > 0)
        age = a;
      else
        Console.WriteLine("Invalid age {0}", a);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      Person Aladdin = new Person();
      Aladdin.SetAge(-99);
    }
  }
}
Try that
ah it says invalid age -99
Right. By doing it this way we have guaranteed that no code using your Person class can accidentally set the age to a silly value.
Jun 12, 2020 08:01
ohk that makes sense
so public means the fuction/variable is shared among all classes ig
public means the variable can be used by code outside the class.
In general you should not do this without a very good reason.
If you only allow class variables to be set by class methods then you can protect them from being set to silly values.
Note that we still use public when defining the function SetAge.
That's because if we left off the SetAge function it couldn't be used outside the class either. It can often be useful to hide class functions so they can't be used outside the class.
Are you happy you understand what public means now?
Jun 12, 2020 08:09
i didnt get about the function thing u spoke now
What wasn't clear?
it can be useful to hide class function so they can't be used outside the classes
Let me use an example. I'll modify the code to show this:
using System;
namespace CsharpFundamentals
{
  public class Person
  {
    int age;

    public void SetAge(int a)
    {
      if (a > 0)
        SetAgeSub(a);
      else
        Console.WriteLine("Invalid age {0}", a);
    }

    void SetAgeSub(int a)
    {
      age = a;
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      Person Aladdin = new Person();

      Aladdin.SetAgeSub(-99); // NOT allowed!
      Aladdin.SetAge(-99);    // Allowed but silly
      Aladdin.SetAge(18);     // Allowed
Try that
SetAgeSub is inaccesible
Yes. What I've done is split my SetAge method into two steps. First I do the checks, then second I call SetAgeSub to change the class variable.
I want other code to be able to use SetAge because it is safe to use i.e. it checks if the age supplied is silly.
But I don't want other code to use SetAgeSub because that doesn't do any checks to protect against silly values.
OK so far?
Jun 12, 2020 08:18
ok
So I make SetAge public, but I don't make SetAgeSub public. That means other code can call SetAge but not SetAgeSub.
That's what I meant when I said it's sometimes useful to stop other code from being able to call class methods.
the other code can use setagesub because they belong to same class?
Yes.
SetAge is part of the class so it can call any method that is also part of the class.
It is only code outside the class that cannot call non-public methods.
Does it all make sense now?
Jun 12, 2020 08:22
It is only code outside the class that cannot call non-public methods.
u mean can call right?
that is set age can call setagesub which is non public
SetAge is part of the class so it is not "code outside the class"
setage is part of class person but outside class program
By code outside the class I mean any code that is not in the block:
  public class Person
  {
    ...
  }
Both SetAge and SetAgeSub are in this block so they have access to each other.
So OK now?
Jun 12, 2020 08:28
It is only code outside the class that cannot call non-public methods.
what is non public here
non-public means anything not defined with the keyword public
SetAge is defined as:
public void SetAge(int a)
It's defined as public so code outside the class can call it.
void SetAgeSub(int a)
SetAgeSub is not defined as public so only code inside the class can call it.
ah u meant this
yeah got it
Cool :-)
Shall we move on to static or have you more to ask about public?
yes
no doubts on public
OK. To call SetAge we have to have an object i.e. first we have to do:
  Person Aladdin = new Person();
to create the object Aladdin, and once we have this object we can then do things like:
Jun 12, 2020 08:33
yes
  Aladdin.SetAge(18);
And this makes sense because you can't set the age for a person who doesn't exist.
So you first create the person then you set their age.
But suppose our class has a maximum age i.e. you can't set the age to a value higher than this maximum. This could be useful so the SetAge function can check for silly ages like 1001.
It would be nice for code using the class to be able to get this maximum age e.g. so it can check it doesn't pass ages larger than the maximum to SetAge.
But this maximum age isn't a property of any particular object. It applies to all Person objects. Its the sort of thing that in C you would do with a #define. Yes?
Jun 12, 2020 08:38
yes
Give me a moment to modify the example code ...
using System;
namespace CsharpFundamentals
{
  public class Person
  {
    const int MAX_AGE = 130;
    int age;

    static public int GetMaxAge()
    {
      return MAX_AGE;
    }

    public void SetAge(int a)
    {
      if (a > 0)
        SetAgeSub(a);
      else
        Console.WriteLine("Invalid age {0}", a);
    }

    void SetAgeSub(int a)
    {
      age = a;
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      int max_age = Person.GetMaxAge();
      Console.WriteLine("Max age = {0}", max_age);
Try that
Max age=130
it shows this
Yes, but note that now we have not created an object then used the object to call the GetMaxAge mehod.
Instead we call Person.GetMaxAge
Jun 12, 2020 08:44
yeah
When you define a method as static you make it callable even when no object exists. It's kind of like a global variable.
That's what static does in the definition:
static public int GetMaxAge()
Yes. It doesn't matter if any Person objects have been created, or how many Person objects have been created. The static functions, and any static variables, always exist.
ok
why does main has static in it
Not sure ...
46
Q: Why should the Main() method be static?

IndishI tried to create public void Main() in C#; it says no static void Main found. What exactly does it mean for Main to be static? I know the code works fine for public static void Main(). But why does Main have to be static?

Jun 12, 2020 08:53
ok so static means that that the function/variable created is created only time and it can't be created in objects
that is only one object for static functions exist
got it
thanks a lot!
 
Conversation ended Jun 12, 2020 at 8:55.