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:
The Code that will use the DLL:
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
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
Age is a consequence of experience
RE: Expose Class Methods and Properties without instantiating
CODE --> C#
I think it can then be used the way you're looking for.
CODE --> C#
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
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
You can now use the Telephone class (as an instance class) to handle your phone number needs.
CODE --> program
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
/Daddy
-----------------------------------------------------
Helping people is my job...
RE: Expose Class Methods and Properties without instantiating
RE: Expose Class Methods and Properties without instantiating
/Daddy
-----------------------------------------------------
Helping people is my job...