none
Why there is a different behavior accessing C# and VB.Net properties or method using VB.Net or C# projects?

    Question

  • Hi,

    Why there is a difference between accessing properties or method using C# project and VB.Net project.

    Intellisesnse is not showing some of the methods in C# project but able to access them when we create a object for the specific class but when we access same method in VB project, all the properties or methods able to access without defining any object.

    What is the difference between these two behaviors when trying to access same class?

    Is this the right behavior or something missed.

    Please suggest?

    //Base Class namespace CsharpBaseProj { public sealed class CSharpBaseClass { public string MethodFromCSharpBaseClass() { return "i am from CSharpBaseClass"; } } public abstract class Script : IScript { public CSharpBaseClass CSharpBaseClass { get; set; } public CSharpBaseClass TestProperty { get; set; } public abstract void Run(bool isSilentMode = false); } public interface IScript { /// <summary> /// Runs this instance. /// </summary> void Run(bool isSilentMode = false); } } //C# Project using CsharpBaseProj; namespace CsharpUserScript { public class Class1 : Script { //Here Not able to access MethodFromCSharpBaseClass()

    // Like string str = CSharpBaseClass.MethodFromCSharpBaseClass();

    CSharpBaseClass obj = new CSharpBaseClass(); private string s { get { return obj.MethodFromCSharpBaseClass(); } set { s = value; } } public override void Run(bool isSilentMode = false) { //Here able to access MethodFromCSharpBaseClass() string str = CSharpBaseClass.MethodFromCSharpBaseClass(); } //why there is a difference between C# and VB.Net Behaviors, calling CSharpBaseClass properties or methods. //Is it the right behavior or missing something here. } } //VBProject Option Strict On #Region "Imports" Imports CsharpBaseProj #End Region Public Class Class1 Inherits Script //Here able to access MethodFromCSharpBaseClass() - Please see below piece code Dim s As String = CSharpBaseClass.MethodFromCSharpBaseClass() Public Sub New() MyBase.New() End Sub Public Overrides Sub Run(Optional isSilentMode As Boolean = False) End Sub End Class


    Thanks in advance,

    Ravi

    Tuesday, November 04, 2014 6:05 AM

Answers

  • Hi Ravi,

    This forum is discuss C# issue.  For VB, please redirect to VB forum.

    Now let us analysis your C# code

    CSharpBaseClass obj = new CSharpBaseClass();  
            //Here Not able to access MethodFromCSharpBaseClass()

    Yes, you can create an instance outside from method, but when you call MethodFromCSharpBaseClass(), you should in a method. Like the following code.

            static void Main(string[] args)
            {
                CSharpBaseClass obj = new CSharpBaseClass();
                obj.MethodFromCSharpBaseClass();
            }
     public override void Run(bool isSilentMode = false)
            {
                //Here able to access MethodFromCSharpBaseClass()
                string str = CSharpBaseClass.MethodFromCSharpBaseClass();       
            }
    

    But here CSharpBaseClass is not CSharpBaseClass class, since you define the code in Script Class

    public CSharpBaseClass CSharpBaseClass { get; set; }

    Try to change CSharpBaseClass to CSharpBaseClassProperty, then build again,you will see the error in CSharpBaseClass.MethodFromCSharpBaseClass();      

    Have a nice day!

    Kristin



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 05, 2014 9:01 AM
    Moderator

All replies

  • Hi Ravi,

    Because your have issues on C# code, I moved this thread to Visual C#  forum. C# experts will provide you better support.

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 05, 2014 7:15 AM
  • Is the VBnet returning correct values?  The VBnet code may not be generating an error but that doesn't mean you are getting valid results.

    Your VBnet code and C# code aren't equivalent which explains why you are getting errors with one and no error with the other.  But it doesn't mean either is right or wrong.  Before you come to the conclusion that either is correct make sure you are getting valid results.


    jdweng

    Wednesday, November 05, 2014 7:57 AM
  • Hi Ravi,

    This forum is discuss C# issue.  For VB, please redirect to VB forum.

    Now let us analysis your C# code

    CSharpBaseClass obj = new CSharpBaseClass();  
            //Here Not able to access MethodFromCSharpBaseClass()

    Yes, you can create an instance outside from method, but when you call MethodFromCSharpBaseClass(), you should in a method. Like the following code.

            static void Main(string[] args)
            {
                CSharpBaseClass obj = new CSharpBaseClass();
                obj.MethodFromCSharpBaseClass();
            }
     public override void Run(bool isSilentMode = false)
            {
                //Here able to access MethodFromCSharpBaseClass()
                string str = CSharpBaseClass.MethodFromCSharpBaseClass();       
            }
    

    But here CSharpBaseClass is not CSharpBaseClass class, since you define the code in Script Class

    public CSharpBaseClass CSharpBaseClass { get; set; }

    Try to change CSharpBaseClass to CSharpBaseClassProperty, then build again,you will see the error in CSharpBaseClass.MethodFromCSharpBaseClass();      

    Have a nice day!

    Kristin



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 05, 2014 9:01 AM
    Moderator
  • Thanks for the reply.

    Here my point is not about displaying exception.

    VB.Net will support creating object and accessing its methods/properties outside method, why not possible in C#.net.

    Is there any specific reason, why the methods of a class should be accessed from method not from the outside of a method.

    Public Class Class1 Inherits Script //Here able to access MethodFromCSharpBaseClass() Dim s As String =CSharpBaseClass.MethodFromCSharpBaseClass()

    End Class

    public class Class1 : Script { //Here Not able to access MethodFromCSharpBaseClass()

    // Like string str = CSharpBaseClass.MethodFromCSharpBaseClass();

    }


    I am bit confused with the behavior.

    Please clarify

    Thanks,

    Ravi 


    • Edited by chrasa Tuesday, November 18, 2014 11:42 AM code adjusted
    Tuesday, November 18, 2014 11:41 AM