Click here to Skip to main content
15,922,696 members
Home / Discussions / C#
   

C#

 
GeneralCLI available at MSDN site Pin
27-Mar-02 21:14
suss27-Mar-02 21:14 
GeneralRe: CLI available at MSDN site Pin
James T. Johnson27-Mar-02 21:56
James T. Johnson27-Mar-02 21:56 
GeneralRe: CLI available at MSDN site Pin
Kannan Kalyanaraman27-Mar-02 23:01
Kannan Kalyanaraman27-Mar-02 23:01 
GeneralRe: CLI available at MSDN site Pin
James T. Johnson28-Mar-02 20:32
James T. Johnson28-Mar-02 20:32 
GeneralC# Process "exited" event Pin
sil27-Mar-02 14:29
sil27-Mar-02 14:29 
GeneralUsing resources Pin
27-Mar-02 9:29
suss27-Mar-02 9:29 
GeneralRe: Using resources Pin
James T. Johnson27-Mar-02 14:36
James T. Johnson27-Mar-02 14:36 
GeneralRe: Using resources Pin
28-Mar-02 3:38
suss28-Mar-02 3:38 
No, it doesn't. What is required here is a namespace:

This is an example from Mr. Petzold:

Icon=new Icon(typeof(ProgramWithIcon),"ProgramWithIcon.ProgramWithIcon.ico");

The first argument of the constructor refers to ProgramWithIcon class. Within that type operator, you can use the name of any class that your program defines. Or you can use the name of any structure, enumeration, interface, or delegate that you program defines.

In any code in the ProgramWithIcon class, the expression:
typeof(ProgramWithIcon)
is equivalent to:
GetType()

This equivalence means that you can use the somewhat shorter constructor:
Icon=new Icon(GetType(),"ProgramWithIcon.ProgramWithIcon.ico");
And the program still works the same.

The second argument to the Icon constructor is more or less a filename. If you named the icon MyIcon.ico, the Icon constructor would look like this:
Icon=new Icon(GetType(),"ProgramWithIcon.MyIcon.ico");

The first part of the quoted name is called a namespace, but it’s a resource namespace. Don’t confuse it with the .NET Framework namespace. By default, Visual C# .NET gives this resource namespace the same name as the project, but you can change it. It’s the field labeled Default Namespace in the Property Pages dialog box for the project. The name in that field must agree with the first part of the quoted name in the Icon constructor. You can even set Default Namespace field to nothing, in which case the second argument to the Icon constructor is just the bare filename:

Icon=new Icon(GetType(),"ProgramWithIcon.ico");

or MyIcon.ico or whatever you’ve named the file.

If you’re running the C# compiler from the command line, you use the /res switch for each resource. For example, if you use the compiler switch:

/res:ProgramWithIcon.ico

you load the icon like so:

Icon =new Icon(GeType(),”ProgramWithIcon.ico”);

Or you can give the icon an extended name following the filename and a comma:

/res:ProgramWithIcon.ico,ProgramWithIcon.ProgramWithIcon.ico

You then use the constructor:

Icon =new Icon(GeType(),”ProgramWithIcon. ProgramWithIcon.ico”);

to load the icon.

Here’s a problem you might run into if you just use the default resource namespace name that Visual C# .NET assigns to your project: Suppose you create a new project named ProgramWithIconPlus in which the ProgramWithIconPlus inherits from the ProgramWithIcon class. In the ProgramWithIconPlus project, you create a new file named ProgramWithIconPlus.cs and you also add a link to the existing ProgramWithIcon.cs file. But you decide not to create a new icon for the new program. Instead, you create a link in the ProgramWithIconPlus project to the ProgramWithIcon.ico file. The constructor in the ProgramWithIcon class continues to load the icon.

And what happens when the program tries to load the icon? It terminates with an exception. So what’s going on? The statement in the ProgramWithIcon constructor to load the icon looks like this:

Icon=new Icon(typeof(ProgramWithIcon),”ProgramWithIcon.ProgramWithIcon.ico”);

But the default resource namespace for the ProgramWithIconPlus is ProgramWithIconPlus, not ProgramWithIcon. The simple solution? Change the Default Namespace field in the ProgramWithIconPlus project to ProgramWithIcon. Or make all the Default Namespace fields blank and use the naked filename in the constructor.

Jerzy

GeneralCoding Standard and Code Review Pin
Michael P Butler27-Mar-02 9:17
Michael P Butler27-Mar-02 9:17 
GeneralRe: Coding Standard and Code Review Pin
James T. Johnson27-Mar-02 19:57
James T. Johnson27-Mar-02 19:57 
GeneralRe: Coding Standard and Code Review Pin
Kevin McFarlane28-Mar-02 7:59
Kevin McFarlane28-Mar-02 7:59 
GeneralWierd collection problem Pin
27-Mar-02 7:05
suss27-Mar-02 7:05 
GeneralRe: Wierd collection problem Pin
Andy Smith27-Mar-02 7:14
Andy Smith27-Mar-02 7:14 
GeneralComplex configuration files Pin
Michael Groeger27-Mar-02 6:59
Michael Groeger27-Mar-02 6:59 
Questionhow to preserve background after invalidate? Pin
Rüpel27-Mar-02 3:36
Rüpel27-Mar-02 3:36 
GeneralAssociated Files Pin
Nick Parker27-Mar-02 2:38
protectorNick Parker27-Mar-02 2:38 
GeneralPrinting Pin
Mazdak27-Mar-02 1:41
Mazdak27-Mar-02 1:41 
GeneralRe: Printing Pin
Rüpel27-Mar-02 1:52
Rüpel27-Mar-02 1:52 
GeneralRe: Printing Pin
Mazdak27-Mar-02 1:56
Mazdak27-Mar-02 1:56 
Generalstring array Pin
Mazdak26-Mar-02 21:27
Mazdak26-Mar-02 21:27 
GeneralRe: string array Pin
SimonS26-Mar-02 21:48
SimonS26-Mar-02 21:48 
GeneralRe: string array Pin
Mazdak26-Mar-02 22:09
Mazdak26-Mar-02 22:09 
Generalbackup Pin
BLaZiNiX26-Mar-02 19:01
BLaZiNiX26-Mar-02 19:01 
GeneralRe: backup Pin
James T. Johnson26-Mar-02 23:10
James T. Johnson26-Mar-02 23:10 
GeneralRe: backup Pin
BLaZiNiX27-Mar-02 15:35
BLaZiNiX27-Mar-02 15:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.