Setting an instance property to a static property [closed] - c#

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'm sorry if the title's a bit unusual or wrong, I'm not certain about how to ask this. Please offer an edit or change it if so.
I'm trying to achieve this:
private class Products
{
public Status
{
// set status to `Active`, `Deleted` or `Suspended` for an instance of Products. //
}
}
What I don't know how to do is code something that will allow me to call Products.Status.Active, then set that value to MyAccount. The other two values will be Suspended and Deleted.

You need to make Status an enum
public enum Status
{
Active = 1,
Suspended = 2,
Deleted = 3
}
Looks like you want to implement ChangeStatus as static
public class TaskClass
{
public static ChangeStatus(Accounts.Account a, MyTask t, Status s)
{...}
}
private void Main() {
Accounts.Account account = new Accounts.Account();
TaskClass.Task task = new TaskClass.Task();
TaskClass.ChangeStatus(account, task, Status.Active);
}
PS If you need to name it TaskClass.Status instead of Status, just nest the enum inside TaskClass.

Related

C# Application Settings. A quick Question about User V's Application Scope [closed]

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 months ago.
The community is reviewing whether to reopen this question as of 7 months ago.
Improve this question
I have just looked at an example from Microsoft Social Network on how to use Application Settings namely this one:
public bool IsUSer(SettingsProperty Setting)
{
bool flag = Setting.Attributes[typeof(UserScopedSettingAttribute)] is UserScopedSettingAttribute;
bool flag1 = Setting.Attributes[typeof(ApplicationScopedSettingAttribute)] is ApplicationScopedSettingAttribute;
if (flag && flag2)
{
throw new ConfigurationErrorsException(SR.GetString("BothScopeAttributes"));
}
if (!flag && !flag2)
{
throw new ConfigurationErrorsException(SR.GetString("NoScopeAttributes"));
}
return flag;
}
This checks to see if the Setting is both User and Application Scoped or neither. Is it even possible that these two situations can occur. Surely the API would not allow this to happen in the first place. Should we be checking for this or is this example a little over the top. I know this is more a discussional rant but really, surely this can't really happen?
Link to Example: https://social.msdn.microsoft.com/Forums/en-US/4c0d2eae-2f0b-41c8-bb60-c4b0ffd3cd0b/how-to-retrieve-usersettings-v-defaultsettings-c-vbe2008?forum=netfxbcl
Thanks
Danny
[UserScopedSetting] and [ApplicationScopedSetting] are attributes so you might use them like this:
public class MyUserSettings : ApplicationSettingsBase
{
[UserScopedSetting()]
[DefaultSettingValue("white")]
public Color BackgroundColor
{
get
{
return ((Color)this["BackgroundColor"]);
}
set
{
this["BackgroundColor"] = (Color)value;
}
}
}
(Source for above code)
If you refer to this question, you'll see that it isn't possible to prevent two attributes occurring together. So, the code you have shown is actually protecting against one of these two scenarios:
1 - Both attributes are applied:
public class MyUserSettings : ApplicationSettingsBase
{
[UserScopedSetting()]
[ApplicationScopedSetting()]
[DefaultSettingValue("white")]
public Color BackgroundColor
{
get
{
return ((Color)this["BackgroundColor"]);
}
set
{
this["BackgroundColor"] = (Color)value;
}
}
}
2 - Neither attribute is applied:
public class MyUserSettings : ApplicationSettingsBase
{
[DefaultSettingValue("white")]
public Color BackgroundColor
{
get
{
return ((Color)this["BackgroundColor"]);
}
set
{
this["BackgroundColor"] = (Color)value;
}
}
}
So ultimately it is checking that exactly one of these two attributes is applied.

C# accessing a property of a generic class T [closed]

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 6 years ago.
Improve this question
I need to accessing a property of a generic Class T
I have this method in class generic
public T calcuste(T obj)
{
calcaulte testobj= new calcaulte ()
var t = GetValue(obj); // get the type of class for example that is calcaulte class
testobj.Id = obj.Id;// that is what I need to do accessing a property of T obj
}
Try following code.
public T calcuste(T obj)
{
calcaulte testobj= new calcaulte ();
calcaulte obj_calcaulte = obj as calcaulte;
if(obj_calcaulte != null)
{
testobj.Id = obj_calcaulte .Id;
}
}
You need to control for null since obj may be null or may belong to different class.

Should I be avoiding several boolean parameters? [closed]

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.

Let the user edit some value,but not all [closed]

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.

How to pass difference type into a function like passing variables [closed]

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
DatePickerOfItemsControl, TextBoxOfItemsControl, ComboBoxOfItemsControl are class, how to pass these class into a function like passing variables
for the function to save these class or type as a variable and use it to create instance when needed
ifactory.AddControl(DatePickerOfItemsControl);
ifactory.AddControl(TextBoxOfItemsControl);
//ifactory.AddControl(textbox2);
ifactory.AddControl(ComboBoxOfItemsControl);
ifactory.AddControl(RadioBoxOfItemsControl);
public void AddControl(Object c)
{
datepickerclass = DatePickerOfItemsControl;
public void Apply()
{
datepickerclass datepicker = new datepickerclass();
Use Type:
public void AddControl(Type c)
{
}
You can use Activator.CreateInstance() to then create an instance of the type:
public void AddControl(Type c)
{
object o = Activator.CreateInstance(c);
}
Then call it like this:
AddControl(typeof(RadioBoxOfItemsControl));
See MSDN -
http://msdn.microsoft.com/en-us/library/wccyzw83.aspx
I'm not really sure but I think your trying to generalise the creation of controls so how about something like this:
ifactory.AddControl(() => { new datepickerclass() });
public void AddControl<T>(Func<T> factory)
where T : BaseControlType
{
var instanceOfControl = factory();

Categories