Im From VB.net base want to learn ASP.NET MVC
For Example : Function created below (how to create in global folder/cs and how to call it then use in Controller)
Function pRound(Number ,NumDigits)
Dim dblPower, vPSTEmp, intSgn
dblPower = 10 ^ NumDigits
vPSTEmp = CDbl(Number * dblPower + 0.5)
pRound = Int(vPSTEmp) / dblPower
End Function
For vb I just add <--#include file="include/function.asp"-->
then can use it like pRound(number, 4)
Please Teach Me How to Do it. Thx a lot.
You could add a new class file in your solution and make a static class;
namespace ProjectName.Functions
{
public static class Utility
{
public static float pRound(float number, int digits){
float result = 0;
// your code here
return result;
}
}
}
Then in your controller, since a static class is instantiated at the start of the program, you could just call it;
using ProjectName.Functions;
public ActionResult TestController
{
// call Utility.pRound(), no need to instantiate the class
float round = Utility.pRound(1,1);
}
I've edited Jerdine Saibo's answer.
By the time it gets approved, here's an updated code. (pRound methods needs to be static)
namespace ProjectName.Functions
{
public static class Utility
{
public static float pRound(float number, int digits){
float result = 0;
// your code here
return result;
}
}
}
You can make a wrapper class and call from the controller else another option is that you can create a base controller and implement it in all your other controller, then you can access like base.pRound(1,1);
Related
I need to find a way to get an instance of DataProcessingEngine without calling it's constractor.
I am trying to find a way to do so using the registered DataProcessingEngine in composition object (please see the following code). But I could not find a way to do so.
Anyone have a suggestion? Thanks in advance.
public class Composer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<IDataProcessingEngine, DataProcessingEngine>(Lifetime.Singleton);
//DataProcessingEngine dataProcessing = compostion.Resolve<IDataProcessingEngine>()??//no resolve function exists in Umbraco.Core.Composing
SaveImagesThread(dataProcessingEngine);
}
public Task SaveImagesThread(IDataProcessingEngine dataProcessingEngine)//TODO - decide async
{
string dataTimerTime = WebConfig.SaveProductsDataTimer;
double time = GetTimeForTimer(dataTimerTime);
if (time > 0)
{
var aTimer = new System.Timers.Timer(time);
aTimer.Elapsed += new ElapsedEventHandler(dataProcessingEngine.SaveImages);
aTimer.Start();
}
return default;
}
}
For all of you who are looking for a way to call a function (that's defined in another class in your code, an Engine or ...) from the composer(where the app starts) and want to avoid calling this function's class' constractor. I've found another way to do so:
public class QueuePollingHandler
{
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class SubscribeToQueuePollingHandlerComponentComposer :
ComponentComposer<SubscribeToQueuePollingHandler>
{ }
public class SubscribeToQueuePollingHandler : IComponent
{
private readonly IDataProcessingEngine _dataProcessingEngine;
public SubscribeToQueuePollingHandler(IDataProcessingEngine
dataProcessingEngine)
{
_dataProcessingEngine = dataProcessingEngine;
SaveImagesThread(_dataProcessingEngine);
}
public void SaveImagesThread(IDataProcessingEngine
dataProcessingEngine)
{
....
}
}
And the logic explenation: You create a class (SubscribeToQueuePollingHandlerComponentComposer from the example) and define its base class to be ComponentComposer<Class_that_inherits_IComponent>.
And when you start the application you could see that it gets to the registered class' constractor (SubscribeToQueuePollingHandler constructor).
That's the way that I found to be able to call a function right when the application starts without needing to call its class constractor and actualy use dependency injection.
Here Cycle is a domain class
class Cycle {
int lenght = 42
String[] monitor = new String[length]
static mapping = {
monitor defaultValue:"defaultstrval(length)"
}
def defaultstrval(int length)
{
String[] defaultval =new String[length]
for(int i=0;i<length;i++)
{
defaultval[i]=","
}
return defaultval
}
}
Is Domain class only accept sql function.I really need help with good example.
Rather than using the mapping closure to call your function you can simply call the function from your variable assignment like so
String[] monitor = defaultstravel(length)
I have a code snippet here but i don't understand the use of "new (code)" in it.
type Product (code:string, price:float) =
let isFree = price=0.0
new (code) = Product(code,0.0)
member this.Code = code
member this.IsFree = isFree
Specifically why the need to enclose the "code" variable inside brackets.
That's a constructor. From MSDN: Classes (F#) (see section 'Constructors'):
You can add additional constructors by using the new keyword to add a member, as follows:
new (argument-list) = constructor-body
In your example, the Product type has one default constructor which accepts code and price, and one additional constructor that takes only code and applies the default constructor with 0.0 for price. In this case, the parentheses around code are not strictly required, and the code would compile just the same without it, although it would be required if you want constructor that takes zero parameters or more than one parameter.
The equivalent C# would be something like this:
public class Product
{
private string code;
private bool isFree;
public Product(string code, double price) {
this.code = code;
this.isFree = price == 0.0;
}
public Product(string code) : this(code, 0.0) { }
public string Code { get { return this.code; } }
public float IsFree { get { return this.isFree; } }
}
I am trying to call ZFCUser from a class within my model but i keep getting this message:
Call to undefined method Members\Model\MemberTable::zfcUserAuthentication()
this is how i tried to call it:
public function getUserEntity()
{
if($this->zfcUserAuthentication()->getAuthService()->hasIdentity())
{
if (!$this->user_entity)
{
$this->setUserEntity($this->zfcUserAuthentication()->getAuthService()->getIdentity());
}
return $this->user_entity;
}
}
i suspect that i need to implement/extend a class inorder for ZFcuser to be recoginised.
i would really appriciate some quick advice on this.
thank you
by the way
the ZFCUser works when i call it from my controller so, the use Zend\Mvc\Controller\AbstractActionController obviouly enables this to be recoginised.
but what is the equivalent of the AbstractActionController for other classes in your framework.
zfcUserAuthentication() is a controller plugin, so that's why it can be called from controllers. You can't (or shouldn't) try and access this from non-controllers. If you need the user entity within another class, you should pass it in as a dependency for that class.
Edit: Update your member table factory so it sets the user entity:
'Members\Model\MemberTable' => function($sm) {
$tableGateway = $sm->get('MemberTableGateway');
$table = new MemberTable($tableGateway);
$authService = $serviceLocator->get('zfcuser_auth_service');
$userEntity = $authService->getIdentity();
$table->setUserEntity($userEntity);
return $table;
}
add a property to your MemberTable class for it:
protected $userEntity;
and getters/setter for it:
public function setUserEntity($userEntity)
{
$this->userEntity = $userEntity;
}
public function getUserEntity()
{
return $this->userEntity;
}
then just call $this->getUserEntity() when you need it.
There are various ways to do that -
One of the way is - do the following in "MemberTable.php" file.
Maybe most of the below lines of code is already available in our project.
a. Add the below lines after 'namespace' statement -
use Zend\ServiceManager\ServiceLocatorAwareInterface; //Added Line
use Zend\ServiceManager\ServiceLocatorInterface; //Added Line
b. Change the class statement as -
class MemberTable implements ServiceLocatorAwareInterface { //Modified Line
....
.....
}
c. Add the below line at the top of the class statement -
class MemberTable implements ServiceLocatorAwareInterface {
protected $serviceLocator; //Added line
....
.....
}
d. Add the following functions -
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
e. In Members/Module.php, make the changes,
'Members\Model\MemberTable' => function($sm) {
$tableGateway = $sm->get('MemberTableGateway');
$table = new MemberTable($tableGateway);
$table->setServiceLocator($sm); //This is the important line.
return $table;
}
Now you have the 'ServiceLocator' available in the MemberTable class.
For accessing the zfcUserAuthentication() or any ControllerPlugin, you can do the following -
public function getUserEntity()
{
$zfcUserAuth = $this->getServiceLocator()->get('controllerPluginManager')->get('zfcUserAuthentication');
if($zfcUserAuth->getAuthService()->hasIdentity())
{
if (!$this->user_entity)
{
$this->setUserEntity($zfcUserAuth->getAuthService()->getIdentity());
}
return $this->user_entity;
}
}
I have a base class which uses a variable in ones of its methods and also a derived class that needs the same vaiable in its methods.
Below are the details
abstract class BaseClass
{
protected Transition transition;
public event EventHandler ActionComplete;
private string abc;
Public string ABC
{
get{ return abc;}
set { abc = value;}
}
public void TransitionState(BaseClass obj)
{
ActionComplete(this, null);
}
public abstract void RequestSomeAction(Transition obj);
}
internal class DerivedClass : BaseClass
{
//do i need to create transition variable again here
internal new Transition transition;
//this parameter's value (here obj) should be assigned to the base class,
public override void RequestSomeAction(Transition obj)
{
//is below code correct.
stateTransition = obj;
base.transition= transition;
}
}
Why the new transition variable and base.transition? If you make a variabele protected in the 'base class' you can just use it directly in the 'child class' that extends that class!?
First if you don't need to redeclare the variable, don't. You can just use the protected property.
this.transition
If you need 2 copies of the variable with the same name (i can't think of any case when you would need this) then you are technically 'hiding' versus overridding so you could do:
base.transition
or the same
((BaseClass)this).transition
MSDN on base.