473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB6 v. C# (something is missing!)

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
Nov 16 '05 #1
13 2193
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
Nov 16 '05 #2
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
Nov 16 '05 #3
"Ole Olsen" wrote...
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?????


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

Nov 16 '05 #4
class kuku
{
private kuku(whatever) {do something}
static get_a_fresh_kuku(params) { return new kuku(whatever); }
}

does that answer your question, or did I misunderstand your post?

Ole Olsen wrote:
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

Nov 16 '05 #5
"Ole Olsen" wrote ...
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?


No.

If you don't want those extra method/properties in the class to be visible,
just declare them private.
// Bjorn A

Nov 16 '05 #6
No, you are right!
I am beginning to see a pattern form here - we are to use "Static
Constructors" to accomplish this type of behaviour?

Well - thanks for the response, all

/Ole
"Uri Dor" <re***************@mivzak.com> wrote in message
news:eM**************@TK2MSFTNGP12.phx.gbl...
class kuku
{
private kuku(whatever) {do something}
static get_a_fresh_kuku(params) { return new kuku(whatever); }
}

does that answer your question, or did I misunderstand your post?

Ole Olsen wrote:
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

Nov 16 '05 #7
I would potentially like these precise poroperties/methods to be used in
other context (ie. another interface also implemented by this class. Okay -
we are far out now, but in theory?

Thanks!
/Ole
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:eF*************@tk2msftngp13.phx.gbl...
"Ole Olsen" wrote ...
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?
No.

If you don't want those extra method/properties in the class to be

visible, just declare them private.
// Bjorn A

Nov 16 '05 #8
Ole Olsen wrote:
I would potentially like these precise poroperties/methods to be used
in other context (ie. another interface also implemented by this
class. Okay - we are far out now, but in theory?


You need to look at this from a different angle. What you want is a class
factory that will return an object implementing the chosen interface.
Without using such a pattern, anyone who instantiates your class will have
access to all the public methods of that class. This is not unique to C#.

--
Gravity: it's not just a good idea, it's the law.
Nov 16 '05 #9
I think you are right! I might need to look at things in a different
perspective.

Just too bad that I have to create a Factory (Factory Pattern) for this,
which was just a single property in VB6?

Well - it seems I have finally managed to identify something that was easier
in VB6.

Thanks all!
/Ole
"Frank Oquendo" <no******@here.com> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
Ole Olsen wrote:
I would potentially like these precise poroperties/methods to be used
in other context (ie. another interface also implemented by this
class. Okay - we are far out now, but in theory?


You need to look at this from a different angle. What you want is a class
factory that will return an object implementing the chosen interface.
Without using such a pattern, anyone who instantiates your class will have
access to all the public methods of that class. This is not unique to C#.

--
Gravity: it's not just a good idea, it's the law.

Nov 16 '05 #10

"Ole Olsen" wrote...

I would potentially like these precise properties/methods
to be used in other context (ie. another interface also
implemented by this class. Okay - we are far out now, but
in theory?


If I understand you correct, you're actually trying to "subtype to the
interfaces" in order to "narrow" the possible behaviour of the actual class.

That's not a common OO-approach (it breaks the Liskov substitutable
principle), though I've seen similar things in the past, but I can't
remember a single one that did it with success.

Maybe you just should consider a different design, where you actually create
two different classes, one for each context.

// Bjorn A

Nov 16 '05 #11
Ole,

Yes you create the class so it dot NOT have any public contrustors
and then you use a static method to create the object. Static methods are
methods which can be called directly on the class and does not require an
class object.

You could also use internal contructors (check the internal keyword) which
makes the contructors public INSIDE your project, but privete OUTSIDE
your project.

HTH,

//Andreas

"Ole Olsen" <Ol*@olsen.it> skrev i meddelandet
news:uc****************@TK2MSFTNGP12.phx.gbl...
No, you are right!
I am beginning to see a pattern form here - we are to use "Static
Constructors" to accomplish this type of behaviour?

Well - thanks for the response, all

/Ole
"Uri Dor" <re***************@mivzak.com> wrote in message
news:eM**************@TK2MSFTNGP12.phx.gbl...
class kuku
{
private kuku(whatever) {do something}
static get_a_fresh_kuku(params) { return new kuku(whatever); }
}

does that answer your question, or did I misunderstand your post?

Ole Olsen wrote:
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


Nov 16 '05 #12
Ole Olsen wrote:
Well - it seems I have finally managed to identify something that was
easier in VB6.


If you want a class that is PublicNotCreatable, you simply specify private
or internal for the class constructor. This is no different than it was in
VB.

In fact, neither is the creation of the object. If a client can use a VB
object but not create it, you have to write a public method that will
instantiate the object and pass it back to the client.

--
Gravity: it's not just a good idea, it's the law.
Nov 16 '05 #13
Just one terminology issue - get_a_fresh_kuku(params) isn't a static
constructor, it's a static method that returns an instance. A static
constructor initializes static class members.

Ole Olsen wrote:
No, you are right!
I am beginning to see a pattern form here - we are to use "Static
Constructors" to accomplish this type of behaviour?

Well - thanks for the response, all

/Ole
"Uri Dor" <re***************@mivzak.com> wrote in message
news:eM**************@TK2MSFTNGP12.phx.gbl...
class kuku
{
private kuku(whatever) {do something}
static get_a_fresh_kuku(params) { return new kuku(whatever); }
}

does that answer your question, or did I misunderstand your post?

Ole Olsen wrote:
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


Nov 16 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
6
by: Just Me | last post by:
My Task List contains the following: At least one reference is missing the 'Name' attribute. Any suggestion on how toe find which project is missing it? Solution contains 25 projects. ...
4
by: John Wood | last post by:
I saw that Microsoft have released a list of breaking changes in .Net here: http://msdn.microsoft.com/netframework/programming/breakingchanges/runtime/default.aspx While this is useful, it seems...
2
by: ken | last post by:
We have a user that has reported the following error when starting up our client application that uses a web service on our back end: System.Configuration.ConfigurationException: Missing required...
2
by: jasonchan | last post by:
here is a code from a book. <html> <body> <script type="text/javascript"> /* Paramater-Passing Basics A function's basic syntax:
12
by: The Frog | last post by:
Hi all, Does anyone have a way to print forms at design time directly from the IDE? I am not a great fan of the print screen to paint to print method. I would really like to know if anyone has a...
5
by: tshad | last post by:
I pulled this from my last question to hopefully make it more clear. After fixing my namespace problem I still need to find out why I am getting an error message: PageInit.cs(43,76): error...
10
by: ALKASER266 | last post by:
Hey guyz I have a prac and I am beginner and I did this code> Is my code is complete and if is it not complete how i can complete it? and how i can arrange it more? How I can make my driver to...
7
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.