Instantiating a class with no constructor - c#

I am trying to use a class that unfortunately does not have a constructor defined. When I try to create the object C# tells me that there is no predefined constructor. I was told that this class was created in Visual Basic 6 many years ago, and that somehow in VB the constructor was not needed.
Can anyone think of any solutions for this problem. Maybe a I could declare a new class and inherit the old one. Then in the new class I could create the constructor.
someClass myClass= new someClass();
//Returns an error saying no constructor can be found at all.

It might have an internal constructor. See this answer. This would mean you won't be able to access the constructor from a different assembly. (See MSDN.)
There might be some method in that assembly that instantiates an instance of that class. Try finding a method like that, and call it to get an instance of someClass.

Related

What is the purpose of empty constructor of DbContext Class in EF Code First Approach?

I have the simple context class definition:
namespace PowerSupply.Persistance.Facade
{
class PowerSupplyDBContext : DbContext
{
public PowerSupplyDBContext() : base("PowerSupplyDatabase")
{
}
}
}
There is nothing inside the body of
public PowerSupplyDBContext() : base("PowerSupplyDatabase")
{
what is the purpose of this empty constructor. How does this empty constructor works?
It calls the constructor of the base class. So it is not empty. It just doesnt add something new.
There are many reasons you could use that "empty" constructor.
You may not have any new properties, or you instantiate those properties somewhere else (i dont recommend)
You may just want to add functionality (new methods) so you extend the class, and implement new methods using the properties and methods of base class. So you dont need the constructor to do something new, opposed to the base class.
Or it has many constructors, and you use the empty constructor in circumstances you need to use limited functionality of the class, or default values.
It's best practice to include an empty constructor in any class that might be used by a third party / other project / anywhere that the compiler can't catch a change in the class.
If you omit the empty constructor, and that class is used by a third party or in another one of your projects/applications, then you can't later change that class to include a constructor with arguments. Otherwise, all the uses of this class that previously did not rely on passing constructor arguments will break, because they will have no empty-constructor to reference.

Please explain what does the following code mean in c#? [duplicate]

This question already has answers here:
Bodyless constructor in non-abstract C# class
(2 answers)
Closed 6 years ago.
I saw a code similar to what mentioned below :
public class SomeClass : AnotherClass
{
public SomeClass();
.
.
.
.
}
So what is the meaning of the following line of code in this, what does it do ?
public SomeClass();
Code I am referring to.
that is the constructor of the class SomeClass, and you can use it (call it) everytime you need to create a new object (AKA instance) of the class SomeClass
take a look at how to use a constructor (Thanks to CrowCoder for the comment) and if required take a look at the constructors doc
Instance constructors are used to create and initialize any instance
member variables when you use the new expression to create an object
of a class. To initialize a static class, or static variables in a
non-static class, you must define a static constructor. For more
information, see Static Constructors (C# Programming Guide).
What you are referencing is called a Constructor. Constructors have the same name as the class, and they are used to initialize data members of the new object.
Please reference.
Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.
If you saw code similar to what you mentioned, then honestly that would cause a compile time error, since the syntax for that default constructor (taking 0 parameters) is incorrect.
Needs to be:
public SomeClass()
{
//initialize members;
}
If you retrieved the code that you just posted a picture of, by hitting Go To Definition of the class, then it will only show declarations, and not implementation.

Can we inherit code from one namespace to another

Lets say I have one namespace named first_namespace in which I have a class named first_class in that class I have a function named first_function. I have one more second_namespace in which I have a class second_class in the main method of second_class I have created an object
second_class sc = new second_class();
//now I wont be able to call firt_function by using the object sc
but if I create object of first class that is in first namespace like this:
first_class fc = new first_class();
fc.first_function();//it works
so my question is: can we call it inheritance ? if not then what I call it ?
yes you can inheritance and namespaces are completely separate concepts.
Inheritance lets you derive a child class from any none sealed object.
A namespace is simply a conceptual container for logically locating and grouping code.
Hope this helps
Matthew
Any method in C# belongs to a class (or struct). You can only call the method on an object of the correct class type (also called an instance of the type).
This is not inheritance. It is one part of object-oriented programming that a method belongs to objects of a particular type (and so do not exist on objects of other types).

Tell if a child class ctor called the base class one

How do you programmatically find out if the ctor of a class invoked its base class ctor and with what arguments?
A derived class constructor always calls a base class constructor. This is required by the C# language specification.
To find out what arguments are passed, you might be able to ask the base class, if it saved them, but there is no universal way to do it.
Finding out which base class constructor got called is impossible for all code I've ever seen. The base class would have to save that information explicitly, but typically different constructors just use default values for some internal data.

How to Instantiate a Custom EventArgs Class that has no Accessible Constructor?

I have a problem; I'm using an external library where one particular event has its own custom eventargs; with no constructor. If I want to throw my own event using these eventargs, how can I?
I'll give more detail if asked, but I'm not sure what exactly I should be giving. :)
Other answers suggest some ways (or hacks) how you could do that.
However, I'd say that if the author of the library didn't give you any way to create a new instance of their custom EventArgs class, then you shouldn't be doing that. If you want to create your own event, then you should define a new delegate and new EventArgs type (even if you were duplicating the class that is already defined in the library).
There are good reasons for this:
The library may change and the custom EventArgs type they provide may no longer fit your needs.
The library may need to pass some special object or special ID to their EventArgs, so you may not be able to create the instance correctly.
Since you can only trigger an event from a class where it is defined, you're probably defining a new event in the class (using the event keyword), so there is no real reason why you couldn't declare your own delegate. Or could you provide more details on how are you triggering the event?
Create a new type that inherits from that EventArgs class and use that. Most likely the existing EventArgs type is abstract so you can create a new type that will inherit from it and C#'s polymorphic support will allow you to pass your new type to all methods that expect an instance of the base type.
Use Reflector to find out how the external library instantiates it.
Clearly, the designers of the library made the constructor internal to prevent you doing exactly what you're trying to do - for better or for worse. So anything you do will be a hack.
You could use Reflection to create an instance the class - see Activator.CreateInstance.
An even bigger hack might be to re-use an instance of the args object that you received and stored, but that's dangerous - who knows what internal data it might contain.
You can try to obtain an instance of the class using System.Runtime.Serialization.FormatterServices. Example:
public class Foo
{
private Foo()
{
}
}
...
Foo foo = (Foo)FormatterServices.GetSafeUninitializedObject(typeof(Foo));

Categories