none
Object instantiation and Method Call RRS feed

  • Question

  • My name is Tebatso and i am a beginner in c# programming.

    My question is, how can i call a method of an Object  in the local/Predefined Method without instantiating another Object.

    Friday, February 7, 2020 1:16 PM

All replies

  • When focusing on "without instantiating", then you can call any static method. E.g.

    namespace ConsoleCS
    {
        using System;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
        }
    }

    Console.WriteLine() and Console.ReadLine() are methods of the Console class and you can call them without instantiating them

    Friday, February 7, 2020 1:41 PM
  • Also under the right circumstances a Singleton class is an option.

    Example

    Usage when the class below is in scope.

    var result = ReferenceIncrementer.Instance.GetReferenceValue();

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Subject
    {
        /// <summary>
        /// Thread safe singleton responsible for creating
        /// unique sequence for email subject.
        /// </summary>
        public sealed class ReferenceIncrementer
        {
            private static readonly Lazy<ReferenceIncrementer> Lazy =
                new Lazy<ReferenceIncrementer>(() =>
                    new ReferenceIncrementer());
    
            public static ReferenceIncrementer Instance => Lazy.Value;
    
            private List<int> _baseList = new List<int>();
    
            /// <summary>
            /// Populate HashSet with random numbers.
            /// HastSet items are unique.
            /// </summary>
            private void CreateList()
            {
                _baseList = new List<int>();
    
                for (var index = 1; index < 9000; index++)
                {
                    _baseList.Add(index);
                }
            }
            /// <summary>
            /// Return a left padded number prefix with REF: 0001
            /// .Any ask if there are any values when called.
            /// </summary>
            /// <returns></returns>
            public string GetReferenceValue()
            {
                if (!_baseList.Any())
                {
                    CreateList();
                }
    
                var number = _baseList.FirstOrDefault();
                _baseList.Remove(number);
    
                return $" REF: {number:D4}";
    
            }
    
            /// <summary>
            /// Instantiate List
            /// </summary>
            private ReferenceIncrementer()
            {
                CreateList();
            }
            /// <summary>
            /// Used to reset at a given time e.g. right before midnight,
            /// perhaps by a scheduled job.
            /// </summary>
            public void Reset()
            {
                CreateList();
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, February 7, 2020 2:47 PM
    Moderator
  • Hi Tebatso,

    Thank you for posting here.

    It seems that you want to call a method without instantiating an object.

    You can use the static method, like the following code.

        class Program
        {
            static void Main(string[] args)
            {
                SayHello("Jack");
                Console.ReadKey();
            }
    
            static void SayHello(string name)
            {
                Console.WriteLine("Hello, I am {0}",name);
            }
        }

    Result:

    Hope my code could help you.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 10, 2020 7:21 AM
    Moderator