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. Advertisements

  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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.