Re: Some understanding questions...



[newsgroup microsoft.public.dotnet.vjsharp]
> >1.1. If I want to use my variables only inside of my class-file they
> must be private?
>
> They should be. That is not compulsory, but it makes sense to declare them
> private.

if you don't add an access specifier they're package-private, that is any
class that has the same package name can access them (e.g. the
gr.cti.eslate.X class would be able to access such fields of gr.cti.eslate.Y
class)

> >1.2. Is this correct: If I want to use variables or methods from
> outside the class-file thy must be public?
>
> If the other class file is in the same namespace, then you can access only
> "public" or "protected" methods and variables.

not sure if in J# you can access "protected" fields of a class from another
class that has same namespace or not. In Java you should be able to do so
only from class descendents, so this is problematic security-wise if this is
true (esp. when porting existing Java code to J#). Unless you can somehow
seal a namespace (as you can do with packages in Java), maybe by using a
signed assembly so that no other third-party code uses it apart from the
code you want. Since there's the notion of "package private" too as I
describe above, not sure why there should be separate namespace behaviour.
To be sure try it and see what it does

> Also, you can declare a variable private and write public accessor methods
> to it, like
>
> private int myPrivateNumber;
> public int get_myPrivateNumber(){
> return myPrivateNumber;
> }
> public void set_PrivateNumber(int value){
> myPrivateNumber = value;
> }
>
> Using this method, you can verify whether the value assigned from outside
is
> valid or not before you actually assign it to a variable.

also see the J# documentation on "properties". There are two get/set
patterns in J#, the JavaBeans-style (getXX/setXX) and the .NET style
(get_XX/set_XX), which differ a bit (the 2nd one I think is understood by J#
as accessors for .NET class properties and it exposes such automatically
[e.g. you could write myObject.xx=5 in C# or VB.net then instead of writing
myObject.setXX(5) if you wanted])

> >1.3. What means protected?
>
> protected variables or methods can be accessed only from the same class or
> the same namespace (package in Java).

see above my comment on "package private" and "protected". They should be
different things

in languages like Object Pascal, indeed all protected class members are
accessible by other classes inside the same unit, but that shouldn't be true
in Java

> >3. What means static?
>
> This is large topic, but briefly, static means that the particular value
> does not depend on what particular instance of the class you are
> investigating. If you have one class, MyClass and 3 objects of type
MyClass
> named objOne, objTwo and objThree, then a static variable myStatVar will
have
> the same value for all of objOne, objTwo and objThree, and can be accessed
by
> calling MyClass.myStatVar.

there's also the notion of "static" function parameters, but they're rarely
used. See "Thinking in Java" free book by Bruce Eckel (www.eckelobjects.com)

> >4.1. If I want to catch all Exceptions, I do this with "catch(Exception
> ex) {...}"?
> 4.2. Else I could specify the exact Exception "catch(OleDbException)
> {...}"
>
> Yes, absolutely true. Also, you can get the error message by calling
> ex.getMessage();

Unfortunately you can't catch .NET exceptions with "catch(Exception ex)" I
think. This is bad since that pattern is used a lot by Java programmers. Try
"catch(Throwable t)" instead, but that way you'll catch "Error" descendents
too (Exception and Error are descendents of Throwable class, and J# compiler
is understand "catch(Throwable t)" as catch-all [.NET exceptions included]
if I remember well, but not "catch(Exception e)" too)


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@xxxxxxxx>
Microsoft Most Valuable Professional
MVP J# for 2004, 2005
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime (Delphi & ActiveX: VB, PowerPoint, .NET)
+ Plugs (InterProcess/Internet communication)
http://www.kagi.com/birbilis
+ Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................


.



Relevant Pages

  • Re: Information hiding in CLOS?
    ... protected and private members. ... If you declare some x in a class C, then that x is a member of the C ... can exist independently of namespace containment. ... current package context. ...
    (comp.lang.lisp)
  • Re: Package == NameSpace?
    ... In Java the directory structure should match the package structure. ... Java has default/package accessibility. ...
    (comp.lang.java.programmer)
  • Re: Modules -> do I have is straight?
    ... ruby book and this forum modules are like superclasses. ... In java you create a package (i know you all know this already I am ... The namespace, unlike in Java, is in no way enforced and it ...
    (comp.lang.ruby)
  • Re: Question regarding Javas OOP implementation.
    ... 'private' means that you cannot access the field from another class. ... same package / subpackage (which is not a concept that java supports (*)) / different package ... friend class (unknown by java as well) / non-friend class ...
    (comp.lang.java.help)
  • Re: static member function, with no public/private/etc.
    ... most limited, for all languages? ... C# - private = most restricted ... Java - package = something in the middle ...
    (microsoft.public.dotnet.languages.csharp)