HighTechTalks DotNet Forums  

Some understanding questions...

Dotnet VJSharp microsoft.public.dotnet.vjsharp


Discuss Some understanding questions... in the Dotnet VJSharp forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Christian-Josef Schrattenthaler
 
Posts: n/a

Default Some understanding questions... - 06-16-2005 , 05:01 AM






Hi!

I am still a beginner with Java/VJ#.NET/ASP.NET, and now I have some
understanding questions:

1. private, public and protected:
1.1. If I want to use my variables only inside of my class-file they
must be private?
1.2. Is this correct: If I want to use variables or methods from
outside the class-file thy must be public?
1.3. What means protected?

2. Sometimes I heard "If the page request is not a postback". What does
this mean?

3. What means static?
3.1. I know that I could use staic variables only from staitc mehtods.
But for what is "static"?

4. Exception handling with try/catch:
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)
{...}"?

Thanks for any help!!!

Greetings,
christian.


Reply With Quote
  #2  
Old   
Christian-Josef Schrattenthaler
 
Posts: n/a

Default Re: Some understanding questions... - 06-16-2005 , 05:17 AM






Hi!

I forgot:

5. "this.":
5.1. Why should I use "this.". In my Java-Learning-Book, and in manny
samples "this." is used, but I tried my code with and without it, and I
got every thime the same result -> NO ERROR...

Kind Greetings,
Christian.


Reply With Quote
  #3  
Old   
Sushovan
 
Posts: n/a

Default Re: Some understanding questions... - 06-17-2005 , 01:16 PM



Dear Christian-Josef Schrattenthaler,

Quote:
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.

Quote:
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.
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.


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

Quote:
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.

Quote:
3.1. I know that I could use staic variables only from staitc mehtods.
No! It's the other way round! Static methods can only access static
variables, but static variables can be accessed from non-static contexts as
well.


Quote:
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();

Quote:
5. "this.":
5.1. Why should I use "this.". In my Java-Learning-Book, and in manny
samples "this." is used, but I tried my code with and without it, and I
got every thime the same result -> NO ERROR...
"this" is especially useful when you are dealing with more than one classes.
Suppose you have two classes MyTopClass and MyBtmClass. MyTopClass creates an
object of type MyBtmClass and calls a method of that class. Suppose that
method needs to call a method of MyTopClass. This can be achieved by passing
the function the object "this", and then that function can use that object
reference to call functions in MyTopClass

This has been a long post... Hope it helps
--
Sushovan, a crazy junior


"Christian-Josef Schrattenthaler" wrote:

Quote:
Hi!

I forgot:

5. "this.":
5.1. Why should I use "this.". In my Java-Learning-Book, and in manny
samples "this." is used, but I tried my code with and without it, and I
got every thime the same result -> NO ERROR...

Kind Greetings,
Christian.



Reply With Quote
  #4  
Old   
George Birbilis [MVP J#] [9880]
 
Posts: n/a

Default Re: Some understanding questions... - 06-18-2005 , 09:19 AM



Quote:
5. "this.":
5.1. Why should I use "this.". In my Java-Learning-Book, and in manny
samples "this." is used, but I tried my code with and without it, and I
got every thime the same result -> NO ERROR...
suppose you have the following code:

public class Test {

private int person;

public void tst(int person){
this.person=person;
}

}

you need "this" only in such scenarios, to differentiate class fields from
local variables (or function parameters) that have the same name (the local
scope has higher priority in name resolution, so a plain "person" would mean
the function parameter "person", not the class field "person"

of course, it's much better to use this pattern:

public class Test {

private int person;

public void tst(int thePerson){
person=thePerson;
}

}

in Borland-style you'd see "aPerson", but I prefer to write "thePerson"

cheers,
George

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis (AT) kagi (DOT) com>
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
.................................................. .......................




Reply With Quote
  #5  
Old   
George Birbilis [MVP J#] [9880]
 
Posts: n/a

Default Re: Some understanding questions... - 06-18-2005 , 09:32 AM



[newsgroup microsoft.public.dotnet.vjsharp]
Quote:
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)

Quote:
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

Quote:
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])

Quote:
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

Quote:
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)

Quote:
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 (AT) kagi (DOT) com>
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
.................................................. .......................




Reply With Quote
  #6  
Old   
George Birbilis [MVP J#] [9880]
 
Posts: n/a

Default Re: Some understanding questions... - 06-20-2005 , 03:18 PM



I've asked the J# team on this and will let you know about it

if anyone has time to checkout "Thinking in Java" book from
www.eckelobjects.com or the Java specification from java.sun.com on this,
please post your findings as a reply

Quote:
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



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.