November 4, 2012

?: Operator return void

Question by Artur Keyan

I want to use the ?: operator intstead of if else


var directory = new DirectoryInfo(path);
if (!directory.Exist())
    // do nothing

I tried to use ?: like this:

var directory = new DirectoryInfo(path);
!directory.Exist() ? directory.Create() : void;

but it says “Invalid expression term ‘void’“, null also isn’t working.

Can anyone tell me a solution? Thanks.

Answer by Starx

Ternary operator are not designed to replace if/else They are to simplify assignment and declaration. It should be able to assign the result the statement to something like variable or function. There major use are in assignment or reading.

Something like

var status = !directory.Exist() ? directory.Create() : false;

The correct solution would be to stick to native if condition. The following is enough:

var directory = new DirectoryInfo(path);
if (!directory.Exist())
April 26, 2012

OOP: Which class should own a method?

Question by Frungi

I’m having trouble understanding how classes relate to their methods. Is a method something that the object does, or something that’s done to it? Or is this a different concept entirely?

Specifically, in a library’s software system, should the borrow() method belong to the class representing the library patron, or the class representing the item that the patron is borrowing? My intuition is that it should read like patron.borrow(copy), like English sentence structure, subject.verb(object); but my instructor says that’s Wrong, and I don’t understand why he would have borrow() belong to the Copy class (and he doesn’t really explain things too well). I’m not looking for justification, but can someone just explain the proper relationship?

Edit: This question was closed as “off topic”. I don’t understand. Are software design questions not appropriate for this site?

Answer by Konstantin Oznobihin

Some generic words first.

Software construction is not something which should be governed by English language rules or “beauty” or whatever, it’s engineering discipline. Think of whether your design solves the problem, whether it will be maintainable, whether it will be testable, whether it will be possible to parallelize development and so on. If you want something more formalized take a look at the “On the Criteria To Be Used in Decomposing Systems into Modules” by D. L. Parnas.

As for your library example. Imagine you have a Copy outside of library, shoult it have borrow method then? How the borrowing is registered? Are you ok with either Copy or Patron classes responsible for data storage? It looks more appropriate to put borrow into a Library class. Responsibilities will be clearly divided, you wouldn’t need to know much about borrowing to implement Copy and Patron and you wouldn’t need much details about them to implement Library.

Answer by Starx

Is a method something that the object does, or something that’s done to it? Or is this a different concept entirely?

Let me clear something about class and objects first. Class are generally used to a denote particular category. Like

  1. Cars not Ferrari, or Porsche
  2. Fruits not Banana, or Apple

So, it’s Ferrari that is driven, and a banana that is eaten. Not their class

Its always an object that has properties and has behavior.

Even going to your case specifically.

borrow() method is an action/behavior done by a object of a person on an object of book whose records is kept by another object of the library system itself.

A good way to represent this in OO way for me would be like

libray.borrow(new book('book title'), new person('starx'));

Just for fun, What do you think about this

person starx = new person('starx');
book title1 = new book('title1');
library libraryname = new library('libraryname');

if(starx.request(title1, libraryname)) {
     starx.take(library.lend(title1, starx));
March 30, 2012

jQuery.html() not working well with Asp.Net innerHtml

Question by Umm….

So basically, I have a div which is runat=”server”

<div id="results" runat="server" class="results"></div>

Now, I add HTML to this using jQuery.

$('.results').html('Some crazy data');

However, when I try to access the data in this DIV using C#, it says the DIV has no contents.

string myResults = results.innerHtml;

myResults is empty.

Can someone help me out? I’ve never seen this behavior before.

Many Thanks!

Answer by amit_g

You may be looking for something like this…

<div id="results" runat="server" class="results"></div>
<input type="hidden" id="hiddenResults" runat="server" class="hiddenResults" />

$('.results').html('Some crazy data');
$('.hiddenResults').val('Some crazy data');

Now you can access the data on server

string myResults = hiddenResults.Value;

Answer by Starx

Instead of mixing up jQuery and Javascript. Try

var myResults = $(".results").html();

Just to verify everything is correct, see if your results is defined as below

var results = document.getElementById("results");
var myResults = results.innerHtml;

If you trying to get the value of results to the server, it is not possible through jQuery or Javascript unless send an AJAX request.

September 13, 2011

is it possible to have multiple classes inside php extension?

Question by sunset

I would like to wrapp a .cc code that contains multiple public classes. Is it possible to do that ? how? Do I need to use multiple .cc files one for each class that i want to wrapp?


Answer by Chris

You want to call a C++ class from php? This is incredibly difficult. Usually you have to write a php module (a lot of work). Alternatively you could take a look at Thrift which would let you call your C++ code as a network service (sounds hard, but trust me it’s easier than writing a php module).

Answer by Starx

I wonder what you are attempting to do… if motives were mentioned may be we can help better. Anyways….

To execute a compiled application you can use execute();

$output = exec('/path/to/your/app');

Besides that, you can always write your own php extension….

Check out these tutorials

AFAIK, Many developers use PHP to execute C functions, because it boosts performance quite remarkably.

June 7, 2011

How to save pictures in MySQL

Question by JKAUSHALYA

I want to save pictures(mostly JPEG) to MySQL database. I saw most people say save pictures elsewhere and add link to table. It is the most efficient way. But i need to encrypt my pictures and want to set user privileges. So how can i do this. Please can anyone help me.

I’m using a C client program to connect to the MySQL.

Answer by genobis

Not a good idea, but if you really have to do it this way, use BLOBs (a data type).

Answer by Starx

Yes, do not insert pictures to the database table. It reduces manageability heavility. Also, when the database increases, the performance is reduced significantly. Let DBMS manage data and let File Management System manage files.
Better, save the pictures on a table and then later only retrieve the images from a folder, once a valid user is found.

June 14, 2010

method vs function vs procedure vs class?

Question by 2easylogic

I know the basics of this methods,procedures,function and classes but i always confuse to differentiate among those in contrast of Object oriented programming so please can any body tell me the difference among those with simple examples ?

Answer by JUST MY correct OPINION

A class, in current, conventional OOP, is a collection of data (member variables) bound together with the functions/procedures that work on that data (member functions or methods). The class has no relationship to the other three terms aside from the fact that it “contains” (more properly “is associated with”) the latter.

The other three terms … well, it depends.

A function is a collection of computing statements. So is a procedure. In some very anal retentive languages, though, a function returns a value and a procedure doesn’t. In such languages procedures are generally used for their side effects (like I/O) while functions are used for calculations and tend to avoid side effects. (This is the usage I tend to favour. Yes, I am that anal retentive.)

Most languages are not that anal retentive, however, and as a result people will use the terms “function” and “procedure” interchangeably, preferring one to the other based on their background. (Modula-* programmers will tend to use “procedure” while C/C++/Java/whatever will tend to use “function”, for example.)

A method is just jargon for a function (or procedure) bound to a class. Indeed not all OOP languages use the term “method”. In a typical (but not universal!) implementation, methods have an implied first parameter (called things like this or self or the like) for accessing the containing class. This is not, as I said, universal. Some languages make that first parameter explicit (and thus allow to be named anything you’d like) while in still others there’s no magic first parameter at all.

Edited to add this example:

The following untested and uncompiled C++-like code should show you what kind of things are involved.

class MyClass
  int memberVariable;

  void setMemberVariableProcedure(int v)
    memberVariable = v;

  int getMemberVariableFunction()
    return memberVariable;

void plainOldProcedure(int stuff)
  cout << stuff;

int plainOldFunction(int stuff)
  return 2 * stuff;

In this code getMemberVariableProcedure and getMemberVariableFunction are both methods.

Answer by Starx

Procedures, function and methods are generally alike, they hold some processing statements.

The only differences I can think between these three and the places where they are used.

I mean ‘method’ are generally used to define functions inside a class, where several types of user access right like public, protected, private can be defined.

“Procedures”, are also function but they generally represent a series of function which needs to be carried out, upon the completion of one function or parallely with another.

Classes are collection of related attributes and methods. Attributes define the the object of the class where as the methods are the action done by or done on the class.

Hope, this was helpful

June 3, 2010

How to fetch the value of selected checkbox inside a checkboxList?

Question by Starx

I want to know the selected value of the markup below. So that I can disabled a textbox, if one of the checkbox is selected.

    <asp:CheckBoxList ID="ChkTest" runat="server" RepeatDirection="Horizontal" CssClass="toggleYesNo">
        <asp:ListItem Value="1">Yes</asp:ListItem>
        <asp:ListItem Value="0">No</asp:ListItem>

I tried using this function it doesnot seem to work

$(document).ready(function() {
    $("#<%=ChkTest.ClientID %>").click(function() {
        value = $(this).val();
        if(value=='1') {
         else {


I also track the output HTML but the id the CheckBoxList the assigned to a table instead.


<table id="ChkTest" class="toggleYesNo" border="0">
        <td><input id="ChkTest_0" type="checkbox" name="ChkTest$0" /><label for="ChkTest_0">Yes</label></td><td><input id="ChkTest_1" type="checkbox" name="ChkTest$1" /><label for="ChkTest_1">No</label></td>

Answer by Starx

Ok, I solved it

my jQuery function is

$(document).ready(function() {
    $("#<%=ChkTest.ClientID %> input").click(function() {
        value = $(this).attr('checked');
        if(value==true) {
        else {

This totally solved it


Please fill the form - I will response as fast as I can!