0

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;
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76

1 Answers1

3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • i guess i get a little over Excited ,over using the `sealed` modifier i got a little bored with static – LoneXcoder Dec 07 '12 at 15:50
  • @LoneXcoder No, you should just treat them as static since they effectively are, instead of making them instanced. Also keep in mind static classes are also effectively sealed. – Servy Dec 07 '12 at 15:51
  • @LoneXcoder: Why would you want to make them *instance* members at all? They're constants. Put it this way: would you ever expect one instance of `GetCPAReport` to have a different value for `IsFreelance` from other instances? – Jon Skeet Dec 07 '12 at 15:52
  • @JonSkeet the names here been translated its all 20 letters or so in length (: – LoneXcoder Dec 07 '12 at 15:54
  • Such as : `HTSps.RobTC_CPA_Users_Names_JobPosition_Field` = * 5 params = five long lines , i hate them (even though i am using 42" Led screen)' – LoneXcoder Dec 07 '12 at 15:56
  • @JonSkeet (iv'e Updated my question) would you think it's something i should not realy think of ? the names of the fields/const/properties are long... reason?: some times when you have many of them (too many), they differ by some extra `different` words to tell you who they are and what they do , so i wanted to try and keep it short when i **use** those in application – LoneXcoder Dec 07 '12 at 16:15
  • 2
    @LoneXcoder: That shouldn't affect your design. If you *really* want to make things shorter, you could use aliasing `using` directives... but you shouldn't make something an instance member just for the sake of it. – Jon Skeet Dec 07 '12 at 16:23
  • @JonSkeet, thanks for your attention mr SKeet, i was woried you might think it's a kind'a Nonsense issue and it's not even something to put in mind but i do care of how the code looks the : googling "aliasing using directives" – LoneXcoder Dec 07 '12 at 16:32
  • @JonSkeet but offcorse (: , i knew about the `using` statement{applied with `namespace` *s* and.. types that are implicitly `idisplosable convertible` Capable ! (don't mistake and think i'm good student... as i knew about `using`, and what it is exactly ... only by trial & error !! (: ) } , as i am trying to learn while working(Low pay but paied job (: ) ..seem that is not the best idea for get ejucated as opposed to - taking **step by step** online tutorial as if i were in a College).. that explains my lack of knowlege of the `alias` part ... – LoneXcoder Dec 07 '12 at 17:15