Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am creating ad hoc reporting solution, so I came up with a this method, its added in WCF service which will be called from front end client,
GetEmployeeDetails(int id, bool includeAddressHistory, bool
includeSalaryHistory, bool includePositionHistory, bool
includeProjectHistory, ...never ending list)
Now issue is I need to get all of the data based on filters and then either return the complete dataset or return it as a stream, as I have another method which returns same dataset as a stream,
Usually, when you need to pass a rather large number of parameters (regardless of their type), it's time to consider parameter object:
// this is just POCO
public class SearchParameters
{
public string SomeString { get; set; }
public DateTime? SomeDate { get; set; }
public bool? SomeBool { get; set; }
// etc...
}
IEnumerable<SomeEntitites> GetSomeEntitites(SearchParameters searchParameters);
Note, that for constructor cases the solution could be a builder pattern.
An alternative if things get even more complicated is to have one class that has the EmployeeDetailsRequestParameters defined. This is particularly useful if you also do things like filtering and have a lot more than just what can fit into a flag enumerable.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
so in my little c# console-program, i have i have this class, where I use get and set methods for my properties
class myClass
{
public int ID { get; set; }
public string value { get; set; }
public myClass(int ID, string value)
{
this.ID = ID;
this.value = value;
}
}
however when i am im my main method, i am not able to access these two properties through the get-method, neither am i able to use set() on any of them, what am I doing wrong?
In C#, you don't need to invoke the getter or setter on a property:
var id = class.ID; // getter invoked automatically
Will automatically call the getter, and in your example, you're already using the setter in your constructor.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is there a way to write unit test that prove class has only public members?
I've done some research about the problem and tried some code but I don't have a solution so far. I am stuck at the point where i need to check are there any private property.
//In the unit test
var viewModels = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(a => a.Namespace != null &&
a.Namespace.StartsWith("Test.Solution"))
.Where(t => t.Name.EndsWith("View"))
.ToList();
//ViewModels
public class PersonView {
public string DurationMinutes; // don't take this into consideration
public string Name { get; set; }
public string LastName { get; set; }
private string Number{ get; set; }
// take into consideration only properies with getter and setter
}
For a type, T you can get the number of non-public properties using reflection:
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
var numberOfNonPublicProperties = typeof(T).GetProperties(flags).Length;
Use Reflection. Basically, .GetType().GetProperties() (if I remember correctly) with appropriate flags. In case you're interested not only in properties, but as well fields, you have to include them also. And then just check that resulting list is empty. LINQ will be handy here.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How to create a binary tree of value type String by C#
i don't want binary tree search i just want to make binary tree of value string to do some algorithm like BFS & DFS .. )
You can start with a simple node class as shown below. Here Data represents a string value like you've mentioned. You can change this to int or any other type.
public class BinaryTreeNode
{
public string Data { get; set; }
public BinaryTreeNode Left { get; set; }
public BinaryTreeNode Right { get; set; }
public BinaryTreeNode(string data)
{
Data = data;
}
}
Why don't you start by taking a look at some of these articles
MSDN
How to create a binary tree
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Someone wrote a method that returns an object type.
public object GetProvider(double personId)
{
// Some other code here
return new {
FirstName = Convert.ToString(reader["FirstName"]),
LastName = Convert.ToString(reader["LastName"])
};
}
In different part of code, we use this library method.
var d = GetProvider(123);
string fName = d.GetType().GetProperty("FirstName").GetValue(d, null).ToString();
How can I improve this code or simplify it? I guess they should have created a class and returned an object of it instead of object type. Also, I have seen code that use dynamic in C#4. I tried that, but it didn't work well with object type. Any thoughts?
Implement dynamic like this:
dynamic d = GetProvider(123);
string fName = d.FirstName;
It seems that this part is actually irrelevant to the original post, as the OP has no control over the method's return-type. I'm leaving it here because it's still best-practice and should be followed in a significant majority of cases, and in an ideal world, the writer of the original method would read this and change his or her ways.
But in general, I'd be tempted to just make a type. It's not like it takes all that much effort to spin up a class to do exactly what you want.
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Then you can have everything strong-typed and you don't need to use dynamic. I don't think I've ever found a truly appropriate use for dynamic. It's nice because it's quick and easy to write, but it tends to just cause problems in the long run in 99% of real-world uses. I'm not saying it should be abolished because that 1% is useful (I'm thinking serialization of dynamic data here, but even there I usually just use the string indexer properties), but generally speaking, think twice before using it and make sure there isn't a better solution. Anonymous types are useful in LINQ queries where it's all in-scope and compiled, but I wouldn't change scopes using them unless you absolutely have to, and you pretty well never do.
If this were me, I would probably wrap the result from GetProvider in a strongly typed class. The class below takes the response from GetProvider as an argument on its constructor.
public class Provider
{
public Provider(object provider)
{
dynamic dynamicProvider = provider;
this.FirstName = dynamicProvider.FirstName;
this.LastName = dynamicProvider.LastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
Then just replace
var d = GetProvider(123);
with
var d = new Provider(GetProvider(123));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am writing a sudoko program, I want the user to edit certain values in the array,but not the values which are already there. how do I initialize the array?
The easiest way to overcome your problem is to use 2 multidimensional arrays, the first one to save the value and the second one to check whether some cell can be edited by the user or not..
int[,] ValueArray= new int[4,4];
boolean[,] EditedArray= new boolean[4,4];
You can approach your problem with multiple solutions, all of which relay on the same principle -> Having your number coupled with a boolean.
You can write it down with a class:
public class SudokuCell
{
public bool IsEditable { get; set; }
private int _value;
public int Value
{
get { return _value; }
set { if (IsEditable) _value = value; }
}
or a struct:
public struct SudokuCell
{
public bool IsEditable;
public int Value;
}
and have a List or an Array of SudokuCells that you can use as your data structure or you can use a lazier method and write it down using Tuple:
List<Tuple<int, bool>> sudokuCells = new List<Tuple<int,bool>>();
Then whenever you want to change the value you can check it's corresponding bool and you instantly know whether you can or can't change it (assuming you set it when you initialize your sudoku)
Your sudoku user edits your UI, not your array. Make the UI element read-only when the associated data should be read-only.