Initialize class fields in constructor or at declaration?

Initialize class fields in constructor or at declaration?

I've been programming in C# and Java recently and I am curious where the best place is to initialize my class fields.Should I do it at declaration?:public class Dice{ private int topFace = 1; private Random myRand = new Random(); p

In what cases do I use malloc vs new?

In what cases do I use malloc vs new?

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the new operator you should pair with delete and it is a mistake to mix the two (e.g. Calling fr

Understanding Generators in Python

Understanding Generators in Python

Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however

What's wrong with using Thread.Abort()

What's wrong with using Thread.Abort()

So I know that you shouldn't use Thread.Abort()But I've never been given a good explanation. Is there a performance penalty or some hidden gotcha?I know you can't ignore/swallow the ThreadAbortException (which makes sense) 1:In addition

C++ delete - It deletes my objects but I can still access the data?

C++ delete - It deletes my objects but I can still access the data?

I have written a simple, working tetris game with each block as an instance of a class singleblock.class SingleBlock{ public: SingleBlock(int, int); ~SingleBlock(); int x; int y; SingleBlock *next;};class MultiBlock{ pu

C# variance problem: Assigning List as List

C# variance problem: Assigning List as List

Look at the following example (partially taken from MSDN Blog):class Animal { }class Giraffe : Animal { }static void Main(string[] args){ // Array assignment works, but... Animal[] animals = new Giraffe[10]; // implicit... List

How to pick an image from gallery (SD Card) for my app?

How to pick an image from gallery (SD Card) for my app?

This question was originally asked for Android 1.6.I am working on photos options in my app. I have a button and an ImageView in my Activity. When I click the button it would redirect to the gallery and I would be able to select an image. T

How to create a Custom Dialog box in android?

How to create a Custom Dialog box in android?

I want to create a custom dialog box like below I have tried the following things.I created a subclass of AlertDialog.Builder and used a custom Title and Custom Content View and used that but the result was not as expected.Another attempt w

Why is the Java date API (java.util.Date, .Calendar) such a mess?

Why is the Java date API (java.util.Date, .Calendar) such a mess?

As most people are painfully aware of by now, the Java API for handling calendar dates (specifically the classes java.util.Date and java.util.Calendar) are a terrible mess.Off the top of my head:Date is mutableDate represents a timestamp, n

How do getters and setters work?

How do getters and setters work?

I'm from the php world. Could you explain what getters and setters are and could give you some examples? 1:Tutorial is not really required for this. Read up on encapsulationprivate String myField; //"private" means access to this is rest

MS Excel crashes when vba code runs

MS Excel crashes when vba code runs

I am having a problem with excel crashing when i run VBA code on an excel sheet.I have some code to add a formula to a range of cells as follows:Private Sub Worksheet_Change(ByVal Target As Range) Worksheets("testpage").Range("A1:A8").For

console.log() async or sync?

console.log() async or sync?

I am currently reading Async Javascript by Trevor Burnham. This has been a great book so far.He talks about this snippet and console.log being 'async' in the Safari and Chrome console. Unfortunately I can't replicate this. Here is the code

How do I properly clean up Excel interop objects?

How do I properly clean up Excel interop objects?

I'm using the Excel interop in C# (ApplicationClass) and have placed the following code in my finally clause:while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet) != 0) { }excelSheet = null;GC.Collect();GC.WaitForPendin

Why is my Spring @Autowired field null?

Why is my Spring @Autowired field null?

Note: This is intended to be a canonical answer for a common problem.I have a Spring @Service class (MileageFeeCalculator) that has an @Autowired field (rateService), but the field is null when I try to use it. The logs show that both the M

Save files in database with entity framework

Save files in database with entity framework

I have an ASP.NET MVC solution built on Entity Framework with Microsoft SQL Server 2008. I need to create a function that lets my users upload files. What I would like is:A solution that uses the Entity Framework to store files in the Datab

Java DateFormat parse() doesn't respect the timezone

Java DateFormat parse() doesn't respect the timezone

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");df.setTimeZone(TimeZone.getTimeZone("America/New_York"));try { System.out.println(df.format(cal.g

Get rid of leading zeros for date strings in Python? [duplicate]

Get rid of leading zeros for date strings in Python? [duplicate]

This question already has an answer here: Python strftime - date without leading 0? 16 answers Is there a nimble way to get rid of leading zeros for da

How to find out which .c file contains the .c functions of R internals, on Windows?

How to find out which .c file contains the .c functions of R internals, on Windows?

I want to view the source code of R's match.call function. As it is an internal function, I downloaded the R source code, went to ./src/main/names.c and looked for match.call there. Thus, I found out that the corresponding .c function is ca

Saving in-memory H2 database to disk

Saving in-memory H2 database to disk

How can I save/load full embedded h2 in-memory database to some file or directory in binary mode for faster loading.I want to use this for caching data so I don't have to run all the lines of create table/insert clauses every time. 1:Ins

Disable stylecop analysis for specific projects within solution

Disable stylecop analysis for specific projects within solution

Is there a way to easily stop StyleCop warnings from being displayed within specific projects in a solution. Or, more pointedly, a way to select which projects StyleCop analysis should be performed on by Visual Studio? We have a solution wi

adding UITableView inside UITableViewCell

adding UITableView inside UITableViewCell

I am working on custom UITableViewCell where I want to add UITableView inside UITableViewCell so is there any controller available to do the sameThis is the image of what I want to add in my projectThis is expandable UITableView where after

Problems filtering django datetime field by month and day

Problems filtering django datetime field by month and day

Can someone explain to me why the following filters are not working at the month and day level? Filtering by year seems to work, but not the other two.>>> clicks.count()36>>> date = clicks[0].created>>> date.month2>>> date.year2014>>> date

Jackson deserialize JsonIdentityReference (alwaysAsId = true)

Jackson deserialize JsonIdentityReference (alwaysAsId = true)

Following up on this question: Question [email protected](alwaysAsId = true) and @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class) works great from the Serialization end, but not so well when it comes time t

How does MongoDB deal with concurrent updates?

How does MongoDB deal with concurrent updates?

I started to use MongoDB at work so far so good.I was wondering though how does MongoDB deal with concurrent updates ?I've read that there is no locking feature in MongoDB so I was wondering what is the the common practice to deal with this

Copy a linked list

Copy a linked list

typedef struct Node{ int data; Node *next; Node *other;};Node *pHead;pHead is a singly linked list. The next field points to the next element in the list. The other field may point to any other element (could be one of the previous nodes