You are here: ApiroTech > Programming > C-Csharp

 
 
 

C-Csharp

PREVIOUS     NEXT

C++: Constructor

 The implementation of C++'s many constructors types.The classic constructor is still available in C++, but they also offer a few additions to allow for shortened code, and fewer constructors. *I will write the following constructors as if they were in a class called Constructor. with 3 global fields to be initialized name, size, and text. Default Constructor: Constructor() {      name = "";      size = 0;      text = ""; } -takes 0 parameters, and initializes them as any other language could. Parameter List Constructor: Constructor(String n, int s, String t){      name = t;      size = s;      text = t; } -again similar to other languages, but this kind of work is not necessary, this is one of many places C++ shines. Paramenter List Constructor 2: Constructor(String n, int s, String t):name(n),size(s),text(t... Read More

C++: Destructor

 The implementation of C++'s Destructor efficientlyWhen writting a destructor it is important to understand why and what before just writting it. *a structure and pointer recipe are on the way to explain anything thing complex here. The point or rather to not point. A destructor removes pointers in the case of manually watching your memory leak. This method is automaticlally called whenever an object loses scope. Scope - part of the code (eg, method, class, etc) in which a variable or parameter can be accessed. thus, it takes care of all local and dynamic variables for us, preventing memory leak if it is written properly. *The example will be written as if it were inside a class which has a structure within it implementing a linked list by the pointer: Next* next; which of course the struct is called Next. Destructor Syntax: ~Destructor() {} -A destructor never takes any parameters, it's only parameter is the implied one accesable by this. -always starts with a ~, tild... Read More

C++: Pointers, Pass by Value, Pass by Reference

 You just can't properly explain pointers, without first knowing the basics of passing by value/reference.I'm sure the odd person who reads this will say "but you can also pass a pointer" while this is all well and true, the pointer passed is still a primitive variable and will in turn either be passed by reference or by value. When passing a value, in eithe case, the call will look the same, it's the recieving method which determines how the pass is made. By Value: void aMethod(aParameter p) { } When passing by value a temporary COPY is made in memory taking up space and making any changes to the variable useless outside of the aMethod scope. Once control returns to the calling procedure any changes to the variable will not be recognized, and you will still have the original variable. By Reference: void aMethod(aParameter& p) { } NOTE: the & can be place either against the parameter type, against the parameter name, or there can be a space on either ... Read More

C++: Template Classes

 How to layout a template class and what it's good for.Template class is exactly what it seems, a template. It allows you to define a class and involve variables of unknown type T. Thus the class can be implemented as a universal class for any group of elements sharing a class or base class. A stack is a good example which can be used for many types. CODE Example: template <class T> class Stack {        int max;    T** rep;            // a dynamic array of pointers to T    int size; public:    Stack(int);            // a constructor    ~Stack( ) {delete [ ] rep;}   // destructor       bool empty( );    void push(T&);    T& pop( ); }; You could add many more methods to this to expand the explanation, but this shows the use. ... Read More

C/C++ Doubly Linked List

 The implementation of a doubly linked list, remove prev pointer for a singly linked list struct Node{ public:    Member*      info; //type is arbitrary, to the linked list implementation    Node*      next; //used to point to next node         Node*           prev; //used to point to previous node    Node():prev(NULL),next(NULL) {} //default constructor         Node(Node* n):prev(n),next(NULL) {} //prev known constructor         Node(Node* n, Node* m):prev(n),next(m) {} //prev and next known constructor    ~Node() { delete info; delete next; } //destructor }; this list would be held by a pointer: Node* head; head = &(new Node(&head);//create Node which knows prev&... Read More

Pass Variables to a New Thread in C#

 When you create a new thread in .net 1.1, you cannot pass any parameters to the ThreadStart delegate, which makes passing startup variables difficult. This recipe shows you an easy workaround.Since the ThreadStart delegate doesn't accept parameters, you need to set the parameters somewhere before you create the new thread. What we'll do is create a small class to store the variables, and then create a function in the class to pass into ThreadStart. class myObject {    public string myvariable;    public void RunThread()    {       // use myvariable here    } } You should really use properties instead of a public variable, but this makes the code sample simpler. The method that you pass into ThreadStart must be void, and accept no parameters. Here's the sample code for creating the object, passing in a variable, and then using the object to create the new thread: myObject m = new myObject(); m.myv... Read More

Load an Icon from an Embedded Resource in .NET

 When you are developing a Windows Forms application in .NET, it's not immediately obvious how to programatically load an icon file embedded in your executable. This recipe shows you the 1 line solution.Your icon file should be a regular windows icon file. Add it to your project, and in the properties for the icon make sure that it is set to Embedded Resource. Now in your code, add the following line: Icon theIcon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.Filename.ico")); Replace "MyNameSpace" with the namespace of your application, and replace "Filename" with the name of your icon file. You should now be able to use the icon directly in any windows control that uses icons, like the NotifyIcon control.... Read More

Open a URL in the Default Browser in Your .NET Application

 Virtually every application these days requires at least some connection to the internet. Letting the user click a link and open it in their browser is essential. This recipe shows you the 1 line method of doing so.This is the absolute simplest way to launch a URL in a .NET application: System.Diagnostics.Process.Start("http://www.tech-recipes.com"); Just put this code in the click event for your control, and replace the URL in the sample code with your URL.... Read More

Make System Tray Based Application in .NET

 Creating an application that lives in the system tray is easy, but it is not entirely obvious how to do so in Visual Studio. This recipe will show you the steps.1) First, create a new Windows Application project in Visual Studio.NET. This can be either Visual Basic or C# 2) Drag a Notifyicon control and a ContextMenu control from the toolbox onto the form. 3) Click on the NotifyIcon control that you just added, and set the Icon property to whatever icon you want your application to have. 4) Set the ContextMenu property of the Notifyicon to the context menu that you added to your project. 5) Right click on the Context Menu control, and select Edit. Since this menu will be the right-click menu for your tray icon, you will want to add the items that the user will see. Make sure to add an Exit menu item. 6) Double click the Exit menu item, and add the following code: this.Close(); 7) Now for the important settings. Click on the form, and go to the Properties window. Set the followi... Read More

Pause Output From a Command Line Application in .NET Debug Mode

 If you are writing a command line application in .NET, you might notice that the application closes immediately if you haven't set a breakpoint. This recipe shows you the one line method to keep the application open so you can see the output.Typically when you are writing to the console, you use the Console.Write() method, but there is also a Read method to go along with it. If you place this line of code in your application, it will pause and wait for a keystroke before continuing: Console.ReadLine(); If you are using Visual Basic instead of C#, just omit the semicolon.... Read More

Get Web Page Contents in Code with C#

 When you are developing an application that needs to access data stored on a web server, you can easily get the contents of a web page with this simple C# function.The .NET framework provides a rich set of methods to access data stored on the web. First you will have to include the right namespaces: using System.Text; using System.Net; using System.IO; The HttpWebRequest object allows us to create a request to the URL, and the WebResponse allows us to read the response to the request. We'll use a StreamReader object to read the response into a string variable. Here's the actual code:    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);    myRequest.Method = "GET";    WebResponse myResponse = myRequest.GetResponse();    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);    string result = sr.ReadToEnd();    sr.... Read More

 

 

Pages : 1