Search

Get the Computer, User and domain name using Javascript

This come quite easy, you just have to initialize an ActiveX object for "WScript.Network"
 
/* Jscript - ComputerName, Username & DoaminName */

var oWshNet = new ActiveXObject("WScript.Network");
var computerNmae = oWshNet.ComputerName;
var userName = oWshNet.UserName;
var domainName = oWshNet.UserDomain;


Note: This works only with ActiveX in IE.

Interface in C#

An interface contains only the signatures of methods, delegates or events.
  • The implementation of the methods is done in the class that implements the interface, as shown in the following example.
  • An interface can be a member of a namespace or a class and can contain signatures of Methods.properties.Indexes & Events.
  • An interface can inherit from one or more base interfaces.
  • When a base type list contains a base class and interfaces, the base class must come first in the list.
  • A class that implements an interface can explicitly implement members of that interface.
  • An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    interface ISampleInterface
    {
    void SampleMethod();
    }

    namespace Tutorial
    {
    class Program : ISampleInterface
    {
    // Explicit interface member implementation:
    void ISampleInterface.SampleMethod()
    {
    // Method implementation.
    Console.WriteLine("Method implementation.");
    }

    static void Main(string[] args)
    {
    // Declare an interface instance.
    ISampleInterface obj = new Program();
    // Call the member.
    obj.SampleMethod();
    Console.ReadLine();
    }
    }
    }





    The following example demonstrates interface implementation. In this example, the interface IPoint contains the property declaration, which is responsible for setting and getting the values of the fields.

    The class Point contains the property implementation. random



    using System;
    using System.Collections.Generic;
    using System.Text;

    interface IPoint
    {
    // Property signatures:
    int x
    {
    get;
    set;
    }
    int y
    {
    get;
    set;
    }
    }// interface

    class Point : IPoint
    {
    // Fields:
    private int _x;
    private int _y;
    // Constructor:
    public Point(int x, int y)
    {
    _x = x;
    _y = y;
    }
    // Property implementation:

    public int x
    {
    get
    {
    return _x;
    }
    set
    {
    _x = value;
    }
    }
    public int y
    {
    get
    {
    return _y;
    }
    set
    {
    _y = value;
    }
    }
    }// class

    namespace IndiLogiX.Tutorial
    {
    class Program
    {
    static void PrintPoint(IPoint p)
    {
    Console.WriteLine("x={0}, y={1}", p.x, p.y);
    }
    static void Main(string[] args)
    {
    Point p = new Point(2, 3);
    Console.Write("My Point: ");
    PrintPoint(p);
    Console.ReadLine();
    }
    }
    }



  • Collection Class in C#

    This tutorial shows how to implement a collection class that can be used with the foreach statement.

    The foreach statement is a convenient way to iterate over the elements of an array. It can also enumerate the elements of a collection, provided that the collection class has implemented the System.Collections.IEnumerator and System.Collections.IEnumerable interfaces.

    Example 1

    The following code sample illustrates how to write a collection class that can be used with foreach. The class is a string tokenizer, similar to the C run-time function strtok.

     

    // tokens.cs
    using System;
    // The System.Collections namespace is made available:
    using System.Collections;

    // Declare the Tokens class:
    public class Tokens : IEnumerable
    {
    private string[] elements;

    Tokens(string source, char[] delimiters)
    {
    // Parse the string into tokens:
    elements = source.Split(delimiters);
    }

    // IEnumerable Interface Implementation:
    // Declaration of the GetEnumerator() method
    // required by IEnumerable
    public IEnumerator GetEnumerator()
    {
    return new TokenEnumerator(this);
    }

    // Inner class implements IEnumerator interface:
    private class TokenEnumerator : IEnumerator
    {
    private int position = -1;
    private Tokens t;

    public TokenEnumerator(Tokens t)
    {
    this.t = t;
    }

    // Declare the MoveNext method required by IEnumerator:
    public bool MoveNext()
    {
    if (position < t.elements.Length - 1)
    {
    position++;
    return true;
    }
    else
    {
    return false;
    }
    }

    // Declare the Reset method required by IEnumerator:
    public void Reset()
    {
    position = -1;
    }

    // Declare the Current property required by IEnumerator:
    public object Current
    {
    get
    {
    return t.elements[position];
    }
    }
    }

    // Test Tokens, TokenEnumerator

    static void Main()
    {
    // Testing Tokens by breaking the string into tokens:
    Tokens f = new Tokens("This is a well-done program.",
    new char[] {' ','-'});
    foreach (string item in f)
    {
    Console.WriteLine(item);
    }
    }
    }