Question

i am trying to instatiate a public sealed class in my program,

the thing is, ...as i am still fresh C# .net not-yet Developer , i find this issue a little difficult ...

As for the problem in Question, you can skip straight to Program example, or read the folowing background:

DB_Schema is a helper namespace, i've Created, to deal with the data accessing

(it holds tables And SPs names ..etc')

one of its classes(below) deals with Stored Procedures, and this one holds names of SPs Parameters

public sealed class SProc
{
    public sealed class GetCPAReport
    {
        public const string RecordNum = "@RecordNum",
                            CPAColumnName = "@CPAColumn_Name",
                            Value = "@value",
                            IsFreelance = "@isFreelance";
    }

}
  • Usage in program:

within method for data access via SP

private DataTable Get_RefTable_OfUsersBy(string DepartmetID)
{
    SProc.GetCPAReport SProcGetCpa = SProc.GetCPAReport();
    SP_Params.Add(new SqlParameter(SProcGetCpa.IsFreelance, 1));
}

trying to access one of the instance (SProcGetCpa) members is not possible the way i tried .

i could just make class SProc + it's sub class UpdateCPAReport not sealed and ...

but as i was searching the question "can sealed class be instantiated?

well.. the answer is Yes ... though trying to find information on the error:

cannot be accessed with an instance reference; qualify it with a type name instead

yields no results, Nor an Example of Accessing Instantiated sealed class public member code atleast not for fresh .net C#arpers like me

- Update

i wanted to avoid long lines and make custom short names for the strings that represents the stored procedure name

instead of

ParListEmployeeUsrs.SP_Params.Add(new SqlParameter(HTSPs.RobTC_CPA_Users_Names_JobPosition.IsFreelance, SelectedDepartmentID));

update 2

for future comers on this subject who seeks for an answer

as suggested by a dear friend of ours, here in StackOverflow

if you do have to make a short namings for your classes, when using them for current peoject :

just place this among the usings of your project

using system...
using restOf.net
//just add your own as follows !

using shortClassName = myHelperNameSpace.MyIncrediblyUnnecessaryLongHelperClassName;
Était-ce utile?

La solution

GetCPAReport doesn't have any instance members. const members are implicitly static. In C#, you can't access static members through a reference as you're trying to at the moment.

You just want:

SP_Params.Add(new SqlParameter(SProc.GetCPAReport.IsFreelance, 1));

Personally I'd make GetCPAReport a static class, too. There's no point in instantiating it, as it just contains constants... so actively prevent it from being instantiated.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top