Exam 70-316: Default access modifiers

Discussion in 'MCSD' started by =?ISO-8859-1?Q?Jo=E3o_Andr=E9_Correia_Rodrigues?=, Jul 14, 2003.

  1. Hi everybody!

    I'm currently studying for exam 70-316, following the second edition
    of the official training kit.

    Page 28 of said book says "If the Dim statement is used (in Visual
    Basic .NET) or no access modifier is used (in Visual C#), the variable
    is considered private in Visual C# and Visual Basic .NET classes,
    Public in Visual Basic .NET structures, and private in Visual C#
    structures. Methods do not require an access modifier. If no access
    modifier is specified, the method is Private (private) by default in a
    class or structure in C#, and Public (public) in a class or structure
    in Visual Basic .NET." And from this, the following table can be
    drawn:

    | Visual Basic .NET | Visual C#
    ----------+-----------------+-------------------+-----------
    | member variable | private | private
    class +-----------------+-------------------+-----------
    | method | public | private
    ----------+-----------------+-------------------+-----------
    | member variable | public | private
    structure +-----------------+-------------------+-----------
    | method | public | private

    However, page 32 says "If an access modifier is not specified for a
    method, it has a default access level of private in Visual C# classes
    and structures and public in Visual Basic .NET classes and structures.
    If an access modifier is not specified for a member variable, it has a
    default access level of private in a class or public in a structure."
    And from this the following table can be drawn:

    | Visual Basic .NET | Visual C#
    ----------+-----------------+-------------------+-----------
    | member variable | private | private
    class +-----------------+-------------------+-----------
    | method | public | private
    ----------+-----------------+-------------------+-----------
    | member variable | public | public
    structure +-----------------+-------------------+-----------
    | method | public | private

    So there must be something wrong somewhere, because a member variable
    of a structure in Visual C# is public or private, depending on where
    the book is read.

    Can someone clarify this? Thanks in advance!
     
    =?ISO-8859-1?Q?Jo=E3o_Andr=E9_Correia_Rodrigues?=, Jul 14, 2003
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Jo=E3o_Andr=E9_Correia_Rodrigues?=

    Maria Guest

    Looks like a struct field is private by default.

    You can use the System.Reflection.FieldInfo class to check
    this out for yourself.
    Create a console app, replace the default code in
    Class1.cs with the code below and press F5.
    (Code was based on sample code in the MSDN FieldInfo topic)

    using System;
    using System.Reflection;
    class Tester
    {
    public static void Main()
    {
    MyStruct x = new MyStruct();
    x.MyMethod();
    Console.WriteLine("Press <Return> to exit...");
    Console.ReadLine();
    }
    }
    public struct MyStruct
    {
    int myField1;
    public void MyMethod()
    {
    FieldInfo[] myFieldInfo;
    Type myType = typeof(MyStruct);
    // Get the type and fields of MyStruct.
    myFieldInfo = myType.GetFields
    (BindingFlags.NonPublic | BindingFlags.Instance
    | BindingFlags.Public);
    Console.WriteLine("\nThe fields of MyStruct are
    \n");
    // Display the field information of MyStruct.
    for(int i = 0; i < myFieldInfo.Length; i++)
    {
    Console.WriteLine("\nName : {0}",
    myFieldInfo.Name);
    Console.WriteLine("Declaring Type : {0}",
    myFieldInfo.DeclaringType);
    Console.WriteLine("IsPublic : {0}",
    myFieldInfo.IsPublic);
    Console.WriteLine("MemberType : {0}",
    myFieldInfo.MemberType);
    Console.WriteLine("FieldType : {0}",
    myFieldInfo.FieldType);
    Console.WriteLine("IsFamily : {0}",
    myFieldInfo.IsFamily);
    }
    }
    }
     
    Maria, Jul 14, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daren Hawes

    70-316 & 70-316 Any Tips?

    Daren Hawes, Feb 23, 2004, in forum: MCSD
    Replies:
    1
    Views:
    443
  2. kingski

    072-316 versus 070-316

    kingski, Nov 5, 2004, in forum: MCSD
    Replies:
    1
    Views:
    954
    UAError
    Nov 5, 2004
  3. Pravin

    Exam 70–315 & Exam 70–316

    Pravin, Feb 23, 2008, in forum: Microsoft Certification
    Replies:
    1
    Views:
    550
    Michael D. Alligood [CertGuard, Inc.]
    Feb 23, 2008
  4. Microsoft

    072-316 versus 070-316

    Microsoft, Nov 5, 2004, in forum: MCAD
    Replies:
    0
    Views:
    383
    Microsoft
    Nov 5, 2004
  5. bogdip
    Replies:
    2
    Views:
    262
    bogdip
    Jul 5, 2006
Loading...

Share This Page