newbie - creating a new instance

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

Hi,
I have a c# class that I am trying to create an instance of from
vb.net, and am getting the error "Sub New() is not accessible in this
context because it is 'Private'. Declaration (c#) and instantiation
(vb.net) are below:

<snip of C#>
namespace LogicTec.JLTRE.Data
{
public class SystemUserClass
{
private SystemUserClass() { }

/* some methods and members here... */

}
}
</snip of C#>

<snip of VB>
Dim userClass As New LogicTec.JLTRE.Data.SystemUserClass
</snip of VB>

Help me!!!!!
 
I have a c# class that I am trying to create an instance of from
vb.net, and am getting the error "Sub New() is not accessible in this
context because it is 'Private'. Declaration (c#) and instantiation
(vb.net) are below:

A very good error message that states exactly what the problem is (Sub
New is VBism for a constructor, ie SystemUserClass() in C#). You could
change the constructor to be public, but it looks like it's been made
private deliberately so if someone else has written the code you may
want to think twice before doing so.


Mattias
 
Kieth,

Well, the error is telling you exactly what is wrong. The constructor
in C# is marked as private, so outside of the class, you can't create new
instances of it. You either have to change the accessibility of the
constructor to public, or create a static factory method which will create
and return new instances for you.
 
Keith said:
Hi,
I have a c# class that I am trying to create an instance of from
vb.net, and am getting the error "Sub New() is not accessible in this
context because it is 'Private'. Declaration (c#) and instantiation
(vb.net) are below:

It seems to me that the error is pretty self-explanatory. Your class
has a private constructor, and so the class can't be instantiated except
from within the class itself.

Change "private" to "public" and it should work. Of course, it begs the
question as to why the constructor is private. If there's a good reason
for that, then you need to instantiate the class some other way (i.e. in
whatever way was intended for the class).

Pete
 
Hi,
I have a c# class that I am trying to create an instance of from
vb.net, and am getting the error "Sub New() is not accessible in this
context because it is 'Private'. Declaration (c#) and instantiation
(vb.net) are below:

<snip of C#>
namespace LogicTec.JLTRE.Data
{
public class SystemUserClass
{
private SystemUserClass() { }

/* some methods and members here... */

}}

</snip of C#>

<snip of VB>
Dim userClass As New LogicTec.JLTRE.Data.SystemUserClass
</snip of VB>

Help me!!!!!

Yes - thank you all for pointing out my stupidity and being nice - I
no more than posted the question when I saw it. As I said I am a
newbie...
 
Back
Top