Reputation: 599
First of all I was unsure how to name this topic. I want to have a class with fields, and I would like to have some of them to have some Properties
, Fields
. Maybe I will show some of my code and explain it a bit.
class Column
{
public string Source { }
public string Destination { }
}
I want both "Source" and "Destination" to have inside them more 'fields'. For example "Name", "Type" etc.
I will read some text like:
Source home(1) type[3] Destination NewYork(3) Seattle[2]
I would like to be able to distinguish which part of my text to put inside "Source" and which inside "Destination". How should I do it?
I know that it may not be 100% clear, but I did my best to write it as simple as possible.
Upvotes: 0
Views: 100
Reputation: 63327
I know it's too late, but this is a full code that can help you, give it a try if you like.
class Column
{
string plainText;
Regex reg = new Regex(@"(Source\b\w+\b\w+\b)(Destination\b\w+\b\w+\b)");
public Source Source {
get {
Match m = reg.Match(plainText);
Group source = m.Groups[0];
string[] s = source.Value.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);
return m.Success ? new Source(new string[]{s[1],s[2]}) : null;
}
set {
Match m = reg.Match(plainText);
Group dest = m.Groups[1];
if(m.Success) plainText = value + " " + dest.Value;
}
}
public Destination Destination {
get {
Match m = reg.Match(plainText);
Group dest = m.Groups[1];
string[] s = dest.Value.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);
return m.Success ? new Destination(new string[]{s[1], s[2]}) : null;
}
set {
Match m = reg.Match(plainText);
Group source = m.Groups[0];
if(m.Success) plainText = source.Value + " " + value;
}
}
//This is used to change the plainText
//for example: SetPlainText("Source home(1) type[3] Destination NewYork(3) Seattle[2]");
public void SetPlainText(string txt){
plainText = txt;
}
}
public class Source
{
public Source(string[] s){
Name = s[0];
Type = s[1];
}
public string Name { get; set; }
public string Type { get; set; }
public override string ToString(){
return string.Format("Source {0} {1}",Name,Type);
}
}
public class Destination
{
public Destination(string[] s){
Name = s[0];
Type = s[1];
}
public string Name { get; set; }
public string Type { get; set; }
public override string ToString(){
return string.Format("Destination {0} {1}",Name,Type);
}
}
Upvotes: 1
Reputation: 32681
I want both "Source" and "Destination" to have inside them more 'fields'
You cannot do it like this as you have specified that they have type string.
If you feel that they should have some fields in them then consider making them objects like
public class Source
{
public string Name{get;set;}
public string Type{get;set;}
}
then you can edit your column class like this
public class Column
{
public Source Source { get;set;}
}
You can do the same for your other class as well
Upvotes: 1
Reputation: 391446
You need to define types and return those from those properties:
public class Column
{
private Source _Source = new Source();
private Destination _Destination = new Destination();
public Source Source { get { return _Source; } }
public Destination Destination { get { return _Destination; } }
}
public class Source
{
public string Name { get; set; }
public string Type { get; set; }
}
public class Destination
{
public string Name { get; set; }
public string Type { get; set; }
}
Upvotes: 4