C# accessing a property of a generic class T [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 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.

Related

How to check how many times a class property was set with a specific value? [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 7 years ago.
Improve this question
Is there any possible way in determining how many times a class property was set with a specific value ?
You can do it by using settter as well, Lake a look into the following snippet
class myClass
{
private string _MyStringVar;
public string specificWord = "word";
public int SpecificCount = 0;
public string MyStringVar
{
get { return _MyStringVar; }
set
{
bool isChanged = false;
if (_MyStringVar != specificWord) { isChanged = true; }
// check for old value to confirm value changed
_MyStringVar = value;
if (value == specificWord && isChanged) { SpecificCount++; }
}
}
}
you can implement INotifyPropertyChanged for this purpose
Source: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

How does c# KeyedCollection initialize? [closed]

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 7 years ago.
Improve this question
I'm reading some c# source codes.And something confuses me.In the following code,OptionSet is inherited from class KeyedCollection,and when OptionSet initialize like the following,it will at first call OptionSet() to get a new but empty OptionSet,then call Add() twice to insert { "h|?|help", v => Syntax() } and { "analyzer=", v => analyzer = v }.
I want to know why it initializes like this,why use Add()?
var p = new OptionSet()
{
{ "h|?|help", v => Syntax() },
{ "analyzer=", v => analyzer = v },
}
public class OptionSet : KeyedCollection<string, Option>{
public OptionSet ()
{
}
public OptionSet Add (string prototype, Action<string> action){...}
...
}
When you do this:
var p = new AnyCollection()
{
...
}
you are using Collection initializers.
From here:
Collection initializers let you specify one or more element
initializers when you initialize a collection class that implements
IEnumerable or a class with an Add extension method. The element
initializers can be a simple value, an expression or an object
initializer. By using a collection initializer you do not have to
specify multiple calls to the Add method of the class in your source
code; the compiler adds the calls.

Adding local variable to List [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 9 years ago.
Improve this question
public class C
{
private List<myClass> _list = new List<myClass>();
public void MyFunction()
{
myClass myClassInstance = new myClass();
// working with myClassInstance
_list.Add(myClassInstance);
}
}
Problem is that List doesn't get populated, _list.Count is always 0 :(
Well, it is depends on your desing of myClass but I see two things;
class is a keyword, that's why you should use it as a #class
Keywords are predefined, reserved identifiers that have special
meanings to the compiler. They cannot be used as identifiers in your
program unless they include # as a prefix. For example, #if is a valid
identifier but if is not because if is a keyword.
your function method missed a return type which is void looks okey for your case.
You need to rename both the instance of myClass and function to something OTHER than C# keywords. At that point you can add the value to the list
public class C
{
private List<myClass> _list = new List<myClass>();
public void MyFunction()
{
myClass myClassInstance = new myClass();
// working with myClassInstance
_list.Add(myClassInstance);
}
}

Setting an instance property to a static property [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'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.

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