Posts

Showing posts with the label object

Exception member class OOP

Exception member class OOP I have met the following concept in the production code: class A { public: class Exception : public std::exception {/* ... */}; //... }; Nobody can give me a clear answer why it is like this. My question is whether this approach is in line with SOLID rules? I think that would be better if this exception class is located outside the class A and is injected while creating the instance of A. Design principles are important, but they aren't followed "because they are design principles". They are followed because they accomplish something. What would injecting the exception object upon construction accomplish? Now the class should manage this objects lifetime. What if an error state is never encountered, and it's never thrown? – StoryTeller Jul 2 at 6:16 ...

Need to create, use and store dynamic object names in Powershell

Need to create, use and store dynamic object names in Powershell I am loading in a text file and looping through each line, and trying to print a checkbox, in a powershell form. However, the way below, all checkboxes have the same variable/object name, which makes it impossible to tell them apart. I need a way to dynamically create $checkbox0 through $checkbox(# of lines in text file, which can change), and fill them in below, as well as store the names, so i can validate if they are clicked later $pFile = Get-Content "C:results.txt" $rowCounter = 0 foreach($line in $pFile){ $checkbox = New-Object System.Windows.Forms.CheckBox $checkbox.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size $checkbox.AutoSize = "true" $checkbox.TabIndex = $rowCounter $checkbox.Text = $line $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 25 $yValue = (20 * $rowCounter) $System_Dra...

How to pass a function that's a property of another object into a function and call it in javascript?

How to pass a function that's a property of another object into a function and call it in javascript? If I have an object in Javascript and one of its properties is a function: function cow() { this.timesMooed = 0; this.sayMoo = function () { this.timesMooed++; return "moo"; }; } Say I also have another function that takes some function as an argument, calls it and records the result: var actionResults = ; function doAction(action) { actionResults.push(action()); } Now let's put this into practice and see what happens: var jerry = new cow(); doAction(jerry.sayMoo); console.log(actionResults); // Outputs ["moo"] -this is correct console.log(jerry.timesMooed); // Outputs 0 -uh oh How can I pass in the function so that it's Jerry that is running the function? Jerry doAction(jerry.sayMoo.bind(jerry)); – ASDFGerte Jul 2 at 2:25 ...

How to destroy a class object in PHP?

How to destroy a class object in PHP? I wrote a little class for storing global variables/functions. My question is - is it necessary to destroy the class object after the script has finished? or will PHP destroy that object itself? Here's my code: $web=new c_web("myWeb"); $web->loadTemplate("/!framework/admin/template.htm"); $web->doStuff(); // script has finished - destroying required here? In case I need to destroy it, how can I do that? 3 Answers 3 If the script finishes, the memory is released. You're ready as is :) great - thanks! and what about if a script will will be terminated by an error? will all variables (database ..) be destroyed aswell? – Fuxi Apr 8 '11 at 11:34 Well, the d...

JavaScript - Wait until all array.push() are complete before returning function [duplicate]

JavaScript - Wait until all array.push() are complete before returning function [duplicate] This question already has an answer here: I have written a function that loops over an object and uses array.push(XYZ) to append the value ( XYZ ) to the array ( array ). After the loop is complete the function returns a promise. When I use myFunction().then(function(response) { console.log(response[0])}) , I get undefined in the console. When I type in console.log(response[0]) in the console, I get the correct value. What am I doing wrong? I think that it is taking time to push the value into the array but I am not 100%. Any help will be appreciated. array.push(XYZ) XYZ array myFunction().then(function(response) { console.log(response[0])}) undefined console.log(response[0]) My Code (I have not included the code defining db but it is not important as getting the info from the database is working fine.) db function getChild(uid) { promise = db.collection("users").doc(uid).get(...

Better way to update an object's value at a variable depth

Better way to update an object's value at a variable depth I am working on some software that reads/writes information in localStorage using a handler. You can find a working example here: http://jsbin.com/wifucugoko/edit?js,console localStorage My problem is with the segment of code below ( focusing on the switch statement ): _t.set = function(path, value) { // Update a single value or object if (~path.indexOf(".")) { let o = path.split(".")[0], p = this.get(o), q = path.split(".").slice(1); switch (q.length) { // There has to be a better way to do this... case 1: p[q[0]] = value; break; case 2: p[q[0]][q[1]] = value; break; case 3: p[q[0]][q[1]][q[2]] = value; break; case 4: ...

React - Trying to render property of object, though it stays “undefined”

Image
React - Trying to render property of object, though it stays “undefined” console.log(detailtext) shows me that the data of the object is there, the props seem to work, but I can't display the properties of the object. Why? console.log(detailtext) It's a very simple component: import React from "react"; import { Link } from "react-router-dom"; class LibraryTextDetails extends React.Component { render() { const detailtext = this.props.detailview || {}; console.log("THIS IS THE DETAILTEXT"); console.log(detailtext); const detailviewIds = detailtext.id; console.log("THIS IS THE DETAILVIEW ID"); console.log(detailviewIds); return ( <div> <div className="covercard"> <img src={detailtext.lead_image_url} width={157} className="coverdetailimage" /> </div> <div className="titledetai...

How to initialize an empty mutable array in Objective C

How to initialize an empty mutable array in Objective C I have a list of objects (trucks) with various attributes that populate a tableview. When you tap them they go to an individual truck page. There is an add button which will add them to the favorite list in another tableview. How do I initialize an empty mutable array in Cocoa? I have the following code: -(IBAction)addTruckToFavorites:(id)sender:(FavoritesViewController *)controller { [controller.listOfTrucks addObject: ((Truck_Tracker_AppAppDelegate *)[UIApplication sharedApplication].delegate).selectedTruck]; } You shouldn't make the controller's mutable array public like that. The controller should only make an immutable array available to other objects; for adding a new truck, you should add a method to the controller that does that. Then, instead of getting the listOfTrucks and directly changing it without the controller's knowledge, you tell the controller to make the change. ...