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);
}
}
Related
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 2 years ago.
Improve this question
Let's say I have a class:
class Foo
{
private int blah;
private char bleh;
private string bluh;
//...
public Foo()
{
}
}
Now normally, I have to initialise it like this:
public Foo(int blah, char bleh, string bluh,...)
{
this.blah=blah;
this.bleh=bleh;
this.bluh=bluh;
//...
}
This is dull and doesn't seem to follow the "Don't Repeat Yourself" principle so I'm looking for a better solution. Can this be done DRYer?
Edit: Changed variables to private to reflect a more common use case.
Use object initialization when initializing your variables/properties instead, if you really don't want to repeat yourself
Like this
Foo f = new foo{
blah = 1;
bleh = 'A';
bluh = "something";
};
And also, don't use public accessor on your fields, that's not good.
class Foo
{
public readonly int blah;
public readonly char bleh;
public readonly string bluh;
//...
public Foo(int blah, char bleh, string bluh)
{
this.blah=blah;
this.bleh=bleh;
this.bluh=bluh;
//...
}
}
Can be written as a record type and is a good candidate as its immutable. To rewrite the above as a record just use
public record Foo(int blah, char bleh, string bluh);
Much cleaner in my opinion and seems to be what you're after.
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.
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.
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.
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();