Ans 1: 3 Hours Ans 2: 1 Hour 20 Minutes -- Peter Stephens
Peter Stephens
Posts
-
Yet another little puzzle... -
How to import C# dll dynamicallyYes, but yours has better examples :-D -- Peter Stephens
-
How to import C# dll dynamicallyI personally have not tried this, but this might point you in the right direction: Try assem = System.Reflection.Assembly.Load(...) to load your assembly (C# dll). Then t = assem.GetType(...) to get your type (class, etc.) Then use t.InvokeMember(...) to call a method You can also get a ConstructorInfo object through the Type object and create an instance of your class through ConstructorInfo.Invoke(...) -- Peter Stephens
-
Compile error, help!Thanks, James, for the link to those two DotNet mailing lists. I'm already signed up! :-D -- Peter Stephens
-
Resource file (.resource/.resx) help/howtoBTW, the prefix to access the file is actually the default namespace for the project. If you blank out your default namespace, you will then access the icon by just "Icons". -- Peter Stephens
-
Playing sounds through c#Profox Jase wrote: public static extern long PlaySound(String lpszName, long hModule, long dwFlags); Your problem is the long type. In C# this is a 64 bit int. You should use int instead. -- Peter Stephens
-
Playing sounds through c#Asynchronous calls in .NET use delegates. There are methods on a delegate called BeginInvoke and EndInvoke. I personally have not used async calls yet, but plan to do so soon. :) AFAIK .NET async calls use a threadpool behind the scenes. -- Peter Stephens
-
Playing sounds through c#Or you could use an Asynchronous method call or make your synchronous call on a different thread. -- Peter Stephens
-
Populating and Viewing ArraysTo find the length of an array, use the Length property:
String[] arr = new String[5];
Console.WriteLine("The array length is {0}.", arr.Length);When you exceed the capacity of your array, you will have to allocate a new array and copy the contents of the old array to the new. You can automate this process by using the System.Collections.ArrayList class which will resize the array when necessary. -- Peter Stephens
-
AxHost, but on C site...I read that there was support for exposing C# controls as ActiveX in early betas, but that feature was dropped before (at least) RC1. Sorry. -- Peter Stephens
-
C# and .NET booksYes, that's the book I'm interested in (couldn't think of the author at the time.) In the mean time, I am reading the source code for the .NET framework (also known as ILDASM) and hacking away. -- Peter Stephens
-
drawing an icon on a formJust looking at form designer generated code I get this:
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
I seems like it gets the icon out of the associated resx file. -- Peter Stephens
-
C# and .NET booksI'm holding out for the .NET book by Jeff Prosise. According to Amazon its coming out in May. You can read the daily progress in Jeff's BLOG. I am also waiting for a good book on .NET remoting (esp. the internals). As the language C# goes, I had no problem learning it through the MSDN docs. Aside from the .NET framework, C# is little more than syntax. If you know how to utilize the .NET framework you will understand how to program in C# (minus a few minor details). -- Peter Stephens
-
Flaky VS.NET DesignerI had a terrible time getting my control icons to work. I finally discovered that VS.NET was caching the icons based on the assembly version. I had no AssemblyInfo.cs file (or its equivalent) and so my version stayed at 0.0.0.0 and my icon was not changing. Once I set my version to 1.0.0.* my icons worked properly. Otherwise I have no problem using controls from one project in another project under the same solution and under the same instance of the IDE. I just have to remember to build my control assembly before trying to use it (ie in the Form Designer of another project). -- Peter Stephens
-
Naming ConventionI don't know about other peoples standards--this is what I use: private instance fields: _camelCasing private static fields: s_camelCasing public instance/static properties: MixedCase any methods: MixedCase any classes: MixedCase local variables: camelCasing local parameters: camelCasing controls on a form: _camelCasing (because after all, they are just private fields) Since there are no global functions or variables in C#, you can make various assumptions in your code. If you have the following snippet:
variable = "value";
Then you know that you are either have a local parameter to the current method, a local variable, an instance field/property, or a static field/property. Local parameters and local variables can be treated in a similar way (because they are all local to the current method). I use camel casing preceded with an underscore for private instance fields (so that I don't have to deal with name clashes). I precede static fields with "s_" for the same reason. The VS.NET IDE provides tooltips with detailed information about the variable/type that the mouse hovers over. Embedding the type into every variable name becomes somewhat redundent. -- Peter Stephens
-
how to get the address of a managed typeLike this:
SomeObject obj = new SomeObject();
Component1.Prop1 = obj;
Component2.Prop2 = obj;All objects are refered to through "object references". You can freely pass this object reference to any part of your system. -- Peter Stephens
-
a public class... for what?"public" is contrasted with "internal". public classes can be accessed outside of the assembly that contains them. internal classes can only be accessed by other classes in the same assembly. -- Peter Stephens
-
Interface arraysIt works fine on my RC1 machine.
namespace Test
{
public interface ITest
{
int GetTest();
}public class EntryPoint { public static void Main() { // Compiles fine--haven't actually run it... ITest\[\] test = new ITest\[3\]; } }
}
-- Peter Stephens
-
C# ?It's something of a cross between Java and C++. It is built on Microsoft's Common Language Runtime (CLR) (and so is VB.NET) which gives it Just In Time compiled characteristics. It is garbage collected, so you don't have to worry about long running programs fragmenting memory. Every type can be converted to an object (the universal base class) though it supports true value types as well (through a process called boxing). It supports multi threading. Array bounds and Null references are always checked. It is impossible to reinterpret_cast any object reference. The only casting analogs are static_casts which can be implicit and dynamic_casts which must be explicit. Now Visual C++ 7 and Visual Basic 7 both target the CLR, so why would you want to learn C#? The answer is that while VB7 and VC++7 support the CLR, they do so in a backwards compatible way. In VB, you must remember to define all method parameters as ByVal because that performs the best in the CLR, but the default is ByRef which was the original VB behavior. In C++ (I'm not quite as familier with C++ CLR code) you must use many nasty looking prefixes such as __managed. C# on the other hand was designed from the ground up to support the CLR. This means that all the syntactical default values are the same as the CLR defaults. Method parameters are always passed by value unless you use the 'ref' or 'out' keywords. The syntax is similar to C++ but IMHO cleaner in some areas. One feature that ppl want that is not present in the current version is templates. There are rumors that generic programming might be present in V2 of C# though. Bottom line: If you need to do web programming or business programming, C# is reliable and performs well too (or at least has good features for scaling across servers). If you need to write a high performance game or a ray tracer, you will probably be better off using C++. -- Peter Stephens
-
Global database connection string in Windows Form ApplicationI use a static method to retrieve my database connection:
using System;
using System.Data.SqlClient;namespace DbUtils {
public class DataTools
{
public static String username = null;
public static String password = null;public static SqlConnection GetConnection() { String connection = "database=mydb; network address=myserver; network library=dbnmpntw; " if(username != null) connection += "user id=\\"" + username + "\\"; "; if(password != null) connection += "password=\\"" + password + "\\"; "; return new SqlConnection(connection); } }
}
And then elsewhere in your app you just get a connection like:
SqlConnection conn = DbUtils.DataTools.GetConnection();
-- Peter Stephens