This is my first post at this site and I’m really delighted with this community.
I’m creating a generator email system. This consists to get student’s name, generate some patterns, check out if the email address is available and finally add it to the database.
For instance, my first name is “Oscar Daniel”, and my last name is “Fimbres Puente”. The system will have to generate odfimbresp@domain.com (Actually it’ll generate many patterns, this is only one of them).
I’ve got a class named Person, the constructor receives a first and last name. To generate the pattern. Like this:
public class Person
{
public string FirstName { get; set; }
public string LastName1 { get; set; }
public string LastName2 { get; set; }
public string Email { get; set; }
public string[] FirstName_Array { get; set; }
public string[] LastName1_Array { get; set; }
public string[] LastName2_Array { get; set; }
public Person(string firstName, string lastName1, string lastName2)
{
...
// it is necessary to split each string in an array
FirstName_Array = SplitName(firstName);
LastName1_Array = SplitName(lastName1);
LastName2_Array = SplitName(lastName2);
}
}
As you can see above, I need to split each string using the space like separator. For example, the properties will be as follows:
First Name: Oscar Daniel
First Name (Array): { Oscar, Daniel }
Therefore, my question would be: Is there a way that I can delete the property array? Because I think I’m being redundant. I was trying to add a function named ToArray() only for them, but I cannot do that.
Any questions or doubts please let me know.
Just to throw this out there. I think you have a Person class with properties such as firstname, last name etc etc. And you need some actions taken on person to generate some patterns such as firstletter of firstname array + first 4 letters of lastname + some pattern@domain.com.
I believe you are trying to avoid the redundant array properties. If so, why don’t you have a method for each pattern something like,
This may be too simple and of course, you have to recalculate the pattern everytime, but it is simple and readable. It all depends on how you are going to use it.
You can even use regular expressions to extract the data you want from different properties. If you have specific rules, it is easy to write them. Just as an introduction, here is some info on regex.
But to answer the question in your title, yes, string property (or any object) can have an extension method as say, ToArray() (any name you want). See here.