Search Results

Search found 3493 results on 140 pages for 'constructor'.

Page 8/140 | < Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >

  • How are Flash library symbols constructed? Why are width/height already available in constructor?

    - by Triynko
    Suppose I draw a square on the stage, convert it to a symbol, export it for ActionScript with a classname of "MySquare" (and of course a base class of MovieClip). How is it that in the MySquare constructor, the width and height of this MovieClip are already available? In fact, any named instances in the clip are also available. I'm confused about how the Flash player seems to be able to pre-construct my movie clip by populating its properties and child clips before my constructor for the class ever runs. I thought that it would have to first construct the clip, calling my constructor code, and then construct and add any children, but obviously the player is doing something special for clips designed in the Flash authoring environment.

    Read the article

  • Configuring Unity with a closed generic constructor parmater

    - by fearofawhackplanet
    I've been trying to read the article here but I still can't understand it. I have a constructor resembling the following: IOrderStore orders = new OrderStore(new Repository<Order>(new OrdersDataContext())); The constructor for OrderStore: public OrderStore(IRepository<Order> orderRepository) Constructor for Repository<T>: public Repository(DataContext dataContext) How do I set this up in the Unity config file? UPDATE: I've spent the last few hours banging my head against this, and although I'm not really any closer to getting it right I think at least I can be a little more specific about the problem. I've got my IRespository<T> working ok: <typeAlias alias="IRepository" type="MyAssembly.IRepository`1, MyAssembly" /> <typeAlias alias="Repository" type="MyAssembly.Repository`1, MyAssembly" /> <typeAlias alias="OrdersDataContext" type="MyAssembly.OrdersDataContext, MyAssembly" /> <types> <type type="OrdersDataContext"> <typeConfig> <constructor /> <!-- ensures paramaterless constructor used --> </typeConfig> </type> <type type="IRepository" mapTo="Repository"> <typeConfig> <constructor> <param name="dataContext" parameterType="OrdersDataContext"> <dependency /> </param> </constructor> </typeConfig> </type> </types> So now I can get an IRepository like so: IRepository rep = _container.Resolve(); and that all works fine. The problem now is when trying to add the configuration for IOrderStore <type type="IOrderStore" mapTo="OrderStore"> <typeConfig> <constructor> <param name="ordersRepository" parameterType="IRepository"> <dependency /> </param> </constructor> </typeConfig> </type> When I add this, Unity blows up when trying to load the config file. The error message is OrderStore does not have a constructor that takes the parameters (IRepository`1). What I think this is complaining about is because the OrderStore constructor takes a closed IRepository generic type, ie OrderStore(IRepository<Order>) and not OrderStore(IRepository<T>) I don't have any idea how to resolve this.

    Read the article

  • Explicit constructor still has default values even though a default constructor is not invoked.

    - by Phoenix
    According to my understanding a default constructor initializes the state of the object to default values, so if i provide an explicit no-arg public constructor like this then how are the values of d and e still getting initialized to zero because in this case the default constructor is not invoked. public class B extends A{ private int d; private int e; public B() { System.out.println(d); System.out.println(e); } } EDIT:: The only thing default constructor does is call to super() then how come if i have a explicitly mentioned a constructor here and A has a protected variable say c which is initialized to 17 in its constructor. Should I not be explicitly calling super() to be able to see that change since I'm using my own constructor ? Why is B still getting the value of 17 through inheritance ?

    Read the article

  • Calling child constructor by casting (ChildClass)parentObject; to track revisions

    - by FreshCode
    To track revisions of a Page class, I have a PageRevision class which inherits from Page and adds a revision ID (Guid RevisionID;). If possible, how should I cast an existing Page object to a PageRevision and ensure that the PageRevision constructor is called to create a new revision ID? I could could have a PageRevision(Page page) constructor which generates the Guid and copies all the Page attributes, but I want to automate it, especially if a Page class has many attributes (and I later add one, and forget to modify the copy constructor). Desired use Page page = new Page(123, "Page Title", "Page Body"); // where 123 is page ID PageRevision revision = (PageRevision)page; // now revision.RevisionID should be a new Guid. Page, PageRevision classes: public class Page { public int ID { get; set; } public string Title { get; set; } public string Body { get; set; } } public class PageRevision : Page { public Guid RevisionID { get; set; } public PageRevision() { this.RevisionID = Guid.NewGuid(); } }

    Read the article

  • Constructor parameter validation in C# - Best practices

    - by MPelletier
    What is the best practice for constructor parameter validation? Suppose a simple bit of C#: public class MyClass { public MyClass(string text) { if (String.IsNullOrEmpty(text)) throw new ArgumentException("Text cannot be empty"); // continue with normal construction } } Would it be acceptable to throw an exception? The alternative I encountered was pre-validation, before instantiating: public class CallingClass { public MyClass MakeMyClass(string text) { if (String.IsNullOrEmpty(text)) { MessageBox.Show("Text cannot be empty"); return null; } else { return new MyClass(text); } } }

    Read the article

  • Using new to allocate an array of class elements with an overloaded constructor in C++.

    - by GordoN
    As an example say I have a class foo that does not have a default constructor but one that looks like this foo:foo(int _varA,int _varB) { m_VarA = _varA; m_VarB = _varB; } How would I allocate an array of these. I seem to remember trying somthing like this unsuccessfully. foo* MyArray = new foo[100](25,14). I don't think this will work either. foo* MyArray = new foo[100](25,14) Can this be done? I typically do this by writing the default constructor using some preset values for _varA and _varB. Then adding a function to reset _varA and _varB for each element but that will not work for this case. Thanks for the help.

    Read the article

  • class classname(value); & class classname=value; difference when constructor is explicit

    - by Mahesh
    When constructor is explicit, it isn't used for implicit conversions. In the given snippet, constructor is marked as explicit. Then why in case foo obj1(10.25); it is working and in foo obj2=10.25; it isn't working ? #include <iostream> class foo { int x; public: explicit foo( int x ):x(x) {} }; int main() { foo obj(10.25); // Not an error. Why ? foo obj2 = 10.25; // Error getchar(); return 0; } error: error C2440: 'initializing' : cannot convert from 'double' to 'foo'

    Read the article

  • Performing user authentication in a CodeIgniter controller constructor?

    - by msanford
    In "The Clean Code Talks -- Unit Testing" (http://youtu.be/wEhu57pih5w), Miško Hevery mentions that "as little work as possible should be done in constructors [to make classes more easily testable]'. It got me thinking about the way I have implemented my user authentication mechanism. Having delved into MVC development through CodeIgniter, I designed my first web application to perform user authentication for protected resources in controllers' constructors in cases where every public function in that controller requires the user to be authenticated. For controllers with public methods having mixed authentication requirements, I would naturally move the authentication from the constructor to each method requiring authentication (though I don't currently have a need for this). I made this choice primarily to keep the controller tight, and to ensure that all resources in the controller are always covered. As for code longevity and maintainability: given the application structure, I can't foresee a situation in which one of the affected controllers would need a public method that didn't require user authentication, but I can see this as a potential drawback in general with this implementation (i.e., requiring future refactoring). Is this a good idea?

    Read the article

  • constructor should not call methods

    - by Stefano Borini
    I described to a colleague why a constructor calling a method is an antipattern. example (in my rusty C++) class C { public : C(int foo); void setFoo(int foo); private: int foo; } C::C(int foo) { setFoo(foo); } void C::setFoo(int foo) { this->foo = foo } I would like to motivate better this fact through your additional contribute. If you have examples, book references, blog pages, or names of principles, they would be very welcome. Edit: I'm talking in general, but we are coding in python.

    Read the article

  • Constructor should generally not call methods

    - by Stefano Borini
    I described to a colleague why a constructor calling a method can be an antipattern. example (in my rusty C++) class C { public : C(int foo); void setFoo(int foo); private: int foo; } C::C(int foo) { setFoo(foo); } void C::setFoo(int foo) { this->foo = foo } I would like to motivate better this fact through your additional contribute. If you have examples, book references, blog pages, or names of principles, they would be very welcome. Edit: I'm talking in general, but we are coding in python.

    Read the article

  • Is std::move really needed on initialization list of constructor for heavy members passed by value?

    - by PiotrNycz
    Recently I read an example from cppreference.../vector/emplace_back: struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } My question: is this std::move really needed? My point is that compiler sees that this p_name is not used in the body of constructor, so, maybe, there is some rule to use move semantics for it by default? That would be really annoying to add std::move on initialization list to every heavy member (like std::string, std::vector). Imagine hundreds of KLOC project written in C++03 - shall we add everywhere this std::move? This question: move-constructor-and-initialization-list answer says: As a golden rule, whenever you take something by rvalue reference, you need to use it inside std::move, and whenever you take something by universal reference (i.e. deduced templated type with &&), you need to use it inside std::forward But I am not sure: passing by value is rather not universal reference?

    Read the article

  • Constructor vs setter validations

    - by Jimmy
    I have the following class : public class Project { private int id; private String name; public Project(int id, String name, Date creationDate, int fps, List<String> frames) { if(name == null ){ throw new NullPointerException("Name can't be null"); } if(id == 0 ){ throw new IllegalArgumentException("id can't be zero"); } this.name = name; this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } I have three questions: Do I use the class setters instead of setting the fields directly. One of the reason that I set it directly, is that in the code the setters are not final and they could be overridden. If the right way is to set it directly and I want to make sure that the name filed is not null always. Should I provide two checks, one in the constructor and one in the setter. I read in effective java that I should use NullPointerException for null parameters. Should I use IllegalArgumentException for other checks, like id in the example.

    Read the article

  • Initializing entities vs having a constructor parameter

    - by Vee
    I'm working on a turn-based tile-based puzzle game, and to create new entities, I use this code: Field.CreateEntity(10, 5, Factory.Player()); This creates a new Player at [10; 5]. I'm using a factory-like class to create entities via composition. This is what the CreateEntity method looks like: public void CreateEntity(int mX, int mY, Entity mEntity) { mEntity.Field = this; TileManager.AddEntity(mEntity, true); GetTile(mX, mY).AddEntity(mEntity); mEntity.Initialize(); InvokeOnEntityCreated(mEntity); } Since many of the components (and also logic) of the entities require to know what the tile they're in is, or what the field they belong to is, I need to have mEntity.Initialize(); to know when the entity knows its own field and tile. The Initialize(); method contains a call to an event handler, so that I can do stuff like this in the factory class: result.OnInitialize += () => result.AddTags(TDLibConstants.GroundWalkableTag, TDLibConstants.TrapdoorTag); result.OnInitialize += () => result.AddComponents(new RenderComponent(), new ElementComponent(), new DirectionComponent()); This works so far, but it is not elegant and it's very open to bugs. I'm also using the same idea with components: they have a parameterless constructor, and when you call the AddComponent(mComponent); method in an entity, it is the entity's job to set the component's entity to itself. The alternative would be having a Field, int, int parameters in the factory class, to do stuff like: new Entity(Field, 10, 5); But I also don't like the fact that I have to create new entities like this. I would prefer creating entities via the Field object itself. How can I make entity/component creation more elegant and less prone to bugs?

    Read the article

  • Use constructor or setter method?

    - by user633600
    I am working on a UI code where I have an Action class, something like this - public class MyAction extends Action { public MyAction() { setText("My Action Text"); setToolTip("My Action Tool tip"); setImage("Some Image"); } } When this Action class was created it was pretty much assumed that the Action class wont be customizable (in a sense- its text, tooltip or image will be not be changed anywhere in the code). Of late, now we are in need of changing the action text at some location in code. So I suggested my co-worker to remove the hardcoded action text from the constructor and accept it as an argument, so that everybody is forced to pass the action text. Something like this code below - public class MyAction extends Action { public MyAction(String actionText) { setText(actionText); setTooltip("My Action tool tip); setImage("My Image"); } } He however thinks that since setText() method belongs to base class. It can be flexibly used to pass the action text wherever action instance is created. That way, there is no need to change the existing MyAction class. So his code would look something like this. MyAction action = new MyAction(); //this creates action instance with the hardcoded text action.setText("User required new action text"); //overwrite the exisitng text. I am not sure if that is a correct way to deal with problem. I think in above mentioned case user is anyway going to change the text, so why not force him while constructing the action. The only benefit I see with the original code is that user can create Action class without much thinking about setting text.

    Read the article

  • WCF Runtime Error while using Constructor

    - by Pranesh Nair
    Hi all, I am new to WCF i am using constructor in my WCF service.svc.cs file....It throws this error when i use the constructor The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host. When i remove the constructor its working fine....But its compulsory that i have to use constructor... This is my code namespace UserAuthentication { [ServiceBehavior(InstanceContextMode=System.ServiceModel.InstanceContextMode.Single)] public class UserAuthentication : UserRepository,IUserAuthentication { private ISqlMapper _mapper; private IRoleRepository _roleRepository; public UserAuthentication(ISqlMapper mapper): base(mapper) { _mapper = mapper; _roleRepository = new RoleRepository(_mapper); } public string EduvisionLogin(EduvisionUser aUser, int SchoolID) { UserRepository sampleCode= new UserRepository(_mapper); sampleCode.Login(aUser); return "Login Success"; } } } can anyone provide ideas or suggestions or sample code hw to resolve this issue...

    Read the article

  • Generic Abstract Singleton with Custom Constructor in C#

    - by Heka
    I want to write a generic singleton with an external constructor. In other words the constructor can be modified. I have 2 designs in my mind but I don't know whether they are practical or not. First one is to enforce derived class' constructor to be non-public but I do not know if there is a way of it? Second one is to use a delegate and call it inside the constructor? It isn't necessarily to be a constructor. The reason I chose custom constructor is doing some custom initializations. Any suggestions would be appreciated :)

    Read the article

  • How is a constructor executed?

    - by simion
    I am doing some reviison from the lecture slides and it says a constructor is executed in the following way; If the constructor starts with this, recursively execute the indicated constructor, then go to step 4. Invoke the explicitly or implicitly indicated superclass constructor (unless this class is java.lang.Object) Initialise the fields of the object in the order in which they were declared in this class Execute the rest of the body of this constructor. What i dont undertsand is that, a constructor can never "start" with this, because even if it forms no class heirarchy/relationship then super() is inserted by default. How would this fit in with the description above? Thanks

    Read the article

  • What is the meaning of ": base" in the constructor definition ?

    - by DotNetBeginner
    What is the meaning of ": base" in the costructor of following class(MyClass) ? Please explain the concept behind constructor definition given below for class MyClass. public class MyClass: WorkerThread { public MyClass(object data): base(data) { // some code } } public abstract class WorkerThread { private object ThreadData; private Thread thisThread; public WorkerThread(object data) { this.ThreadData = data; } public WorkerThread() { ThreadData = null; } }

    Read the article

< Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >