Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Working around the lack of friend classes

Working around the lack of friend classes

Scheduled Pinned Locked Moved C#
csharphelpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matt cole
    wrote on last edited by
    #1

    Hi, I have a situation where I have a class which I would like to be visible within its assembly, but only to be able to be created by a single other class. Ideally I would make the constructor private and the other class a friend and this would solve the problem, but that's not possible in c#. I was hoping there may be some special property of a nested class to allow this but it doesn't seem so. Can anyone think of another way to do this? Thanks, Matt

    C L 2 Replies Last reply
    0
    • M matt cole

      Hi, I have a situation where I have a class which I would like to be visible within its assembly, but only to be able to be created by a single other class. Ideally I would make the constructor private and the other class a friend and this would solve the problem, but that's not possible in c#. I was hoping there may be some special property of a nested class to allow this but it doesn't seem so. Can anyone think of another way to do this? Thanks, Matt

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      There is no clean way. C# sucks.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      J 1 Reply Last reply
      0
      • C Christian Graus

        There is no clean way. C# sucks.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        Christian Graus wrote:

        There is no clean way. C# sucks.

        ;P Aww man...come on now. I come from a C++ and Java background; to me, C# rocks. :)

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        1 Reply Last reply
        0
        • M matt cole

          Hi, I have a situation where I have a class which I would like to be visible within its assembly, but only to be able to be created by a single other class. Ideally I would make the constructor private and the other class a friend and this would solve the problem, but that's not possible in c#. I was hoping there may be some special property of a nested class to allow this but it doesn't seem so. Can anyone think of another way to do this? Thanks, Matt

          L Offline
          L Offline
          Leslie Sanford
          wrote on last edited by
          #4

          matt cole wrote:

          I have a situation where I have a class which I would like to be visible within its assembly, but only to be able to be created by a single other class.

          Create an interface representing the functionality of the first class. Have the first class implement that functionality as a private class to the second class:

          public interface ISomeInterface
          {
          void DoSomething();
          }

          public class MyClass
          {
          private class MyPrivateClass : ISomeInterface
          {
          public void DoSomething()
          {
          // Do stuff here...
          }
          }

          public ISomeInterface Create();
          {
              return new MyPrivateClass();
          }
          

          }

          Maybe not exactly what you're looking for; you have the interface in your namespace which may not be implemented any where else, but that may not be a problem.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups