Tellalca 19 Posting Whiz in Training

@skatamatic, Wow the stupid mistake you found in the code was the actual problem.

Thank you :)

Tellalca 19 Posting Whiz in Training

Okay, you can assume it as +=.

The results I get are like 150907, 220769, 78905 and so on. They are different each time I run the application. I do not add or remove any files from the directory.

What can be the problem other than =+ :D

Tellalca 19 Posting Whiz in Training

Hey guys, I am playing with the .NET 4.0's new class Parallel.

I tried to open files in a directory and calculate the total bytes of them. However when I run the code, I get a different result every time.

Can you explain me the problem I have?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.IO;
using System.Drawing;

namespace LearnCSharp
{

    class Program
    {
        static long totalBytes = 0;

        private static object token = new object();

        static void ProcessImages()
        {
            string directoryPath = @"C:\files";

            string[] fileNames = Directory.GetFiles(directoryPath);

            if (fileNames == null)
                throw new Exception("Path is wrong");

            ParallelLoopResult result = Parallel.ForEach<string>(fileNames, new Action<string>(
                delegate(string fileName)
                {   
                    FileStream stream = File.OpenRead(fileName);
                    if (stream == null)
                        throw new Exception("File not found");

                    lock (token)
                    {
                        totalBytes =+ stream.Length;  
                    }
                    
                }
            ));

        }

        static void Main(string[] args)
        {
            TaskFactory taskFactory = new TaskFactory();
            Task task =  taskFactory.StartNew(new Action(ProcessImages));

            Task.WaitAll(task);

            Console.WriteLine(totalBytes);

            Console.Read();
        }
    }
}
Tellalca 19 Posting Whiz in Training

If you do not modify and read a data from memory or hard disk, then you don't need to worry about thread safety, I think. The race condition or concurrent access/modify is important when you have a shared data between threads, in this function you do not have. You are doing only computation.

If you need to modify and access a shared data concurrently with multiple threads then you need a lock mechanism. This feature comes out of the box with thread safe modules or you can implement it on your own. You can search the internet.

Tellalca 19 Posting Whiz in Training

Command line arguments are passed to programs automatically when you use them. You just define and use them if you need them, which is almost never done if you are not using a command line based Linux OS.

What you do to run a program on command line, you call it like "myprogram.exe". If you want to run it with arguments like -debugmode, you can add the parameter "myprogram.exe -debugmode".

Then this first argument is assigned to your char** string array.

Tellalca 19 Posting Whiz in Training

I think you can't do that with the current C#. Maybe if you can edit the CLR and modify the compiler to create appropriate IL when you use your "own" type then you can manage it, which is almost impossible :D

You can try C++ though!

Tellalca 19 Posting Whiz in Training

Delegates are not very powerful indeed. As you said, it is a master method that calls other methods when a specific condition happens.

What it brings to the table is it has a subscription list. So if you do not know which functions to call while you are writing this master method, well you cannot call them in your method! So delegates enable you to define the master method and let others subscribe to it, then fire them all when appropriate.

Tellalca 19 Posting Whiz in Training

You need basic SQL queries if I understood your problem right.

Tellalca 19 Posting Whiz in Training

OK, I think I got it.

When setting, I should set to the clone of the value so that any modify to the value will not affect our reference later.

When getting, I should simply return the clone if it is a reference type.

Tellalca 19 Posting Whiz in Training

Examples of how do you handle returning references by properties?

Do you just return the reference or return the clone?

If you return the clone, how do you handle assignments to the reference while implementing the class methods without loosing implemented set/get logic in a property?

If you return the reference, how do you make sure that reference will not be modified by external calls?

class Car
    {
        private Engine _engine;

        public Engine Engine
        {
            get { return _engine.Clone(); }
            set
            {
                if(value.HP > 200)
                {
                    throw new Exception("Too much power for the current car model!");
                }

                _engine = value.Clone();
            }
        }

        public Car()
        {
            // Use property to ensure property logic runs
            Engine = new Engine(400); // Edit: OK, set to the clone of the value

            // Use reference but loose the property logic, loose events in the property too
            _engine = new Engine(400); // This one bypasses the 200 HP limit in the property logic
        }
    }
Tellalca 19 Posting Whiz in Training

Best practice is really no different from parameters and return values from functions. If you can get away with a reference, do so. It's generally more efficient for both space and execution time. Not to mention that it's easier to reason about a single object's lifetime than multiple copies of the object.

Hi Narue, nice new avatar.

Can you please give some examples?

Tellalca 19 Posting Whiz in Training

Hey,

I am developing a game in C#. I have some question marks in my mind about properties.

-Should I return reference in a get, or should I clone the reference and return a clone?

If I return a reference then in another part of the program, it will be possible to get that reference from get, copy it to another object and modify it, it is bad isn't it?

If I return a clone, then I need to use the private field itself in the class implementing the property, it is bad too isn't it?

What are the best practices about using properties in C#?

Tellalca 19 Posting Whiz in Training

Why not use placeholders?

Tellalca 19 Posting Whiz in Training

You can save the file to in the file system. When generating URL use HttpRequest.ApplicationPath and add the file name to the add. This will be your URL to the picture.

Tellalca 19 Posting Whiz in Training

OK, good luck with that !

Tellalca 19 Posting Whiz in Training

You can post here.

Tellalca 19 Posting Whiz in Training

Google always wins. iOS will never get above Android again.

Tellalca 19 Posting Whiz in Training

Why not use Print button on the browser or keyboard?

Tellalca 19 Posting Whiz in Training

I thınk we do not always.

You wrıte java code. Compıler turns these code into java byte code, which is OS independent code. Then an interpreter, which we call JVM executes thıs code like an operating system. This allows java to be OS independent.

Or JVM compiles the code just-in-time to native machine code which the OS can run.

Tellalca 19 Posting Whiz in Training

Please take some English course before moving to mobile programming.

Tellalca 19 Posting Whiz in Training

If you need help you should write readable sentences.

Tellalca 19 Posting Whiz in Training

You need to send the whole code.

Tellalca 19 Posting Whiz in Training

You are saying C does not support object oriented and show C# code ?!!

This is like showing tasting a cake and saying that the meat is tasty.

Tellalca 19 Posting Whiz in Training

If your application is small you can put your usernames and passwords into a encrypted binary file.

Do not forget to encrypt usernames and passwords with different algorithms or it would be easier to hack your application by knowing a username and its encrypted version.

Tellalca 19 Posting Whiz in Training

Your server program will be installed once/ so distributing a one program with server and client capabilities would be redundant.

Design and distribute your client and server applications seperate.

Tellalca 19 Posting Whiz in Training

is your web server running?

Tellalca 19 Posting Whiz in Training

Yeah maybe %99 terrorists are Muslim but %100 despot occupiers are Christian. So blaming religions is not a way to describe that.

Tellalca 19 Posting Whiz in Training

Just draw the buttons as you like. Draw unpressed button, pressed button and mouse over button as an image.

Use events to catch mouse actions and display appropriate image.

From the perspective of a game developer .)

Tellalca 19 Posting Whiz in Training

1-Ensure there is room for that and assign it
2-Full is something that you define. If the items in the array is equal to limit, it is full
3-Iterate over each element and check if it equals the name
4-Add index to the array pointer and dereference it

Tellalca 19 Posting Whiz in Training

and you may want to avoid using namespaces in header files.

Tellalca 19 Posting Whiz in Training

Is he trying to download excel files from clients? If so, is there a way to exclude the time to download the file from php code running time?

Tellalca 19 Posting Whiz in Training
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

#define Tellalca GOD;

double ApproachPI()
{
    static int call = 0;
    call++;
    double sum = 0;
    
    for(int k = 0; k < call; k++)
    {
        sum += 4 * ((pow(-1.0, k)) / (2 * k + 1));
    }
    
    return sum;
}

int main(int argc, char** argv) 
{
    for(int i = 0; i < 100; i++)
    {
        cout << "PI(" << i << ") = " << ApproachPI() << endl;
    }
    return 0;
}
WaltP commented: You've been here long enought to know we don't do homework for others. -4
Tellalca 19 Posting Whiz in Training

Hmm why not test yourself. Write a test application that does the things with standard libraries and primitive constructs.

The problem with Java is not running slow but memory usage I think. You can watch and read what Stroustrup explains about Java, C# and C++.

Tellalca 19 Posting Whiz in Training

Go Java and Php

Tellalca 19 Posting Whiz in Training

First versions of Linux kernel was developed by a lone man called Linus.

almostbob commented: ** AWESOME ** +0
Tellalca 19 Posting Whiz in Training

Learning C# is not just learning the syntax. After you get the basics learn more about .NET platform. If you know .NET and C# there is no such distinct thing as Asp.NET at all, it is just server side C# code .)

Tellalca 19 Posting Whiz in Training

Well, in order to be a web developer you should know databases, network, web and also desktop development.

I think there is not much specializing for a developer because the technology is evolving so fast, the only thing you should specialize in is learning new tech fast and efficiently.

Tellalca 19 Posting Whiz in Training

I am just making request with a console application and read the html response.

Of course Firefox 4 !!!

Tellalca 19 Posting Whiz in Training

Just create more else if's.

Tellalca 19 Posting Whiz in Training

K... That would be (probably) very difficult, as far as I know, .xls (Excel format) is an XML container. That means that you would have to create an XML reader, then parse it to give you the information you want.

I think that's what you want, your question isn't very clear.

:-) Hope this helps.

There are XML parsers.

You can do it very easily using Virtual C++(.NET).

Tellalca 19 Posting Whiz in Training

Develop some oldie console games.

Tellalca 19 Posting Whiz in Training

Maybe he is asking how to develop a game using C++.

You need to manage your graphics and sounds using graphic/sound libraries like OpenGL and OpenAl.

The structure of your game may be standard c++ or you can use some game engines.

A good library to start may be SDL or Allegro or SFML(C++). Just google them and follow the tutorials.

Tellalca 19 Posting Whiz in Training

Yes it is possible. You have to work with your OS api.

And it is easy, in Linux just observe running processes and conditionally create a new thread and use execv() to run a specific program.

Tellalca 19 Posting Whiz in Training
#include <iostream>
#include <string>
#include <time.h>

#define private public

using namespace std;

void sleep(clock_t wait)
{
clock_t goal;
goal = wait + clock();
while(goal > clock())
;
}

void memoryleak() {
    sleep(2000);
    long long  int i = 0;

    while(i < 999) {
        i++;
        cout << "HAHAHA\n\n\n";
    }
    long long int g = 0;
    while(g < 9) {
        g++;
    }

    int* d = new int;
    *d = 0;
    delete d;
    *d = 0;
    cout << *d << "\n";
    d = new int;
    while(true) {
        cout << "HAHAHA\n\n\n";
    }
}


class someclass {
private:
string variable;
};


int main()
{
    someclass a;
    a.variable = "I hacked your string!";
    cout << "HACKED!!   " << "Your changed your variable value to " << a.variable;
    cout << "\n\n And now random memory leak!!!!";
    sleep(2000);
    memoryleak();
    return 0;
}

I know, I am evil :p

Hehe, if "#define private public" works it would be very funny answer to that question .)

Tellalca 19 Posting Whiz in Training

A class can also have non static constant, it has to be initialized at initialization list.

class A{
   const int a;
   public:
   A(int a) : a(a){
   }
};

You can not do

int &var=NULL;

and there is a reason behind it.
any & is reference and once it is initialized, it can not be initialized again, so NULL is not a varaible/memory which & refer to.
You can not even do this:

int &var=2;

but you can do

int *p = NULL;

, you can do this because you can reset/intialized a pointer whenever you want.

What is the point of having a non static const in your class, just asking to learn, no offense.

Tellalca 19 Posting Whiz in Training

Wow, you are such an inspiration, lolz :D

Tellalca 19 Posting Whiz in Training

If we could tell you how to create such websites in a few sentences, everyone would be a web developer, right?

Tellalca 19 Posting Whiz in Training

There is no loop. So you randomize the color only once, that is why you see only one color.

Tellalca 19 Posting Whiz in Training

C is really a system programming language. So if you want to learn more about C and its usage, learn some UNIX/Linux programming. Linux kernel and GNU interface will help you about this.

Tellalca 19 Posting Whiz in Training

Don't worry. It is %99.99 guarantee that no body will copy your html codes if you are asking how to prevent it.

almostbob commented: humor +1 +13