x


C# Going Static or Going OOP

I have some problems with the "static" thinking in C#... It seems it is not legal to access a static method or member variable through an instance.

public FSM fsm;

instance -> fsm.transition();

static -> FSM.SomeMethod();

Question for C# Unity Users - How does one know for example in the Unity Engine which methods or members are static and those that are instantiated?

Thanks CSDG

more ▼

asked May 24 '11 at 02:18 AM

UniteMage gravatar image

UniteMage
134 28 32 36

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Console is actually a public sealed class, but Console.ReadLine() is a static method. C# doesn't strictly support singleton classes in the sense that there's a keyword (like Java's singleton keyword), but you can implement the pattern manually. For those interested, Microsoft explains how in this article.

It's interesting that you should point out the ambiguity between instance access and static access, I'd never realized that, as in your example with A.B(). You see, you're never really in doubt if you use an editor with proper syntax highlighting. Here's an example with a bit of code I zoomed in on, in Visual Studio 2010:

Syntax highlighting

Notice the light blue color? Everything colored light blue is statically accessed. If it's black, it's variable access. I.e. Util.ToolDepthLabelScript.SetTextSize(); is an access to the method SetTextSize() on the variable ToolDepthLabelScript (a script instance), located in the static class Util. The highlighting makes it really simple.

more ▼

answered May 24 '11 at 06:45 AM

CHPedersen gravatar image

CHPedersen
10.8k 14 31 77

I thought that the code coloring for editors are based on color rules and Code Templates. The members of a class can be either instance members or static members. You access a static member through the name of the class in which it is declared just as you would if it were instance member. So I guess it is not possible to distinguish a static member nor an instance member in the Unity Engine because all class names are highlighted.

Thanks CSDG

May 24 '11 at 07:38 PM UniteMage
(comments are locked)
10|3000 characters needed characters left

The docs list a var/function as a class function if it is a static, which seems like pretty standard C# convention.

C# really is confusing that way, it's not a Unity thing. In C++, regular functions are probably in a namespace, and you use Mathf::sqrt(9);. The :: lets you know that Mathf is just the scope.

Whereas C# (not just Unity) encourages you to group regular functions in "fake" classes, full of only statics and no member vars, which will never be instantiated. You can never tell in A.B() if A is a variable or a scope. I always assumed it was a design issue: I'm not sure if Console if a singleton or a class, but I use Console.ReadLine() either way.

more ▼

answered May 24 '11 at 06:03 AM

Owen Reynolds gravatar image

Owen Reynolds
19.4k 2 8 66

I should have given a better clarification in order to determine if there was a way to distinguish the difference between the different types of classes and their members. I am fascinated with C#. I behaves like C++ but has a lot of characteristics in Java. With all things considered, the question was not that important.

May 24 '11 at 07:47 PM UniteMage
(comments are locked)
10|3000 characters needed characters left

I normally rely on naming conventions, i.e. fsm is a field, whereas Fsm is a class. What does cause problems is when you have a public property of, for example, a Rectangle that is called Rectangle. In this case, you would have to use either highlighting or looking at statically available functions. VS does make this really clear, IntelliSense is much better than normal rules.

more ▼

answered May 24 '11 at 08:00 PM

ckfinite gravatar image

ckfinite
1.4k 7 9 24

I am trying to get a handle on MonoDevelop as well as everything. I don't want to seem paranoid but it seems that some are inclined lay down misinformation or inclined to lay down principles as incontrovertibly true. Please.

May 24 '11 at 08:42 PM UniteMage

MonoDevelop is much better for C# dev than Unitron/UniScte. You syntax highlighting will work in a similar way to VS's.

May 24 '11 at 09:26 PM ckfinite
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x7712
x390
x21

asked: May 24 '11 at 02:18 AM

Seen: 2377 times

Last Updated: Jul 20 '11 at 11:50 AM