Category Archives: c#

Why c++ is not pure object oriented language?

I have just been asked why c++ is not pure object oriented language.

Well the answer is simple: – C++ supports object-oriented programming, but OO is not intrinsic to the language. In c++ you can write a valid, well-coded, excellently-styled C++ program without using a single object, while in Java or c# you cannot write anything without using classes and objects. For example:

class HelloWorld  {
public static void main(String[] args) {
System.out.println("Hello World!"); 
}
}

and c++

int main ()
{
cout << "Hello World!";
return 0;
}

There a good article related to OOPSLA: http://www.stroustrup.com/oopsla.pdf

 

Launch another app from metro style app

In Windows(phone) 8 you cannot run other external apps just like that. As seen in Windows 8, Microsoft is moving away from allowing an application to start any other application.  The recommended method of starting another process and that’s to use the File Asssociations. There is some old similar example and a new one.

On the desktop side this

Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.FileName = "C:\SomeApp.exe";
    p.Start();

or this

   ProcessStartInfo startInfo = new ProcessStartInfo("C:\SomeApp.exe");
    Process.Start(startInfo);
    startInfo.Arguments = "123";
    Process.Start(startInfo);

would work fine but for Windows 8 we have to use Windows.System.Launcher class.

Basically you will need to “launch” a file or an uri then you will have the option to choose how it will be run if not configured yet.

  var someFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\somedata.xml";); 
  bool success = await Windows.System.Launcher.LaunchFileAsync(someFile);

 

get listeners in c#

Here it is an example how to get listeners in c#:

Data d = new Data();
d.OnSave += delegate { Console.WriteLine("onSave event"); };
var handler = typeof(Data).GetField("OnSave", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(d) as Delegate;
var subscribers = handler.GetInvocationList();
if (subscribers.Count() == 0)
{
//no listeners added
}
else
{
//invoke them
foreach (var s in subscribers)
{
s.DynamicInvoke(null, null);
}
}

 

Handy c# snippets

string to byte[]

static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}

byte[] to string
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}

byte[] = byte[] concat byte[]

 

private byte[] Combine(byte[] a, byte[] b)
{
byte[] rv = new byte[a.Length + b.Length];
System.Buffer.BlockCopy(a, 0, rv, 0, a.Length);
System.Buffer.BlockCopy(b, 0, rv, a.Length, b.Length);
return rv;
}

private byte[] Combine( params byte[][] arrays )
{
byte[] rv = new byte[ arrays.Sum( a => a.Length ) ];
int offset = 0;
foreach ( byte[] array in arrays ) {
System.Buffer.BlockCopy( array, 0, rv, offset, array.Length );
offset += array.Length;
}
return rv;
}