OOP question - multiple value properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to write a class that has multiple values associate to it

public class Person {
public string FirstName {get; set;}
public string LastName {get; set;}
public DateTime Date {get; set;}
}

For example, I want to create an object like this:

Person p = new Person();
p.FirstName = "John Smith";
p.FirstName.Description = "First Name Text";
p.FirstName.IsUsed = true;

How do I write it? do I need to write a wrapper class for "string" and
"DateTime"?
 
Hi,

That will not compile, try something like this instead:

public struct Field
{
string Value;
string Description;
bool isUsed;
}
public class Person
{
Field _firstname;
public Field FirstName{ get; set ; }
}

then you can do:
Person p = new Person();
p.FirstName.Value = "John Smith";
p.FirstName.Description = "First Name Text";
p.FirstName.IsUsed = true;



Cheers,
 
In this case, you will have to create an object that you want to expose
through the FirstName, LastName, etc, etc etc properties. You don't want to
expose a string, but rather, an object that has these properties that you
wish to expose.

As a result, you would have to declare the properties as getting/setting
types of this object.

Hope this helps.
 
With 2.0, you could create a generic type, which you would use to expose
the value as that type. Other than that, the only options you have now are
to expose it as an object (and force a cast), or create distinct types.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top