Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# Sealed Class

  • 17-06-2010 10:52am
    #1
    Registered Users, Registered Users 2 Posts: 1,832 ✭✭✭


    Hi,

    I'm trying to use the functionality in a sealed class which doesn't have any constructors. So line below doesn't compile;
    objSealedClass Test = new objSealedClass ()
    

    I've tried;
    objSealedClass Test;
    Test.Method()
    

    But I get a compiler Error - Use of unassigned local variable 'Test'.

    What is the correct way of implementing this?


Comments

  • Closed Accounts Posts: 1,759 ✭✭✭Dr.Silly


    have a look at this example, at http://www.c-sharpcorner.com/UploadFile/mahesh/SealedClasses11142005063733AM/SealedClasses.aspx


    // Sealed class
    sealed class SealedClass
    {

    }[FONT=Verdana, Arial, Helvetica, sans-serif] [/FONT][FONT=Verdana, Arial, Helvetica, sans-serif]
    In the following code, I create a sealed class SealedClass and use it from Class1. If you run this code, it will work fine. But if you try to derive a class from sealed class, you will get an error.[/FONT]
    [FONT=Verdana, Arial, Helvetica, sans-serif]
    [/FONT]

    using System;
    class Class1
    {
    static void Main(string[] args)
    {
    SealedClass sealedCls = new SealedClass();
    int total = sealedCls.Add(4, 5);
    Console.WriteLine("Total = " + total.ToString());
    }
    }
    // Sealed class
    sealed class SealedClass
    {
    public int Add(int x, int y)
    {
    return x + y;
    }
    }
    [FONT=Verdana, Arial, Helvetica, sans-serif] [/FONT]


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    I don't get why this code would not compile even without a constructor. The C# compiler includes a default, does nothing constructor if you don't.
    objSealedClass Test = new objSealedClass();
    

    The 2nd example doesn't work because you need to instantiate Test as per example 1.


  • Registered Users, Registered Users 2 Posts: 1,832 ✭✭✭CountingCrows


    fasty wrote: »
    I don't get why this code would not compile even without a constructor. The C# compiler includes a default, does nothing constructor if you don't.
    objSealedClass Test = new objSealedClass();
    

    The 2nd example doesn't work because you need to instantiate Test as per example 1.

    Using the above, the Compile returns error - The type 'objSealedClass' has no constructors defined


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    This works for me. And looking at the class with .Net Reflector shows the generated constructor. Please post the code for objSealedClass.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SealedClassTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                IAmASealedClass seal = new IAmASealedClass();
                Console.WriteLine(seal.Info);
                Console.ReadKey();
            }
        }
    
        sealed class IAmASealedClass
        {
            public String Info { get { return "Brilliant!"; } }
        }
    }
    


  • Registered Users, Registered Users 2 Posts: 1,832 ✭✭✭CountingCrows


    fasty wrote: »
    This works for me. And looking at the class with .Net Reflector shows the generated constructor. Please post the code for objSealedClass.

    Unfortunately I don't have access to the code for 'objSealedClass' as its within a complied DLL. Very annoying, I'm sure I'm missing something obvious.

    Using Reflector I can see 3 .ctor's all marked as internal.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Hmmm, if the contructors are internal then maybe there's a create function of some kind that returns and instance of objSealedClass? That's all I can think of!


  • Registered Users, Registered Users 2 Posts: 669 ✭✭✭whatstherush


    If its a public class with no public constructor then there might be a static method on the class that creates the object and returns it otherwise I don't see the point of having a public class with only internal constructors.

    The fact that it is sealed is neither here nor there with respect to your problem, it only means that you can't inherit from it.


  • Registered Users, Registered Users 2 Posts: 1,832 ✭✭✭CountingCrows


    Contacted DLL developer and turns out there is indeed a bug in this class! Sorry for the wild goose chase guys, but thanks for some useful insights.


Advertisement