locked
object keyword and defining fields like this in class RRS feed

  • Question

  • User702049738 posted

    Hello Experts;

    Can you please help explain the following below

    public class pode

    {

       private object info;   // what does the object keyword mean. Does it mean an associated an instance of 

                                       //of an associated class, if so what instance class is it

        private pode after;  //why is the class used as a type for the after variable.

    }

    Friday, February 26, 2016 4:23 AM

Answers

  • User-271186128 posted

    Hi olybobo,

    what does the object keyword mean.

    It is the type of the field.

    I suggest you could refer to the following code:

        public class Test1
        {
            //private: Access Modifiers
            //object: data type.
            //name: field name.
            private object name;
            
            //Test2: the data type is Test2. When you assign value to this field, the data type should be a Test2 type
            public Test2 T2;
     
        }
    
        public class Test2
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
    
           protected void Page_Load(object sender, EventArgs e)
            {
                Test2 t2 = new Test2() { ID = 1001, Name = "AAA" };
    
                Test1 t1 = new Test1();
    
                t1.T2 = t2; //
    
                Response.Write(t1.T2.Name);
            }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2016 7:17 AM
  • User-434868552 posted

    @olybobo

    First, i suggest you complete the free Microsoft course "C# Fundamentals for Absolute Beginners" from the the Microsoft Virtual Academy https://mva.microsoft.com/en-US/training-courses/c-fundamentals-for-absolute-beginners-8295

    Regarding your question:

    public class pode
    {
         private object info;
         private pode after;
    }

    olybobo, it is absolutely important that you study the following MSDN article:

    https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx  "Object class

    "System.Object is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy."

    olyboboobject (i prefer to write System.Object) can represent any type the programmer desires.

    objects, classes, and instances are fundamental building blocks of OOP (Object Oriented Programming)

    Study this simple code snippet:

    System.Object anything = null;
    Console.WriteLine(anything);
    Int32 ten = 10;
    anything = ten;
    Console.WriteLine(anything);
    String olybobo = "olybobo";  // System.String is a class -- see MSDN
    anything = olybobo;
    Console.WriteLine(anything);
    Boolean truth = true;
    anything      = truth;
    Console.WriteLine(anything);
    DateTime dateTimeStructure = DateTime.Now; // DateTime is a struct -- see MSDN
    anything = dateTimeStructure;
    Console.WriteLine(anything);

    output:

    null
    10
    olybobo
    True
    2016-02-26 11:46:46

    olybobo, where did you find the code that you have shown to us?  i'm guessing in a book or somewhere else because you would understand that code had you written it.

    BTW, i'm not criticizing you; a c# beginner is someone who has started the very long journey to mastery of c# (i'm still learning myself).

    BACK TO YOUR EXAMPLE:

    the are types like Double, DateTime, String, et cetera

    there are also custom types.

    The class named poole in your example is a custom type.

    poole contains 2  members:  info is an objectafter is a poole.

    The designer of the class poole would be able to tell you why she/he wanted the class poole to be able to contain an instance of itself.

    study this example:

    public class MyCustomType
    {
        public MyCustomType myCustomType1;
        public MyCustomType myCustomType2;
    }
    void Main()
    {
        MyCustomType myCustomType = new MyCustomType();
        myCustomType.myCustomType1 = null;
        myCustomType.myCustomType2 = null; 
        Console.WriteLine(myCustomType);
    }

    output:

    MyCustomType
    
    myCustomType1  null 
    
    myCustomType2  null 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2016 5:07 PM
  • User-434868552 posted

    @olybobo

    caution:  at the YouTube link, there is a follow up link to 

    www  regawmod com ... Tutorial zip

    McAfee's site advisor reports http://www.siteadvisor.com/sites/regawmod.com :

    regawmod.com
    This link is suspicious.
    We tested it and found potential security risks. Be careful.

    Website Category:
    Parked Domain

    YOUR  QUESTION ... 

    Usually, there are features of the .NET Framework that meet most of our needs.

    Sometimes we write code we do not need because we do not understand enough of the .NET Framework.

    study https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx "List<T> Class"

    Here is a trivial example using List<T>:

    public class SharedBook
    {
        public String Title;
        public DateTime DateBorrowed;
        public DateTime DateReturned;
        public String BorrowedBy;
    }

    The task is simply to write a list of borrowers and who had the book before and after each borrower.

    void Main()
    {
        List<SharedBook> sharedBooks = new List<SharedBook>();
        SharedBook borrower1 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2014, 5, 15), DateReturned = new DateTime(2014, 6, 12),
                        BorrowedBy = "olyboro" };
        SharedBook borrower2 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2014, 7, 17), DateReturned = new DateTime(2014, 7, 21),
                        BorrowedBy = "Terri" };
        SharedBook borrower3 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2015, 1, 19), DateReturned = new DateTime(2015, 2, 2),
                        BorrowedBy = "Whoopi"
        };
        SharedBook borrower4 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2016, 2, 21), DateReturned = new DateTime(2013, 2, 23),
                        BorrowedBy = "Cher"};
        sharedBooks.Add(borrower1);
        sharedBooks.Add(borrower2);
        sharedBooks.Add(borrower3);
        sharedBooks.Add(borrower4);
        // write our list ... there must be 3 or more books in the list for the following code to succeed
        String previous;
        String next;
        Int32  items = sharedBooks.Count;
        Console.WriteLine("     Title                  Previous       ------        Next");
        for (Int32 book = 0; book < items; book++)
        {
            if (book == 0) { previous = "*** in library ***"; next = sharedBooks[book + 1].BorrowedBy;}
            else if (book == items - 1) { previous = sharedBooks[book - 1].BorrowedBy; next = "*** not known ***";}
            else
            {
                previous = sharedBooks[book-1].BorrowedBy;
                next     = sharedBooks[book+1].BorrowedBy;
            }
            Console.WriteLine("{0}     {1}     {2}     {3}",
                                sharedBooks[book].Title,
                                previous,
                                sharedBooks[book].BorrowedBy,
                                next);
        }
    }

    output:

         Title                  Previous       ------        Next
    Animal Farm     *** in library ***     olyboro     Terri
    Animal Farm     olyboro     Terri     Whoopi
    Animal Farm     Terri     Whoopi     Cher
    Animal Farm     Whoopi     Cher     *** not known ***

     #########################################

    olybobo, the code in the video that you mentioned is a special type of list.

    BTW, you wrote    pode ...   node is the correct term.

    STUDY these articles:

    https://msdn.microsoft.com/en-us/library/he2s3bh7(v=vs.110).aspx "LinkedList<T> Class" "Represents a doubly linked list."

    https://msdn.microsoft.com/en-us/library/ahf4c754(v=vs.110).aspx  "LinkedListNode<T> Class"
        "Represents a node in a LinkedList<T>. This class cannot be inherited."

    http://www.dotnetperls.com/linkedlist 

    here is an example of a linked list *

       * you do not need to read the article where i found the above image,
         however you can find it here:  
         http://www.codeofhonor.com/blog/avoiding-game-crashes-related-to-linked-lists

    FWIW, olybobo, linked lists are a computer science topic; in day to day programming, most programmers will not be using linked lists.  Regardless, it is worth understanding how they work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 27, 2016 3:42 PM

All replies

  • User-271186128 posted

    Hi olybobo,

    what does the object keyword mean.

    It is the type of the field.

    I suggest you could refer to the following code:

        public class Test1
        {
            //private: Access Modifiers
            //object: data type.
            //name: field name.
            private object name;
            
            //Test2: the data type is Test2. When you assign value to this field, the data type should be a Test2 type
            public Test2 T2;
     
        }
    
        public class Test2
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
    
           protected void Page_Load(object sender, EventArgs e)
            {
                Test2 t2 = new Test2() { ID = 1001, Name = "AAA" };
    
                Test1 t1 = new Test1();
    
                t1.T2 = t2; //
    
                Response.Write(t1.T2.Name);
            }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2016 7:17 AM
  • User702049738 posted

    Why is it exactly needed. Why is the object type needed in that manner. thank you.

    Friday, February 26, 2016 12:41 PM
  • User-434868552 posted

    @olybobo

    First, i suggest you complete the free Microsoft course "C# Fundamentals for Absolute Beginners" from the the Microsoft Virtual Academy https://mva.microsoft.com/en-US/training-courses/c-fundamentals-for-absolute-beginners-8295

    Regarding your question:

    public class pode
    {
         private object info;
         private pode after;
    }

    olybobo, it is absolutely important that you study the following MSDN article:

    https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx  "Object class

    "System.Object is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy."

    olyboboobject (i prefer to write System.Object) can represent any type the programmer desires.

    objects, classes, and instances are fundamental building blocks of OOP (Object Oriented Programming)

    Study this simple code snippet:

    System.Object anything = null;
    Console.WriteLine(anything);
    Int32 ten = 10;
    anything = ten;
    Console.WriteLine(anything);
    String olybobo = "olybobo";  // System.String is a class -- see MSDN
    anything = olybobo;
    Console.WriteLine(anything);
    Boolean truth = true;
    anything      = truth;
    Console.WriteLine(anything);
    DateTime dateTimeStructure = DateTime.Now; // DateTime is a struct -- see MSDN
    anything = dateTimeStructure;
    Console.WriteLine(anything);

    output:

    null
    10
    olybobo
    True
    2016-02-26 11:46:46

    olybobo, where did you find the code that you have shown to us?  i'm guessing in a book or somewhere else because you would understand that code had you written it.

    BTW, i'm not criticizing you; a c# beginner is someone who has started the very long journey to mastery of c# (i'm still learning myself).

    BACK TO YOUR EXAMPLE:

    the are types like Double, DateTime, String, et cetera

    there are also custom types.

    The class named poole in your example is a custom type.

    poole contains 2  members:  info is an objectafter is a poole.

    The designer of the class poole would be able to tell you why she/he wanted the class poole to be able to contain an instance of itself.

    study this example:

    public class MyCustomType
    {
        public MyCustomType myCustomType1;
        public MyCustomType myCustomType2;
    }
    void Main()
    {
        MyCustomType myCustomType = new MyCustomType();
        myCustomType.myCustomType1 = null;
        myCustomType.myCustomType2 = null; 
        Console.WriteLine(myCustomType);
    }

    output:

    MyCustomType
    
    myCustomType1  null 
    
    myCustomType2  null 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2016 5:07 PM
  • User702049738 posted

    this is where i found the code. it was being used in implementing a linkedlist

    https://www.youtube.com/watch?v=cEsicmlaWDk

    Friday, February 26, 2016 5:58 PM
  • User-434868552 posted

    @olybobo

    caution:  at the YouTube link, there is a follow up link to 

    www  regawmod com ... Tutorial zip

    McAfee's site advisor reports http://www.siteadvisor.com/sites/regawmod.com :

    regawmod.com
    This link is suspicious.
    We tested it and found potential security risks. Be careful.

    Website Category:
    Parked Domain

    YOUR  QUESTION ... 

    Usually, there are features of the .NET Framework that meet most of our needs.

    Sometimes we write code we do not need because we do not understand enough of the .NET Framework.

    study https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx "List<T> Class"

    Here is a trivial example using List<T>:

    public class SharedBook
    {
        public String Title;
        public DateTime DateBorrowed;
        public DateTime DateReturned;
        public String BorrowedBy;
    }

    The task is simply to write a list of borrowers and who had the book before and after each borrower.

    void Main()
    {
        List<SharedBook> sharedBooks = new List<SharedBook>();
        SharedBook borrower1 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2014, 5, 15), DateReturned = new DateTime(2014, 6, 12),
                        BorrowedBy = "olyboro" };
        SharedBook borrower2 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2014, 7, 17), DateReturned = new DateTime(2014, 7, 21),
                        BorrowedBy = "Terri" };
        SharedBook borrower3 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2015, 1, 19), DateReturned = new DateTime(2015, 2, 2),
                        BorrowedBy = "Whoopi"
        };
        SharedBook borrower4 = new SharedBook { Title = "Animal Farm",
                        DateBorrowed = new DateTime(2016, 2, 21), DateReturned = new DateTime(2013, 2, 23),
                        BorrowedBy = "Cher"};
        sharedBooks.Add(borrower1);
        sharedBooks.Add(borrower2);
        sharedBooks.Add(borrower3);
        sharedBooks.Add(borrower4);
        // write our list ... there must be 3 or more books in the list for the following code to succeed
        String previous;
        String next;
        Int32  items = sharedBooks.Count;
        Console.WriteLine("     Title                  Previous       ------        Next");
        for (Int32 book = 0; book < items; book++)
        {
            if (book == 0) { previous = "*** in library ***"; next = sharedBooks[book + 1].BorrowedBy;}
            else if (book == items - 1) { previous = sharedBooks[book - 1].BorrowedBy; next = "*** not known ***";}
            else
            {
                previous = sharedBooks[book-1].BorrowedBy;
                next     = sharedBooks[book+1].BorrowedBy;
            }
            Console.WriteLine("{0}     {1}     {2}     {3}",
                                sharedBooks[book].Title,
                                previous,
                                sharedBooks[book].BorrowedBy,
                                next);
        }
    }

    output:

         Title                  Previous       ------        Next
    Animal Farm     *** in library ***     olyboro     Terri
    Animal Farm     olyboro     Terri     Whoopi
    Animal Farm     Terri     Whoopi     Cher
    Animal Farm     Whoopi     Cher     *** not known ***

     #########################################

    olybobo, the code in the video that you mentioned is a special type of list.

    BTW, you wrote    pode ...   node is the correct term.

    STUDY these articles:

    https://msdn.microsoft.com/en-us/library/he2s3bh7(v=vs.110).aspx "LinkedList<T> Class" "Represents a doubly linked list."

    https://msdn.microsoft.com/en-us/library/ahf4c754(v=vs.110).aspx  "LinkedListNode<T> Class"
        "Represents a node in a LinkedList<T>. This class cannot be inherited."

    http://www.dotnetperls.com/linkedlist 

    here is an example of a linked list *

       * you do not need to read the article where i found the above image,
         however you can find it here:  
         http://www.codeofhonor.com/blog/avoiding-game-crashes-related-to-linked-lists

    FWIW, olybobo, linked lists are a computer science topic; in day to day programming, most programmers will not be using linked lists.  Regardless, it is worth understanding how they work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 27, 2016 3:42 PM
  • User702049738 posted

    Thanks for the caution. I am going to remove the youtube link right now. thanks for the explanation.  I am just trying to learn and understand linkedlist from the C# perspective. I only modified it from a node to pode just for a better reflection and to fully understand why it was done in that manner....

    Saturday, February 27, 2016 6:31 PM