×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Expose Class Methods and Properties without instantiating

Expose Class Methods and Properties without instantiating

Expose Class Methods and Properties without instantiating

(OP)
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

RE: Expose Class Methods and Properties without instantiating

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

Age is a consequence of experience

RE: Expose Class Methods and Properties without instantiating

That is correct... you need static methods in order to access them without an instance object.


CODE --> C#

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 --> C#

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 --> StringExtensions.cs

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 --> Telephone.cs

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 --> program

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




RE: Expose Class Methods and Properties without instantiating

Nice answer!

/Daddy

-----------------------------------------------------
Helping people is my job...

RE: Expose Class Methods and Properties without instantiating

(OP)
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...

RE: Expose Class Methods and Properties without instantiating

@Neld: you thank Mark by giving his post a start :)

/Daddy

-----------------------------------------------------
Helping people is my job...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close