-3

I'm following this course here and this is live demo

Please I have few questions to ask you, as I want to confirm that I'm understanding what I'm reading :

1) Why does one set value directly while creating object in below code

Transaction t1 = new Transaction("8877", "6/25/2018");

instead of doing like the below; which doesn't work !!!

Transaction transac1 = new Transaction();
transac1.("1234", "2019/10/03");

2) Is public Transaction() { and public Transaction(string c, string d) overloading concept?

3) Is the below a constructor method, using overloading?

public Transaction()
{
    tCode = " ";
    tDate = " ";
}

4) Why Transaction class doesn't have properties, eventhough I only see two below fields/variable with private access modifiers. whereas I read in OOP book that you must always use properties not to expose fields from outside.

private string tCode;
private string tDate;

public interface ITransactions
{
    // interface members
    void showTransaction();
}

public class Transaction : ITransactions
{
    private string tCode;
    private string date;

    public Transaction()
    {
        tCode = " ";
        date = " ";
    }

    public Transaction(string c, string d)
    {
        tCode = c;
        date = d;
    }

    public void showTransaction()
    {
        Console.WriteLine("Transaction ID: {0}", tCode);
        Console.WriteLine("Date: {0}", date);
    }
}

class Tester
{
    static void Main(string[] args)
    {
        Transaction t1 = new Transaction("8877", "6/25/2018");
        Transaction t2 = new Transaction("5656", "7/25/2018");

        t1.showTransaction();
        t2.showTransaction();
        Console.ReadKey();
    }
}
Airn5475
  • 2,452
  • 29
  • 51
Alice
  • 19
  • 1
  • 10
  • Read about `Types of constructor` https://www.c-sharpcorner.com/article/different-types-of-constructor-in-c-sharp/ – Prasad Telkikar Oct 03 '19 at 11:48
  • thanks. what about other questions? – Alice Oct 03 '19 at 11:55
  • You can read that: [C# - Polymorphism](https://www.tutorialspoint.com/csharp/csharp_polymorphism.htm) and [Constructor Overloading in C#](https://www.tutorialspoint.com/constructor-overloading-in-chash) and [Using Constructors (C# Programming Guide)](https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/using-constructors). I recommend this book: [Beginning Visual C# 2012 Programming](http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Visual-C-2012-Programming.productCd-1118314417.html) –  Oct 03 '19 at 12:00
  • Also you can read this: [What is polymorphism?](https://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used/58197730#58197730) –  Oct 03 '19 at 12:02
  • thanks. why do you recommand me that book? can you please give me a overview of it? cause I read many books but i still need to practice more in order to comprehend what I'm reading in those books. – Alice Oct 03 '19 at 12:05

1 Answers1

0

1)

[...] which doesn't work !!!

Transaction transac1 = new Transaction();
       transac1.("1234", "2019/10/03");

Yes, this simply doesn't work, it is invalid syntax. Either you call the constructor new Transaction(); or new Transaction("8877", "6/25/2018");

2)

Is public Transaction() { and public Transaction(string c, string d) overloading concept?

Yes.

3)

Is the below a constructor method, using overloading?

[...]

There is no such thing as "constructor method". You have constructors and you have methods, but there aren't any "constructor methods".

4)

Why Transaction class doesn't have properties, even though I only see two below fields/variable with private access modifiers.

You don't need to expose all the private fields with public properties. It is not a requirement to have public properties for any private field you have. If you don't want to provide such access to any data in your class you don't need to. In this case you only need showTransaction().

Progman
  • 16,827
  • 6
  • 33
  • 48
  • thanks. I understood the second, third and fourth... but I still don't understand the first question... I got it [here](https://stackoverflow.com/questions/740658/whats-the-difference-between-an-object-initializer-and-a-constructor). **Object instantiation VS constructor initialization** – Alice Oct 03 '19 at 12:34
  • @Alice If you have any question post a new question of what you want to ask. Keep in mind to check [ask] before asking any question and search for any information before asking a question (like https://stackoverflow.com/questions/740658/whats-the-difference-between-an-object-initializer-and-a-constructor what you have already found). Also, the code you wrote in the first part simply doesn't work, it cannot be compiled. And avoid asking multiple questions at once. the question will become too broad that way. – Progman Oct 03 '19 at 12:45
  • The code can be found [here](https://www.tutorialspoint.com/Interface-in-Chash) – Alice Oct 03 '19 at 12:47
  • [ECMA 335](https://stackoverflow.com/a/3646513/424129): "Constructors shall be instance methods, defined via a special form of method contract...". I don't believe I've ever heard the phrase "constructor method" before, and it sounds odd to me too. I would advise Alice not to use the term, just because people will say "wut?". But it is an accurate observation on Alice's part: It looks like a method, and it is a special case of a method. – 15ee8f99-57ff-4f92-890c-b56153 Oct 03 '19 at 12:47
  • @Alice The code `Transaction transac1 = new Transaction(); transac1.("1234", "2019/10/03");` you wrote does not exists in the linked page. It is invalid code which cannot be compiled. – Progman Oct 03 '19 at 12:50
  • @Progman Yes that code is not present in the original one. I added this as I was thinking that maybe it could also be written like so as I read in many books on how to create a new instance of a class. – Alice Oct 03 '19 at 12:58