VB6诉C#(缺少一些东西!) [英] VB6 v. C# (something is missing!)

查看:49
本文介绍了VB6诉C#(缺少一些东西!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初来自VB6背景,现在完全用C#编程。

总而言之,这是一个很棒的语言(C#),有很多强大的实体;然而有一件事我似乎无法弄清楚。


在VB6中,创建一个类时,你可以将它标记为PublicNotCreatable,这意味着它无法直接实例化它。通常会这样做,你想强迫人们通过定义的界面访问这个具体的类。


所以你会在客户端应用程序中写这样的东西。


将myobject调暗为IInterface

设置myobject = new myClass''这没关系


如果尝试这种方法:

Dim myobject as myClass

设置myobject = new myClass''这将失败


我认为这是一个非常好的功能,你可以强制人们通过界面访问您的班级。现在 - 我如何在C#.NET中实现这个屏蔽/强制?????


/谢谢!

/ Ole

I originally come from a VB6 background, now entirely programming in C#.
All in all a great language (C#) with a lot of powerful entities; there is however one thing that I just can''t seem to figure out.

In VB6, when creating a Class, you can mark it at "PublicNotCreatable", meaning that it is not possible to instantiate it directly. This would one normally do, it you want to force people to access this concrete class through a defined Interface.

So you would write something like this in the Client app.

Dim myobject as IInterface
Set myobject = new myClass ''this is OK

if trying this approach:
Dim myobject as myClass
Set myobject = new myClass '' this wil FAIL

I think this is a quite nice feature, that you can force people to access your class via an Interface. Now - how do I accomplish this "shielding"/forcing in C#.NET?????

/Thanks!
/Ole

推荐答案

我不太明白这个好处?你能解释一下吗?


David。

" Ole Olsen" < Ol*@olsen.it>在消息新闻中写道:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

我最初来自VB6背景,现在完全用C#编程。

总而言之,它是一个很棒的语言(C#),有很多强大的实体;然而有一件事我似乎无法弄清楚。


在VB6中,创建一个类时,你可以将它标记为PublicNotCreatable,这意味着它无法直接实例化它。通常会这样做,你想强迫人们通过定义的界面访问这个具体的类。


所以你会在客户端应用程序中写这样的东西。


将myobject调暗为IInterface

设置myobject = new myClass''这没关系


如果尝试这种方法:

Dim myobject as myClass

设置myobject = new myClass''这将失败


我认为这是一个非常好的功能,你可以强制人们通过界面访问您的班级。现在 - 我如何在C#.NET中实现这个屏蔽/强制?????


/谢谢!

/ Ole
I don''t quite see the benefits of this?? Can you explain?

David.
"Ole Olsen" <Ol*@olsen.it> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
I originally come from a VB6 background, now entirely programming in C#.
All in all a great language (C#) with a lot of powerful entities; there is however one thing that I just can''t seem to figure out.

In VB6, when creating a Class, you can mark it at "PublicNotCreatable", meaning that it is not possible to instantiate it directly. This would one normally do, it you want to force people to access this concrete class through a defined Interface.

So you would write something like this in the Client app.

Dim myobject as IInterface
Set myobject = new myClass ''this is OK

if trying this approach:
Dim myobject as myClass
Set myobject = new myClass '' this wil FAIL

I think this is a quite nice feature, that you can force people to access your class via an Interface. Now - how do I accomplish this "shielding"/forcing in C#.NET?????

/Thanks!
/Ole


好吧 - 让我们想象具体类包含的内容比接口中定义的更多属性和方法。


允许实例化直接类 - 这些额外的方法/属性是可见的。如果将对象声明为接口 - 只显示已定义的属性/方法(因为已经过了!)。


足够的理由?


谢谢!

/ Ole

" David" < no*@vailable.com>在消息新闻中写道:OI ************** @ TK2MSFTNGP11.phx.gbl ...

我不太明白这个好处?你能解释一下吗?


David。

" Ole Olsen" < Ol*@olsen.it>在消息新闻中写道:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

我最初来自VB6背景,现在完全用C#编程。

总而言之,它是一个很棒的语言(C#),有很多强大的实体;然而有一件事我似乎无法弄清楚。


在VB6中,创建一个类时,你可以将它标记为PublicNotCreatable,这意味着它无法直接实例化它。通常会这样做,你想强迫人们通过定义的界面访问这个具体的类。


所以你会在客户端应用程序中写这样的东西。


将myobject调暗为IInterface

设置myobject = new myClass''这没关系


如果尝试这种方法:

Dim myobject as myClass

设置myobject = new myClass''这将失败


我认为这是一个非常好的功能,你可以强制人们通过界面访问您的班级。现在 - 我如何在C#.NET中实现这个屏蔽/强制?????


/谢谢!

/ Ole
Well - lets imagine that the concrete class contained more properties and methods than was defined in the Interface.

By being allowed to instantiate the Class directly - these extra method/properties are visible. If declaring your object as an Interface - only the defined properties/methods are exposed (as disired!).

Reason enough?

Thanks!
/Ole
"David" <no*@vailable.com> wrote in message news:OI**************@TK2MSFTNGP11.phx.gbl...
I don''t quite see the benefits of this?? Can you explain?

David.
"Ole Olsen" <Ol*@olsen.it> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
I originally come from a VB6 background, now entirely programming in C#.
All in all a great language (C#) with a lot of powerful entities; there is however one thing that I just can''t seem to figure out.

In VB6, when creating a Class, you can mark it at "PublicNotCreatable", meaning that it is not possible to instantiate it directly. This would one normally do, it you want to force people to access this concrete class through a defined Interface.

So you would write something like this in the Client app.

Dim myobject as IInterface
Set myobject = new myClass ''this is OK

if trying this approach:
Dim myobject as myClass
Set myobject = new myClass '' this wil FAIL

I think this is a quite nice feature, that you can force people to access your class via an Interface. Now - how do I accomplish this "shielding"/forcing in C#.NET?????

/Thanks!
/Ole


" Ole Olsen"写了......
"Ole Olsen" wrote...
在VB6中,创建一个类时,你可以在
PublicNotCreatable中标记它,这意味着无法实例化它。它直接。通常会这样做,你想通过定义的界面强迫人们访问这个具体的类。

所以你会在客户端应用程序中写这样的东西。

将myobject变暗为IInterface
设置myobject = new myClass''这没关系

如果尝试这种方法:
将myobject昏暗为myClass 设置myobject = new myClass''这将失败


我必须承认我以前在VB中没有看到过类似的东西,但是又一次

我没有做那么多的VB编程......

我认为这是一个非常好的功能,你可以强迫人们通过界面访问你的课程。 />现在 - 我如何实现这个屏蔽/强制在C#.NET ?????
In VB6, when creating a Class, you can mark it at
"PublicNotCreatable", meaning that it is not possible to
instantiate it directly. This would one normally do, it
you want to force people to access this concrete class
through a defined Interface.

So you would write something like this in the Client app.

Dim myobject as IInterface
Set myobject = new myClass ''this is OK

if trying this approach:
Dim myobject as myClass
Set myobject = new myClass '' this wil FAIL
I must admit I haven''t seen anything like that in VB before, but then again
I didn''t do so much VB programming either...
I think this is a quite nice feature, that you can
force people to access your class via an Interface.
Now - how do I accomplish this "shielding"/forcing in
C#.NET?????




我想我明白你在哪里走了。但是,即使你能够实现这一目标,这样做的实际好处是什么?


由于该类已实现了界面,所有的预期的行为
接口的
即使变量是类型类型的
也可通过实例获得。


如果class已经实现了接口,实例仍然是-b
接口实例。


或许以下方法至少会出现接近 ;我认为你是什么

搜索:


公共接口MyInterface {}


公共类MyClass :MyInterface

{

private MyClass()

{

//任何需要启动......

}


public static MyInterface FactoryMethod()

{

MyInterface mif = new MyClass();

返回mif;

}

}


反过来可用于b ;类似"你在VB中所拥有的方式:


MyInterface x = MyClass.FactoryMethod(); //允许


MyInterface x = new MyClass(); //不可编辑

MyClass x = new MyClass(); //不可编辑

MyClass x = MyClass.FactoryMethod(); //不可编辑


我不确定它涵盖OP所需要的所有基础,因为它在C#中仍然可以

被强制转换到任一方向的变量(界面的变量

类型 - >类类型的变量,和vv。)。


// Bjorn A



I think I understand where you''re going. However, even if you could
accomplish that, what would be the actual benefit of doing that?

As the class has implemented the interface, all of the behaviour expected
from the interface is available through the instance even if the variable is
of the class type.

If the class has implemented the interface, the instance still is-an
"instance of the interface".

Perhaps the following approach at least comes "near" what I think you''re
searching for:

public interface MyInterface { }

public class MyClass : MyInterface
{
private MyClass()
{
// Any needed initiation...
}

public static MyInterface FactoryMethod()
{
MyInterface mif = new MyClass();
return mif;
}
}

Which in turn can be used in a "similar" manner as what you had in VB:

MyInterface x = MyClass.FactoryMethod(); // Allowed

MyInterface x = new MyClass(); // Not compilable
MyClass x = new MyClass(); // Not compilable
MyClass x = MyClass.FactoryMethod(); // Not compilable

I''m not sure it covers all bases of what the OP wants as it in C# still can
be "cast" to a variable in either direction (variable of the interface
type --> variable of the class type, and vv.).

// Bjorn A


这篇关于VB6诉C#(缺少一些东西!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆