473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Ensure the propper calling class.

I have several classes that has a public interface (nothing really different
there :) ).

1) I would like to ensure that some classes can only be instantiated by a
specific class type.

2) I would like to restrict some public methods from being called only
from another certain class type.

I was trying to figure out a way that I could do this with attributes but I
am coming up empty.

Any ideas?
Nov 20 '05 #1
3 1058
One way to do this would be to have a custom permission(based on strong name or whatever other criteria) and in your class do a link demand to check that the caller has the required custom permission.

--
Regards,
Anand M
VB.NET MVP

http://www.dotnetindia.com
"Ray Cassick (Home)" wrote:
I have several classes that has a public interface (nothing really different
there :) ).

1) I would like to ensure that some classes can only be instantiated by a
specific class type.

2) I would like to restrict some public methods from being called only
from another certain class type.

I was trying to figure out a way that I could do this with attributes but I
am coming up empty.

Any ideas?

Nov 20 '05 #2
"Ray Cassick (Home)" <rc************@enterprocity.com> schrieb
I have several classes that has a public interface (nothing really
different there :) ).

1) I would like to ensure that some classes can only be
instantiated by a specific class type.
Make the class a nested, private class, so only the outer class can create
instances. Implement a Public interface. Something like:

class outer
public interface i
end interface

private class inner
implements i
end class

public function GetI as I
end function
end class
2) I would like to restrict some public methods from being called
only from another certain class type.


Apart from the same solution as above, you could put the classes in the same
assembly and declare the methods as Friend. But, usually there are other
criteria to choose the classes for the same assembly, so the criteria might
be competing, that's why I wouldn't do it. Instead, my design rule is: As
soon as an object can be accessed, all methods are available. I don't see
why it depends on the object calling the public methods whether
encapsulation of the object is broken or not.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
Ray,
In addition to the other comments. The "easiest" way to ensure that a class
can only be instantiated or used by a specific class is to put both classes
in their own Assembly (Class Library) then make the constructor (Sub New)
for the second class Friend. To restricting the methods called make those
methods Friend also.

' In a class library by themselves:
Public Class Class1

Public Function CreatesClass2() As Class2
Return New Class2
End Function

End Class

Public Class Class2

Friend Sub New()
End Sub

Friend Sub MethodForClass1()
End Sub

Public Sub MethodForEveryOne()
End Sub

End Class

' In a different assembly referencing the above assembly

Dim c1 As New Class1
Dim c2 As Class2 = c1.CreatesClass2()
c2.MethodForClass1() ' error
c2.MethodForEveryOne() ' succeeds

Hope this helps
Jay

"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I have several classes that has a public interface (nothing really different there :) ).

1) I would like to ensure that some classes can only be instantiated by a specific class type.

2) I would like to restrict some public methods from being called only
from another certain class type.

I was trying to figure out a way that I could do this with attributes but I am coming up empty.

Any ideas?

Nov 20 '05 #4

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

Similar topics

7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
7
by: M O J O | last post by:
Hi, I can't figure out how to use the DesigMode property. I've searched Google and not found a soilid solution. Therefor I ask you - what is the propper way to find out if I'm in DesignMode??? ...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
3
by: kk_oop | last post by:
Hi. I have a template class Base_t<typename T1, typename T2>. I intend to instantiate it like this: class Derived : public Base_t<SomeClass1, SomeClass2> { .... }; I'd like to have the...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
7
by: garyusenet | last post by:
Hi I have finally made progresss with my use of the open file dialog box! I have now made my own method that opens the dialog box and returns the file selected. Can you please have a quick...
6
by: Smokey Grindle | last post by:
I always learned when you have a constant name it in all upper case like this PASSWORD_MAXIMUM_LENGTH but FxCop is saying this isn't correct, but doesnt tell me the correct "accepted" way in...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.