Generate Property

S

shapper

Hello,

I have the following class:

public class XmlSource {
public String Path { get; set; }
public XDocument Context { get; set; }

public XmlSource() { } // XmlSource

public XmlSource(String path) {
Path = path;
if (Context != null) Context = XDocument.Load(Path);
} // XmlSource

} // XmlSource

The problem is that if I define the Path not using the constructor I
will not have the Context defined.

Basically the functionality I would like for this class would be to be
able to define a Path and automatically the Context would be
created ... And it don't need to change unless Path is redefined.

Context is only supposed to be read by other classes so I can perform
queries with the file.

What would be the correct way to do this?

Does this make any sense?

Thanks,
Miguel
 
F

Family Tree Mike

shapper said:
Hello,

I have the following class:

public class XmlSource {
public String Path { get; set; }
public XDocument Context { get; set; }

public XmlSource() { } // XmlSource

public XmlSource(String path) {
Path = path;
if (Context != null) Context = XDocument.Load(Path);
} // XmlSource

} // XmlSource

The problem is that if I define the Path not using the constructor I
will not have the Context defined.

Basically the functionality I would like for this class would be to be
able to define a Path and automatically the Context would be
created ... And it don't need to change unless Path is redefined.

Context is only supposed to be read by other classes so I can perform
queries with the file.

What would be the correct way to do this?

Does this make any sense?

Thanks,
Miguel

I think you want the following:

public class XmlSource
{
private string _Path;
public String Path
{
get { return _Path; }
set { _Path = value; Context = XDocument.Load(Path); }
}
public XDocument Context { get; private set; }

public XmlSource() { } // XmlSource

public XmlSource(String path)
{
Path = path;
// how would the folllowing ever not be null?
if (Context != null) Context = XDocument.Load(Path);
} // XmlSource
} // XmlSource
 
S

shapper

  public XmlSource(String path)
  {
    Path = path;
    // how would the folllowing ever not be null?
    if (Context != null) Context = XDocument.Load(Path);
  } // XmlSource

Thank You!

The null part was from a past implementation and I didn't realized
that I didn't deleted it.

Thank You for the tip and for the help.
 
P

Peter Duniho

I think you want the following:

public class XmlSource
{
private string _Path;
public String Path
{
get { return _Path; }
set { _Path = value; Context = XDocument.Load(Path); }
}
public XDocument Context { get; private set; }

public XmlSource() { } // XmlSource

public XmlSource(String path)
{
Path = path;
// how would the folllowing ever not be null?
if (Context != null) Context = XDocument.Load(Path);
} // XmlSource
} // XmlSource

Actually, the following would suffice:

public class XmlSource
{
private string _Path;
public String Path
{
get { return _Path; }
set { _Path = value; Context = XDocument.Load(Path); }
}
public XDocument Context { get; private set; }

public XmlSource() { } // XmlSource

public XmlSource(String path)
{
Path = path;
} // XmlSource
} // XmlSource

With the change to the Path property setter, there's no need to do the
same load in the constructor; setting the property in the constructor will
accomplish that. (It's more maintainable too, since there's no duplicated
code...copy/paste is a terrible way to implement a class :) ).

One last comment: note that if the class really makes no sense without a
path, you may want to simply not even provide the parameterless
constructor. Just require the client to always provide the path when
creating the object.

Pete
 

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

Similar Threads


Top