dogged
dogged

Reputation: 33

Is it possible to use hard-coded values in a base constructor?

I need to inherit from a base class, one method of which has a constructor with 8 arguments.

I won't ever need all 8 arguments, I only need one. However, I do need to implement that method.

public MyClass : BaseClass
{    
    public class MyClass(string myArg):base(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
    {
         // do stuff
    }
}

results in an epic fail compiler message where args2-8 are hard-coded. Any hints?

Clarification: Arguments are all properties of the base class which are set via with this initialization.

As the base class is a framework class, I'm not in a position to amend its properties or constructor.

Hopefully the final edit for those who want to know what the arguments are (and no, the opening brace is not an issue, thanks anyway) here's the VB.NET code I'm trying to port as part of a Custom Membership Provider.

Sadly, finding an en example of C# membership provider was not possible.

Public Sub New(ByVal userName As String)
            MyBase.New("Shiningstar.SSSMembershipProvider", userName, _
            userName.GetHashCode, userName, "", "", False, False, Today, _
            Today, Today, Today, Today)

            Me.m_UserName = userName ' UserName() is read only
End Sub

The base class in this case is System.Web.Security.MembershipUser

Upvotes: 0

Views: 1052

Answers (5)

Kleinux
Kleinux

Reputation: 1541

If you want to use properties of the base or current class as arguments to a constructor, those properties need to be static. I am guessing the error you are getting is something about the "this reference is not initialized" or similar. The same is true of any methods that would return a value to then use as an argument.

Upvotes: 1

JaredPar
JaredPar

Reputation: 754565

Can you give us some more context on what arg2-arg8 are?

There are restrictions on the type of values that can be passed into the base class constructor. For instance you cannot pass instance fields or properties. Constants, constructor arguments and literals should be just fine though.

Upvotes: 1

SLaks
SLaks

Reputation: 887275

Without the exact error message, I cannot be sure, but I suspect that your arg1 .. arg8 are instance fields of the class, which cannot be used before the base constructor.

If you make them static fields, that problem would be solved.

Upvotes: 2

Blair Conrad
Blair Conrad

Reputation: 241734

Wild guess here. Is the problem that you're missing the class keyword in your class declaration?

public class MyClass : BaseClass
{
    public MyClass(string myArg):base(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
    {
         // do stuff
    }
}

And depending on what you do in "do stuff", you might be throwing away your myArg... but that's probably not your problem.

Upvotes: 1

wsd
wsd

Reputation: 1246

I've never heard of method constructors, so I assume you mean a normal constructor. You should be able to hard code some of the parameters in. Here is a small code example that compiles without error:

public class MyBaseClass
    {
        public MyBaseClass ( int arg2, string arg1,string ar3)
        { }
    }

    public class MyClass : MyBaseClass
    {
        public MyClass (int argument) : base (argument, "not supplied", "hard-coded") 
        { }

    }

I hope this answers your question.

Edit (some stumble-stones:)


Are you sure you have the right access modifiers (protected, internal, private, public, protected internal) ?

Upvotes: 3

Related Questions