I want do something like that:
public class MyClass
{
public String varA{ get; set; }
public String[] varB{ get; set; }
//.....
public ?? string ToHtml()
{
//return HTML value
}
}
public class Run()
{
MyClass c = new Myclass();
c.varA = "Toto";
c.varB = new string[] { "foo", "bar" };
string a = c.varA.ToHtml() // -> "<p>Toto</p>";
string b = c.varB.ToHtml() // -> "<ul><li>foo</li><li>bar</li></ul>";
}
How can do that ?
Edit: I have change the Run()
Create an extension method on the String class, which returns a HTML representation of that string (or whatever ToHtml() should do).
public static class StringExtensions
{
public static string ToHtml ( this string target )
{
// TODO :
}
}
This is a way to implement your scenario with extension methods. While, as others have noted, it would make sense to keep the logic to turn your strings to HTML within MyClass, in certain scenarios it might make sense to use this approach.
public class MyClass
{
public String varA{ get; set; }
public String[] varB{ get; set; }
}
public static class MyExtensions {
public static string ToHtml(this string input, string format)
{
return string.Format(format, input);
}
public static string ToHtml(this string input)
{
return ToHtml(input,"<p>{0}</p>");
}
public static string ToHtml(this IEnumerable<string> input, string wrapperformat, string elementformat)
{
string body= input
.Select(s => string.Format(elementformat,s))
.Aggregate((a,b)=> a+b);
return string.Format(wrapperformat,body);
}
public static string ToHtml(this IEnumerable<string> input)
{
return ToHtml(input,"<ul>{0}</ul>","<li>{0}</li>");
}
}
Unless you define ToHtml() extension methods for both strings and arrays, you can't call it that way (on the fields themselves). See the other answers for how to implement extension methods.
A simpler alternative that applies to just MyClass is to make two overloads of your method which accept a string and a string array as arguments respectively, then pass them:
public string ToHtml(string arg)
{
//return HTML value
}
public string ToHtml(string[] argAsArray)
{
//return HTML value
}
Then call them like this:
string a = c.ToHtml(c.varA);
string b = c.ToHtml(c.varB);
What you're trying to do is add an helper method to the string class. It's called an extension method and it must respect certains rules :
It must use this on the first parameter
It must be static
It must be in a static class
.
public static class HtmlStringHelper
{
public static string ToHtml(this string s)
{
// Add you logic here
return (s);
}
}
i suppose you could use an extension method (for both string and string[] for that), but then you would not need the ToHtml() method of MyClass (as the logic would reside in the extension methods).
edit: i should note that calling member methods usually is considered a bad practice. Without more information its hard to imagine what MyClass is supposed to do/be, but you might want to keep control of class members in the class. So another way would be to create a ToHtmlA() and ToHtmlB() method for MyClass.
If you changed ToHtml to except a value:
public static string ToHtml(string a)
{
// your code here - obviously not returning ""
return "";
}
then you can call it like so:
MyClass c = new MyClass();
c.varA = "some text";
c.varA = MyClass.ToHtml(c.varA);
But, I maybe WAY off what you require.
Related
How can I make my method have defined list of values that can be passed. I've seen this in VB.net, but can't find it in C#, and it dosen't look like enum.
class Test
{
List { active, all, completed}
public string get(string a, List b)
{
// some code
}
string a = get("foo", active);
string b = get("foo", all);
}
If enum is called test, I need to pass test.active and I don't want that. I need to pass only active
What you can do is use the using static keyword when you want to use the enum, then you can just use the word you want.
namespace Foo
{
class Test
{
public string get(string a, List b)
{
// some code
}
}
public enum List { active, all, completed}
}
used like
using Foo;
using static Foo.List;
public void Example()
{
var test = new Test();
//Because of "using static Foo.List;" you don't need to use "List.active"
string a = test.get("foo", active);
string b = test.get("foo", all);
}
have tried some searches. Probably my lack of knowledge that I'm not using the right search terms or perhaps just not understanding the answers.
I have a method that is being passed an object, which I want to output a particular value to a text file.
I already know the object will be a List< someClass > of a few different possible classes (customers/employees/items etc). But all of the classes contain the same string property (e.g.) string idNumber.
So something like this:
public static void OutputFile(object myInput)
{
foreach (someGenericObject in (List<anyType>)myInput)
{
string textToOutput = someGenericObject.idNUmber;
//output the text to somewhere else here
}
}
I feel like as long as I know that it will always contain a this "idNumber" property regardless of the type, that I should be able to reference it somehow. But I just can't seem to get my head around it?
The error I typically get is something like:
Cannot cast List< Employee > to List< object > etc.
Thanks in advance
As I suggested in the comments, if you have the ability to modify these classes, you can have them all inherit from an interface IHasIdNumber with an idNumber property.
Your method would then become:
public static void OutputFile(IEnumerable<IHasIdNumber> myInput)
{
foreach (var item in myInput)
{
string textToOutput = item.idNUmber;
//output the text to somewhere else here
}
}
There are a few ways you can solve this.
Recommended way: Implement common interface:
public interface INumberable { // I'm sure you can come up with a better name...
string IDNumber { get; set; }
}
And then all the possible classes that can be passed into the method will implement INumberable. Your method can then look like this:
public static void OutputFile(List<INumerable> myInput)
{
foreach (var someGenericObject in myInput)
{
string textToOutput = someGenericObject.idNUmber;
//output the text to somewhere else here
}
}
Not-so-recommended way: Reflection:
Type t = someGenericObject.GetType();
var p = t.GetProperty("idNumber");
string theStringYouWant = (string)p.GetValue(someGenericObject);
Note that this is not very safe.
You can use [dynamic].
foreach (var someGenericObject in (dynamic)myInput)
{
//...
}
If all your classes have the same property you want to access in foreach loop you can do in via interface.
public interface ISmth {
int MyProperty { get; set; }
}
public class Student : ISmth {
public int MyProperty { get; set; }
}
public class Employee : ISmth {
public int MyProperty { get; set; }
}
public static void DoSmth(object myObj) {
foreach(ISmth item in (List<object>)myObj) {
Console.Write(item.MyProperty);
}
}
List<Student> stdList = new List<Student>();
DoSmth(stdList.Cast<object>().ToList());
I have a class with only string members like this :
public class MyClass
{
public string MyProp1 { get; set; }
public string MyProp2 { get; set; }
}
I create an instance :
Var myClass = new MyClass();
Later in the code, I’d like to know if all the member (MyProp1 and MyProp2) are not null or empty. I know I can use a if of course but there is much more properties than 2 in my real code.
Is there a way to do this ?
Thanks,
Using a dictionary based store for your properties is probably the easiest way of doing this:
public class MyClass
{
private IDictionary<String, String> _store;
public MyClass()
{
_store = new Dictionary<String, String>();
}
public string MyProp1 {
get { return GetOrDefault("MyProp1"); }
set { _store["MyProp1"] = value; }
}
public string MyProp2 {
get { return GetOrDefault("MyProp2"); }
set { _store["MyProp2"] = value; }
}
public Boolean HasData()
{
return _store.Any(x => !String.IsNullOrWhiteSpace(x.Value));
}
public Boolean IsEmpty()
{
return _store.All(x => String.IsNullOrWhiteSpace(x.Value));
}
private String GetOrDefault(String propertyName)
{
if (_store.ContainsKey(propertyName))
{
return _store[propertyName];
}
return String.Empty;
}
}
Another method for doing this would be to compare it with a default instance:
public class MyClass
{
public string MyProp1 { get; set; }
public string MyProp2 { get; set; }
public static readonly MyClass Empty = new MyClass();
public Boolean HasData()
{
return !Empty.Equals(this);
}
public Boolean IsEmpty()
{
return Empty.Equals(this);
}
}
You can try to use the reflect to check the properties. You should need confirm that all the properties are public, and the type is string. Here is the code.
public static bool IsNullOrEmpty(MyClass prop)
{
bool result = true;
PropertyInfo[] ps = prop.GetType().GetProperties();
foreach (PropertyInfo pi in ps)
{
string value = pi.GetValue(prop, null).ToString();
if (string.IsNullOrEmpty(value))
{
result = false;
break;
}
}
return result;
}
To check if your class contains 'any' properties which are null:
System.Reflection.PropertyInfo[] properties = myClass.GetType().GetProperties
(BindingFlags.Public | BindingFlags.Instance);
bool hasNullProperty = properties.Any(y => y.GetValue(x, null) == null);
You can always initialize your class like
public class MyClass
{
public MyClass() {
this.MyProp1 = this.MyProp2 = String.Empty;
}
public string MyProp1 { get; set; }
public string MyProp2 { get; set; }
}
and, unless your programmatically assign a null value to it, the new MyClass() will always have String.Empty in their 2 properties...
from comment:
What I do in those cases is call a helper, for example: string name = myHelper.CheckNode(xmlNode); and in that helper I check if it's null, any other check, I can easily tweek the helper method and it will be available to all elements, and you can extend it to support not only strings but all other data types as well
So, imagine that you are reading nodes from your XML, you write them like:
string name = myHelper.CheckNode(node);
in your helper, you could have something like:
XmlNodeList datasourceNodes = rootNode.SelectNodes("dataSources/dataSource");
foreach (XmlNode datasourceNode in datasourceNodes)
{
DataSource dataSource = new DataSource();
dataSource.Name = myHelper.CheckAttr(datasourceNode.Attributes["name"]);
dataSource.ODBC = myHelper.CheckNode(datasourceNode.SelectSingleNode("odbc"));
// or a variant (Extension Method)
dataSource.UID = datasourceNode.CheckNode("user");
dataSource.PWD = datasourceNode.CheckAttr("password");
ds.Add(dataSource);
}
your helper then could have a method like:
public static string CheckAttr(XmlAttribute attr)
{
return attr == null ? "" : attr.Value.Trim();
}
public static string CheckNode(XmlNode node)
{
return node == null ? "" : node.InnerText.Trim();
}
or for the variant (Extension Method)
public static string CheckAttr(this XmlNode, string attrName)
{
return attrName[attrName] == null ? "" : attrName[attrName].Value.Trim();
}
public static string CheckNode(this XmlNode, string nodeName)
{
return node.SelectSingleNode(nodeName) == null ?
"" :
node.SelectSingleNode(nodeName).InnerText.Trim();
}
If there are many properties in the class, one way of handling this is storing them in a collection, such as an array or a dictionary, instead of declaring each property as a separate member of the class.
Then you can access data in the dictionary by key, which is as easy as accessing a property of a class. And the advantage is that you can loop over the dictionary and check all the properties in a loop.
I would suggest creating a function in your class where you check String.IsNullOrEmpty(MyProp1) etc. for all your properties. This way you at least have gathered all the ckecking functionality in a single place. And you only have this place to modify whenever you add new properties.
To check if all the elements are set you could add a IsEmpty() method to your class that would check the internal properties. Then you wouldn't have to duplicate the if statements everywhere trough your code.
In your IsEmpty() method you can use a regular if statement to check all the fields or you can use reflection to automaticaly retrieve all string properties and check their values. The performance of reflection will be worse then a normal if check but if that's not a problem you can reuse the reflection code in all your entities to check their values.
By using Attribute Base programming you can achieve this. In this approach you will need to place attribute over the class member, and validation is can be done. You can also use Microsoft Enterprise Library for this.
Probably the best way would be to :
Restructure your properties in the form of a Dictionary of strings. Loop through the dictionary to test the strings using string.IsNullOrEmpty(). You could replace the N getter/setters by a single Indexer property which sets and retrieves the strings directly from the dictionary based on a key
You can use try the following style. I haven't tried it before but you might see if it helps
If (String.IsNullOrEmpty(string1 && string2 && string3))
I need to access a property by an index or something similar. The reason why is explained in this already answered question. That answer uses Linq and I prefer something without that dependency. I have no control over the class.
public class myClass
{
private string s = "some string";
public string S
{
get { return s; }
}
}
class Program
{
static void Main(string[] args)
{
myClass c = new myClass();
// I would like something similar
// or same functionality
string s = c["S"];
}
}
As you have no control over the class you can use extension method and reflection to get property value by name:
static class ObjectExtensions
{
public static TResult Get<TResult>(this object #this, string propertyName)
{
return (TResult)#this.GetType().GetProperty(propertyName).GetValue(#this, null);
}
}
Usage:
class A
{
public string Z
{
get;
set;
}
public int X
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
A obj = new A();
obj.Z = "aaa";
obj.X = 15;
Console.WriteLine(obj.Get<string>("Z"));
Console.WriteLine(obj.Get<int>("X"));
Console.ReadLine();
}
}
use (EDIT - as per comment):
string s = c.GetType().GetProperty ("S").GetGetMethod().Invoke (c, null).ToString();
It gives you the value of the (public) property named S of the the instance c regardless of the type of c and doesn't use LINQ at all although I must admit that I don't see why LINQ should be a problem...
You can achieve the same thing by using a default property on your class and a collection. Provided that you will always want strings, you could use the Dictionary class as your default property.
Then in the constructor you could intialize myDictionary["s"] = "some string";
You could then use the myClass as a collection, so myClass["s"] would return "some string".
Reflection is usually an indicator that you haven't created an API to do the job you need, if you have the code to modify then I recommend you use the default property.
See this MSDN article:
Is there a way to specify in an interface a known return type, but unknown number/type of parameters.
The reason I am asking is that I am using Windows Azure Table Storage and each table will have different Partition and Row keys with different input values.
I am creating a ITableOperations interface the code will be something like:
interface ITableOperations<T>
where T : Azure.AzureTableEntity
{
// Key specification
string PartitionKey(/* ? What should go here */);
// Key specification
string RowKey(/* ? What should go here */);
}
And the item table... For another table, the input params would be different
public class ScheduledItem : ITableOperations<ScheduledPostEntity>
{
public string PartitionKey(Guid userGuid)
{
return userGuid.ToString();
}
public string RowKey(DateTime dateScheduled)
{
return dateScheduled.ReverseTicks();
}
}
You could try having a very generic interface. For example:
interface ITableOperations<T, P, R>
where T : Azure.AzureTableEntity
{
string PartitionKey(P partitionKey);
string RowKey(R rowKey);
}
Then your implementation could be:
public class ScheduledItem : ITableOperations<ScheduledPostEntity, Guid, DateTime>
{
public string PartitionKey(Guid userGuid)
{
return userGuid.ToString();
}
public string RowKey(DateTime dateScheduled)
{
return dateScheduled.ReverseTicks();
}
}
EDIT:
Looking at some of your comments since I originally wrote this answer, you could come at it from a different angle. The PartitionKey and RowKey won't change on your object once it has been created, so I'd almost take these particular functions out of this class and move it to the constructors of the classes that inherit from AzureTableEntity. e.g.
public class ScheduledPostEntity : Azure.AzureTableEntity
{
private Guid _userGuid;
private DateTime _dateScheduled;
public ScheduledPostEntity()
{
// Needed for deserialisation from Azure Table Storage
}
public ScheduledPostEntity(Guid userGuid, DateTime dateScheduled)
{
_userGuid = userGuid;
_dateScheduled = dateScheduled;
}
public string PartitionKey
{
get { return _userGuid.ToString(); }
set { _userGuid = Guid.Parse(value); }
}
public string RowKey
{
get { return _dateScheduled.ReverseTicks(); }
set { _dateScheduled = value.FromReverseTicks(); }
}
// These are functions to avoid them being saved as additional properties
// in Azure Table Storage. Sometimes you can get away with them being
// read only properties, but it depends on the type.
public DateTime DateScheduled()
{
return _dateScheduled;
}
public Guid UserGuid()
{
return _userGuid;
}
}
This has the advantage that whenever you create on of these objects, you know minimum requirements to save the object. It also stops you from messing with things that will change your PK and RK.
C# supports multiple parameter in the form of an array by using the params keyword.
You could do this:
interface ITableOperations<T>
where T : Azure.AzureTableEntity
{
// Key specification
string PartitionKey(params object[] data);
// Key specification
string RowKey(params object[] data);
}
If you already know the alternatives of parameters, then you can use overload.
Lets say you have a method that can either receive a string or a Guid or both, you could do this:
string PartitionKey(Guid guid);
string PartitionKey(string str);
string PartitionKey(Guid guid, string str);
If you are using C# 4, then you can use optional parameters:
string PartitionKey(Guid guid = default(Guid), string str = null);
You could define one parameter, which would be an array. This array would contain name/value pairs and could have as many as you need. I think this would give you the flexibility you're looking for.
This still won't show you the proper list of parameters for DoStuff (you'll just see params object[]) but it's about as flexible as you'll get. Note that I've implemented the method explicitly in the implementing class so you don't see it in Intellisense if "foo" is declared as a Foo rather than an IFoo.
class Program
{
static void Main(string[] args)
{
IFoo foo = new Foo();
foo.DoStuff(Guid.NewGuid());
}
}
public interface IFoo
{
void DoStuff(params object[] args);
}
public class Foo : IFoo
{
public void DoStuff(Guid arg)
{
}
void IFoo.DoStuff(params object[] args)
{
if (args.Length != 1) throw new ArgumentException("args");
if (args[0].GetType() != typeof(Guid)) throw new ArgumentException("args");
DoStuff((Guid)args[0]);
}
}