Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expose Class Methods and Properties without instantiating 1

Status
Not open for further replies.

Neld

Programmer
Jul 2, 2012
5
CA
Hi,

I am new to C# and trying to do some testing.

I created a Class Library and compiled it to a DLL. The DLL replaces unwanted characters from a Telephone number. e.g. When a user enters a telephone number (+1(123)-456-7890) in a textbox and on event onTextBox_TextChange() the telephone number gets cleaned to 11234567890.

The code below is from the Class Library:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Windows.Forms;


namespace Common.DLL
{
    public class Telephone

    {

        public string FormattedAsIs { get; set; }

        private string m_Telephone = string.Empty;

        public string Formatted

        {
            get { return m_Telephone; }

            set

            {

                m_Telephone = CleanTelephone(value);

            }
        }

        public string CleanTelephone(string StrValue)

        {

            string pattern = @"[^\d]";
            string replacement = string.Empty;
            Regex rgx = new Regex(pattern);

            StrValue = rgx.Replace(StrValue, replacement);

            return StrValue;
        }
    }
}

The Code that will use the DLL:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Common.DLL;

namespace Practice
{
    static class Program

    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            //The telephone functionalities...

            //When instantiating the Telephone Object the Properties and Methods are exposed.
            //*******************************************************************************

            Telephone T = new Telephone();

            T.FormattedAsIs = "12345";

            T.Formatted = "a9b8c7654#3@2!1";

            MessageBox.Show (T.FormattedAsIs + ' ' + '*' + ' ' + T.Formatted);


            //When accessing the Telephone Object the Properties and Methods are not exposed except equals and referenceequals.
            //*****************************************************************************************************************

            Telephone.



            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            
        }
   }
}

The question i'd like to ask is when I instantiate the Telephone object, all the properties and methods are exposed and I can use them.

But when I try accessing the "Telephone." object directly, it only shows 2 methods (Equals and ReferenceEquals). I'd like to expose all the methofs and properties without instantiating the Telephone objects. Can this be done? How?

Please help.

Thanks

Neld
 
By using Class. (Telephone.) you are only looking at static methods and you have none?

Age is a consequence of experience
 
That is correct... you need static methods in order to access them without an instance object.


Code:
public class Telephone
    {
        public static string CleanTelephone(string StrValue)
        {
            string pattern = @"[^\d]";
            string replacement = string.Empty;
            Regex rgx = new Regex(pattern);

            StrValue = rgx.Replace(StrValue, replacement);

            return StrValue;
        }
    }

I think it can then be used the way you're looking for.

Code:
            string formattedPhone = Telephone.CleanTelephone("a9b8c7654#3@2!1");

            Console.WriteLine(formattedPhone);
            Console.ReadLine();

However, just for something to think about (since you've claimed that you're new at C# and learning is always a good thing)...

If you're just looking to strip non-numeric characters from a string - it doesn't have anything specifically to do with a phone number. What if you extended the string class to give you a numeric only representation? Then used the Telephone class to give you both a formatted (for display) and a numeric only (for database) representation of the phone number?

String class extension:
Code:
    public static class StringExtensions
    {
        public static string ToNumericOnly(this string str)
        {
            string pattern = @"[^\d]";
            string replacement = string.Empty;
            Regex rgx = new Regex(pattern);

            return rgx.Replace(str, replacement);
        }
    }

You can now use it with any string by adding .ToNumericOnly() to the string (see example below)

Now modify the Telephone class to use the extension.

Code:
    public class Telephone
    {
        public Telephone() { }

        public Telephone(string phoneNumber)
        {
            this.PhoneNumber = phoneNumber;
        }

        public string PhoneNumber { get; set; }

        public string NumericOnlyPhoneNumber
        {
            get
            {
                return this.PhoneNumber.ToNumericOnly();
            }
        }

        public string FormattedPhoneNumber
        {
            get
            {
                string result = this.NumericOnlyPhoneNumber;

                if (!string.IsNullOrEmpty(result))
                {
                    long filteredNumber = System.Convert.ToInt64(result);

                    switch (result.Length)
                    {
                        case 11:
                            result = string.Format("{0:+# (###) ###-####}", filteredNumber);
                            break;
                        case 10:
                            result = string.Format("{0:(###) ###-####}", filteredNumber);
                            break;
                        case 7:
                            result = string.Format("{0:###-####}", filteredNumber);
                            break;
                    }
                }
                

                return result;
            }
        }
    }

You can now use the Telephone class (as an instance class) to handle your phone number needs.

Code:
            Telephone phone = new Telephone();
            phone.PhoneNumber = "11a9b8c7654#3@2!1";

            Console.WriteLine(string.Format("stripped number: {0}", phone.NumericOnlyPhoneNumber));
            Console.WriteLine(string.Format("formatted number: {0}", phone.FormattedPhoneNumber));

            //this is more likely the input you're expecting from your form
            Telephone altPhone = new Telephone("+1(123)-456-7890");

            Console.WriteLine(string.Format("stripped number: {0}", altPhone.NumericOnlyPhoneNumber));
            Console.WriteLine(string.Format("formatted number: {0}", altPhone.FormattedPhoneNumber));

            Console.ReadLine();

Sorry if I got a bit wordy. This isn't necessarily the "best" solution... just some concepts to think about and look up. Good luck on your journey in C#!


-Mark




 
Nice answer!

/Daddy

-----------------------------------------------------
Helping people is my job...
 
Thank you very much guys. Mark thank you for the very detailed response and I now have a more better understanding of what I am trying to accomplish. cheers...
 
@Neld: you thank Mark by giving his post a start :)

/Daddy

-----------------------------------------------------
Helping people is my job...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top