For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2006 > syntax question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author syntax question
Iconoclast

2005-12-18, 3:56 am

I'm fairly new to C# and am seeing something repeatedly that I can't
find an explanation for in the book I have. What exactly are the
phrases that appear in square brackets such as :
[STAThread]

I know what this one means because I've found people discussing it
here and there, but what is it? I've seen it referred to as a keyword
but it's not in the list of keywords on MSDN

For that matter, I don't even find reference to that in a search of
MSDN

Here's a code snip from the db4o tutorial

[accessDb4o]
ObjectContainer db = Db4o.OpenFile(Util.YapFileName);
try {

{
// do something with db4o
}
} finally {

{
db.Close();
}
}

What does that [] line do?

Thanks
Eric
Iconoclast

2005-12-18, 9:57 pm

On Sun, 18 Dec 2005 09:33:57 GMT, everymn@yahoo.com (Iconoclast)
wrote:

Okay my bad. I figured out that the bracketed statement in the
tutorial was just their way of expressing labels.

I'd still love it if someone could explain to me what type of
expression [STAThread] is.

Are there any more expressions like this that I'd need to be aware
off?

Thanks
Bruce Wood

2005-12-19, 3:58 am

Sorry I didn't reply earlier. I typed up a nice answer for you only to
be hit by the dreaded "Server Error"... translation: "We lost all of
that stuff you spent fifteen minutes typing, but that's OK: you can
type it all over again in a few minutes once our servers come back
on-line." The one time I don't do a control-A control-C and that
happens. Oh well.

Anyway, the bracketed expressions are meta-data about your program.
Think of them as the .NET equivalent of "ReadOnly" for files (or any of
that other stuff you find in the "Properties" dialog of a Windows
file), or metadata in a database: they're not part of your program, but
rather they _describe_ parts of your programs.

They're called "attributes" in .NET.

You can attach an attribute to a class, method, property, field, etc.
You can create your own attributes, and add restrictions as to where
they can be attached, and what kinds of arguments they can take. The
clearest example (for my money) of what attributes can be used for are
the attribuets that you can attach to properties in UserControls or
your own derived controls for use in the Visual Studio Designer. If you
create a UserControl, you can (and really should) "decorate" your
public properties with attributes such as these:

[Category("Appearance"), Description("Sets the background pattern for
the panel."), DefaultValue(null)]
public Pattern BackgroundPattern
{
get { ... }
set { ... }
}

What the stuff in brackets tells the Designer is to:

1. Show this property in the "Appearance" section of the Designer's
property grid (which shows up in Visual Studio when you're doing
Windows Forms development and you right-click on some control and
choose "Properties").

2. When the user clicks on the BackgroundPattern property, show the
text given in the Description attribute in the help window below the
property grid.

3. The default value for this property is null, so if the user sets the
property to null, don't bother serializing (generating code for) it in
the "Windows Forms Designer" section of the source file.

Of course, attributes can be used for lots of other things. You can
even write your own and then write programs to look for attributes and
take particular action when they're found. An example of that is NUnit
(soon to be bundled with VS2005 as the unit test facility) that looks
for [Test] tags on classes and methods to tell it which methods are
unit tests.

The [STAThread] attribute (which takes no arguments, notice) indicates
that your program is a Single Threaded Application (thus STA) as
opposed to a Multi-Threaded Application ([MTAThread], I believe). This
tells the .NET runtime how to set up the environment for your program.

If you look into .NET security, you'll also see that you can "decorate"
(MS's word, not mine) your program with attributes that tell the .NET
runtime what kinds of operations your program will want to do and
therefore what level of security it's going to demand in order to run.
That way if the program is loaded from a location that doesn't have
sufficient privileges, the program will bomb up front with a security
exception rather than when it eventually tries to do something beyond
what it's permitted to do.

I could go on and on about attributes, but that's the nutshell version.

Iconoclast

2006-01-10, 4:09 am

Bruce,
Great notes, thanks very much for typing them up, twice!

At least I now know what to search for to get more info.

Eric
Ciaran

2006-01-10, 4:09 am

STAThread actually means "Single Threaded Apartment" (and MTAThread is a
Multithreaded appartment).
The apartment threading model is a COM threading model that every COM aware
application must use.
Basically, the STAThread attributes tells .NET to create this application
in a single threaded appartment which only allows the execution on one
thread on the object at a time. The MTAThread attribute tells .NET to use
the MTA model which allows multiple threads to access the same objects at
the same time but this requires the coder to take note of all the issues of
concurrency and code against them

Hope this clears it up a little.


--


Ciaran O'Donnell
There are 10 types of people in this world. Those that understand binary and
those that don't


"Bruce Wood" <brucewood@canada.com> wrote in message
news:1134972817.162647.306980@g14g2000cwa.googlegroups.com...
> Sorry I didn't reply earlier. I typed up a nice answer for you only to
> be hit by the dreaded "Server Error"... translation: "We lost all of
> that stuff you spent fifteen minutes typing, but that's OK: you can
> type it all over again in a few minutes once our servers come back
> on-line." The one time I don't do a control-A control-C and that
> happens. Oh well.
>
> Anyway, the bracketed expressions are meta-data about your program.
> Think of them as the .NET equivalent of "ReadOnly" for files (or any of
> that other stuff you find in the "Properties" dialog of a Windows
> file), or metadata in a database: they're not part of your program, but
> rather they _describe_ parts of your programs.
>
> They're called "attributes" in .NET.
>
> You can attach an attribute to a class, method, property, field, etc.
> You can create your own attributes, and add restrictions as to where
> they can be attached, and what kinds of arguments they can take. The
> clearest example (for my money) of what attributes can be used for are
> the attribuets that you can attach to properties in UserControls or
> your own derived controls for use in the Visual Studio Designer. If you
> create a UserControl, you can (and really should) "decorate" your
> public properties with attributes such as these:
>
> [Category("Appearance"), Description("Sets the background pattern for
> the panel."), DefaultValue(null)]
> public Pattern BackgroundPattern
> {
> get { ... }
> set { ... }
> }
>
> What the stuff in brackets tells the Designer is to:
>
> 1. Show this property in the "Appearance" section of the Designer's
> property grid (which shows up in Visual Studio when you're doing
> Windows Forms development and you right-click on some control and
> choose "Properties").
>
> 2. When the user clicks on the BackgroundPattern property, show the
> text given in the Description attribute in the help window below the
> property grid.
>
> 3. The default value for this property is null, so if the user sets the
> property to null, don't bother serializing (generating code for) it in
> the "Windows Forms Designer" section of the source file.
>
> Of course, attributes can be used for lots of other things. You can
> even write your own and then write programs to look for attributes and
> take particular action when they're found. An example of that is NUnit
> (soon to be bundled with VS2005 as the unit test facility) that looks
> for [Test] tags on classes and methods to tell it which methods are
> unit tests.
>
> The [STAThread] attribute (which takes no arguments, notice) indicates
> that your program is a Single Threaded Application (thus STA) as
> opposed to a Multi-Threaded Application ([MTAThread], I believe). This
> tells the .NET runtime how to set up the environment for your program.
>
> If you look into .NET security, you'll also see that you can "decorate"
> (MS's word, not mine) your program with attributes that tell the .NET
> runtime what kinds of operations your program will want to do and
> therefore what level of security it's going to demand in order to run.
> That way if the program is loaded from a location that doesn't have
> sufficient privileges, the program will bomb up front with a security
> exception rather than when it eventually tries to do something beyond
> what it's permitted to do.
>
> I could go on and on about attributes, but that's the nutshell version.
>



Bruce Wood

2006-01-10, 4:09 am

Thanks for that clarification. I had had a vague memory that the thing
was called "Single-Threaded Apartment," but that seemed silly. Now I
remember why. Thanks.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com