错误:由于其保护级别而无法访问

编程入门 行业动态 更新时间:2024-10-26 09:25:17
本文介绍了错误:由于其保护级别而无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

HI, 我知道这是一个基本问题,但我需要一些深度细节 为什么我无法在静态方法中为非静态类创建对象? 前例: 在以下代码中,由于其保护级别无法访问错误... 请解释..

命名空间 dotnetfunda_ex1 { public class ClassA { ClassA( int var ) {} public static void tm() {} } class 计划 { public static void Main( string [] args) { ClassA obj = 新 ClassA( 1 ); // obj.tm(); } } }

Manikandan Muthuraj 谢谢&此致, Manikandan Muthuraj talkheredotnet.blogspot/

解决方案

无法从给定类的实例访问静态字段/方法/属性。 这意味着,如果要访问它(及其访问权限)修饰符允许你这样做),你必须使用类本身而不是它的一个实例。 因此,这会变成你的

obj.tm();

to

ClassA.tm();

希望这会有所帮助。

您显示的代码将无法编译 - 不是因为保护级别,但因为您必须使用适当的保护声明声明构造函数: public 或 private 例如:

public class ClassA { public ClassA( int var ) {} 公开 静态 void tm() {} } class 计划 { public static void Main( string [] args) { ClassA obj = new ClassA( 1 ); // obj.tm(); } }

如果您取消注释 tm 方法访问权限,则会收到其他错误:

无法使用实例引用访问;使用类型名称来限定它

因为它是一个静态方法 - 你需要通过类名来访问它们:

public static void Main( string [] args) { ClassA obj = new ClassA( 1 ); ClassA.tm(); }

错别字 - 两次 - 我讨厌IE,我讨厌IE ... - OriginalGriff [/ edit ]

此编译时错误意味着您尝试访问的属性不是公共的,访问它的唯一方法是修改其访问修饰符或使用反射。 当它不可见到达时:例如,如果该类在另一个项目中并且可见性是内部或更低(受保护或私有),则您赢了不能用它。在这种情况下你必须将它改为公开:

public class ClassA { public ClassA( int var ) {} public static void tm() {} } class 计划 { public static void Main( string [] args) { ClassA obj = new ClassA( 1 ); // obj.tm(); } }

HI, I know this is a basic question but i need some depth details Why i could not able create objects for the non static class in the static method? Fo ex: In this following code and getting "inaccessible due to its protection level" error ... Please explain..

namespace dotnetfunda_ex1 { public class ClassA { ClassA(int var) { } public static void tm() { } } class Program { public static void Main(string[] args) { ClassA obj = new ClassA(1); //obj.tm(); } } }

Manikandan Muthuraj Thanks & Regards, Manikandan Muthuraj talkheredotnet.blogspot/

解决方案

You cannot access a static field/method/property from an instance of the given class. It means, if you want to access it (and its access modifier allows you to do it), you have to use the class itself rather than an instance of it. Thus, this would turn your

obj.tm();

to

ClassA.tm();

Hope this helps.

The code you show will not compile - not because of a protection level, but becasue you must declare constructors with an appropriate protection declaration: public or private for example:

public class ClassA { public ClassA(int var) { } public static void tm() { } } class Program { public static void Main(string[] args) { ClassA obj = new ClassA(1); //obj.tm(); } }

If you then uncomment your tm method access, you will get a different error:

cannot be accessed with an instance reference; qualify it with a type name instead

because it is a static method - and you need to access those via the Class name instead:

public static void Main(string[] args) { ClassA obj = new ClassA(1); ClassA.tm(); }

[edit]Typos - twice - I hate IE, I hate IE... - OriginalGriff[/edit]

This compile-time error means that the property you are trying to access is not public and the only way to access it is by either modifying its access modifier or using reflection. When it''s not visible enough to reach: If, for example, the class is in another project and the visibility is interal or lower (protected or private), you won''t be able to use it. You''ll have to change it to public in such a case:

public class ClassA { public ClassA(int var) { } public static void tm() { } } class Program { public static void Main(string[] args) { ClassA obj = new ClassA(1); //obj.tm(); } }

更多推荐

错误:由于其保护级别而无法访问

本文发布于:2023-11-26 19:59:13,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:于其   无法访问   级别   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!