I want to define a unified namespace for all my classes and put them all in it.
I did it with
Namespace
...
End Namespace
But now I can not import it in other projects, also I can not import it in another file in my main project (That file is not in my namespace it is just UI).
Thank you in andvance !
Your project has a root namespace defined in the project properties. Let's say this root namespace is MyApp.MyProject.
Then inside this project, you have a class defined like this:
Namespace MyNamespace
Public Class MyClass...
End Namespace
If you want to use MyClass inside another class, you would just add the following Imports statement at the top of the file:
Imports MyApp.MyProject.MyNamespace
You could also define a Namespace globally outside of the root namespace of your project with Global like so:
Namespace Global.MyNamespace
...
End Namespace
More information can be found in the .NET documentation
Related
I am confused by the statement
"EPPlus is a .NET library that reads and writes Excel files using the Office Open XML format (xlsx). EPPlus has no dependencies other than .NET. "
then the following statement:
"The first thing you do is to create an instance to the ExcelPackage class. To do that you first need to add a using directive to OfficeOpenXml namespace in the top of your file. This is the top namespace in EPPlus;
using OfficeOpenXml;"
so, should I always import OfficeOpenXml? The datatable to spreadsheet code works fine without it...
Thanks
Those two statements aren't contradictory. EPPlus doesn't require anything other than .NET to run. And the "OfficeOpenXml" namespace is part of EPPlus.
"ExcelPackage" is a class in the "OfficeOpenXml" namespace, so you could use a "using OfficeOpenXml" or you could fully qualify it as "OfficeOpenXml.ExcelPackage".
I am using Yii2 and want to create a attribute and access this attribute in every where in my project for exp:
Yii::$app->name
Above is an example of yii2 default name.
Is there any way to declare my custom attribute as following :
Yii::$app->supportname
You can extend Application class, add required properties, and use it on bootstrap.
Create class for web:
class MyApplication extends \yii\web\Application {
public $supportname;
}
And use it in index.php:
(new MyApplication($config))->run();
You need to do the same for \yii\console\Application and yii script.
But probably the best way is to use Application::$params and access value by Yii::$app->params['supportname'].
Is there anyway to change the namespace of the Module and use it as a namespace of my models?
I'm building a project using the basic app template of yii2.
What I want is to have a centralized namespace so that when I use the module into different project and reuse the models inside it, (either basic or advanced template), I won't have to change the namespace based on its root directory.
This will possible with Yii2 aliases.
first put all your models in your custom folder and set it alias name Yii config file. like below
// an alias of a file path
Yii::setAlias('#myFolder', '/path/to/foo');
finally, update your all models namespace based on this alias one time. like below.
namespace myFolder\models\xxx;
now you can copy this folder to any yii2 app and just set alias name, like above i mention.
Note:- directory will be independent like below and namespace will based on this directory.
folderName
->models
->xxxxx
->xxxxx
.....
// and namespace should add all the models like below
namespace folderName\models;
// while using these models, you can import like below
import myFolder\xxxxxx;
You can edit the composer.json file find
{
.....
"autoload": {
"psr-4": {
"App\\": "src/" #<-key=namespace value=dirname
}
}
......
}
run composer update
I'm just starting with my first ASP.Net WebApi 2 project. I have a file like this:
/AppCode/Remits.vb
Public Class Remits
Public Shared Function GetBoolean(id As Integer, id2 As Integer) As Boolean
Return True
End Function
End Class
This function will need to be called from all my controllers. The function checks that the two ID parameters are valid (done using some extensive database logic which is removed for brevity).
/Controllers/TestController.vb
Namespace Controllers
Public Class TestController
Inherits ApiController
<Route("Test/{id}")>
Public Function [Get](id As Int32) As IHttpActionResult
Dim b As Boolean = myNs.Remits.GetBoolean(123, 456) ' <-- error (see below)
Return Ok(b)
End Function
End Class
End Namespace
This won't even compile, with the error
'myNs' is not declared. It may be inaccessible due to its protection
level
Why is the namespace and/or class not accessible from within a controller? Or, what am I doing wrong?
Update
Anything inside App_Code is not recognised by files in the former directories. I've updated the question to reflect this. Here is a (tiny) sample project highlighting the problem.
http://1drv.ms/1kqjwTX
When you add classes to App_Code it is set to build action "Content". You need to change this to "Compile".
So:
Do not use App_Code, rename that folder
Change any class files to "Compile" build action.
For #2. Select the file in Solution Explorer and in the properties window (if you don't have this open by default right click the file and choose properties) there is an option called Build Action. Change that to Compile.
I have solution with few projects included.
One of project is common library project which contain mostly reusable code and functions which shares all other projects under solution and which is referenced to all included projects.
In that project I also have resources like are images and icons and I can access those resources from other projects like that:
Me.Icon = myCommonDll.My.Resources.backup__add__16x16
Question is:
Can it be done somehow that those common resources in myCommonDll will be actual resources for other projects in solution so when I click to "Image" or "Icon" in properties window in designer I get listed images or icons which are present in myCommonDll.My.Resources and how to do that?
EDIT: This is primarily for VS versions prior to VS2008 which did NOT allow you to change the access level modifier for REsources. Other alternatives were external tools or Reflection, but still did not expose resources to the designer.
DLL resources are available at runtime fairly easily but not in the designer. You need to write a small broker in the DLL to fetch images by name. DLL:
Public Class ResMgr
' depending on what else is in the DLL, can just add to an existing class
Public Function GetImage(imgName As String) As Image
Return My.Resources.ResourceManager.GetObject(imgName)
End Function
'' alternatively declare it SHARED eg
''Public Shared Function/Property GetImage As Image
End Class
App:
MyRM = New ResMgr
thisImg = New Image
thisImg = MyRM.GetImage(userImg)
If you construct it as Shared method, it is just:
thisImg = ResMgr.GetImage(userImg)
If you want, you can expose an Enum in the DLL to act as a resource manifest:
Public Enum ResImg
Image1 ' use the names of the images
Backup52
Flag_FR
Flag_RUS
...
End Enum
The DLL function can either act on ResImg and use a big case statement, or you can use [Enum].GetNames to convert/get an array of image resource names.
Edit your .vbproj file and update the two statements below by adding the common directory path of your shared resource files. Good Luck!
<Compile Include="<common Dir>\My Project\Resources.Designer.vb">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<EmbeddedResource Include="<common Dir>\My Project\Resources.resx">
<Generator>VbMyResourcesResXFileCodeGenerator</Generator>
<CustomToolNamespace>My.Resources</CustomToolNamespace>
<SubType>Designer</SubType>
<LastGenOutput>Resources.Designer.vb</LastGenOutput>
</EmbeddedResource>