# How to set the value of a symbol in a package [duplicate]

I am writing a package like this:

BeginPackage["HNotation2"];

Begin["Private"];

HTransR=3;

End[];

EndPackage[];


When I load the package, it seems that the symbol HTransR is not defined. Is there something wrong with the way that I'm assigning a value to the symbol in the package?

• First, it should be Begin["Private"];. Afterwards, you will find HTransR in the context "HNotation2Private". Unless you let appear the symbol HTransR already before Begin["Private"];, you have to refer to it by HNotation2PrivateHTransR from outside that package. – Henrik Schumacher Feb 4 '19 at 17:58
• @HenrikSchumacher - I am confused by the first sentence of your comment. – Jason B. Feb 4 '19 at 18:04
• but it should be Begin["Private"] – Jason B. Feb 4 '19 at 18:05
• @JasonB. Oops. Sorry. I did not know about that possibility (good to know!). I tend to use the full context path (Begin["HNotation2 Private"];), but of course that is a bit redundant and error prone. Once again, sorry guys for the confusion caused by me. – Henrik Schumacher Feb 4 '19 at 18:29

With respect, I believe Begin["Private"] is acceptable, and perhaps preferable. Begin["Private"] establishes a context "Private". This is fine, but if two packages do that they will share that context. Begin["Private"] establishes context HNotation2Private. This will be private to this package only.
The reason HTransR appears to be undefined is that it is defined within context HNotation2Private, which is not on the \$ContextPath -- it can be accessed by its full name as HNotation2PrivateHTranR.
If you wish it to be accessible by its short nme, you could place a usage statement for it before Begin["Private"]`, or just define it there. Then it will be in the package context, which is placed on the context path when the package is loaded.