Setting property value to string or accessing property methods

  • Thread starter Thread starter Brian Mitchell
  • Start date Start date
B

Brian Mitchell

Hello, I am trying to create a property that can be set to a string or give
the user a list of methods inside that property.



For instance, I have the property -Location-. I would like the option of
either saying something.Location="Houston" OR something.Location.GetLocation
(which sets a private variable) . Is there a way of doing this?



Thanks!!
 
Hello, I am trying to create a property that can be set to a string or give
the user a list of methods inside that property.



For instance, I have the property -Location-. I would like the option of
either saying something.Location="Houston" OR something.Location.GetLocation
(which sets a private variable) . Is there a way of doing this?

There is no way to do that in the current VB. That would require an
implicit conversion operator defined for your Location object property.
It can however be done in C# (and probably VB.NET 2005 - I'd have to
check):

using System;

public class Location
{
private string location;

public string GetLocation ()
{
return this.location;
}

public static implicit operator Location (string theString)
{
Location loc = new Location ();
loc.location = theString;
return loc;
}
}

public class Something
{
private Location location = new Location ();

public Location Location
{
get
{
return this.location;
}
set
{
this.location = value;
}
}

public static void Main ()
{
Something something = new Something ();

something.Location = "Huston";
Console.WriteLine (something.Location.GetLocation ());
}
}

Very simple example - not something I would normally recommend doing...
 
Thanks for the info, I'll just stick to keeping my properties and methods
seperate for now then.
 

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