What's the best way to reference code over and over? - visual-studio-2010

Just as an example, say I'd like to reference a msgbox in multiple areas, could I create a function for this? Private or Public? Differences?
Here's an example.
Public Function Message() As String
MsgBox("Test Message")
End Function
Also, why would I get the following message: Function 'Message' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used
It seems as though its just a warning and it seemed to work when I called the function.

Make it Sub so it's not expecting a return value.
Public Sub Message() As String
MsgBox("Test Message")
End Sub

Related

Accessing Property of a method from another method inside a class in Laravel

I want to Access the property "na" of "ha" public method from "test" method in same class. But I am getting error "Trying to get property 'ni' of non-object"
public function ha(){
$ni = 'fs';
$nin = 'adfsfsfs';
}
public function test()
{
echo $this->ha()->ni;
}
Variables inside a method are only available inside that method. You should either define on your class or return the values from the method and call the method to get the values. Also please note that this is not about laravel, but it's about php. For more information about variable scope: check this

Cannot set property for Model class in Laravel

I'm trying add dependency injection on User class and after few hours trying, I found this way:
I added this code to App\Models\User class
public function __construct()
{
$this->roleService = resolve(RoleService::class);
}
I've checked $this->roleService value, it work normally, buut when I try to use it in a function:
public function isAdmin() {
return $this->roleService->isAdmin($this->role_id);
}
This function throw an error Call to a member function isAdmin() on null. When I logged $this->roleService, it returned null.
Anyone can solve this?
Create a custom accessor for your model, and get/set the value from there.
public function getRoleServiceAttribute ()
{
if(is_null($this->roleService)){
$this->roleService = resolve(RoleService::class);
}
return $this->roleService;
}
Then you can access the attribute as $this->role_service
Also make sure you declare the $roleService class property as protected, to avoid accidental access as $this->roleService from anywhere else.
Thank for help. I just have referenced answer of an expert. He said that we should not dependency injection in Model class because it will broke project structure. Project should be followed structure: Controller->Service->Repository->Model->Database. So, I'll refactor my code follow it.

Using Laravel JSON response inside a __toString method

I have method in my class that returns a JSON response.
public function response(){
return \Response::json(['data'=>'somedata']);
}
To be able to use more chain methods I want to use the __toString() method whenever I want to return an object as a response. Like this:
public function __toString(){
return $this->response();
}
But I get this error:
Method MyClass::__toString() must return a string value
Which makes sense but how can I do that. I looked in Laravel and Symfony JsonResponse classes and couldn't find a method to fix this. I tried the getContent() but that is just a string not a proper Json response.
After a lot of research I found a solution. I don't know if it is the best but here it is:
public function __toString(){
$this->response()->send();
return '';
}
Which is actually a call to Symfony\Component\HttpFoundation\Response::send() method instead of returning an string.
Thyis is a late response but I found a better solution, letting Laravel handle the response:
public function __toString(){
return Response::json($this)->content() ' Returns a string
}

Freemarker call Java Filter

In my Java code im adding a POJO
rootMap.put("_Field_",field);
there is a function "getFilteredHtml" that for the time being does nothing but return the string
that is given as a parameter (the idea is later to alter this - but for test purposes it keeps the html untouched).
/**
* get filtered html for this field
* #param html
* #return
*/
public String getFilteredHtml(String html) {
return html;
}
in the Freemarker template I am using this function like this:
${_Field_.getFilteredHtml(fieldRef?eval!"-")}
which creates the following error message:
Root Cause: freemarker.template.TemplateModelException: Argument type mismatch;
can not unwrap argument #1 (class: freemarker.ext.beans.NumberModel, toString: "4")
to class java.lang.String
I tried to understand what http://freemarker.org/docs/pgui_misc_beanwrapper.html
wants to tell me but this is all greek to me. How can I avoid this Beanwrapper behaviour and make
sure the function getFilteredHtml is simply called with all this mumbo jumbo about wrapping and unwrapping?
Since no other answer showed up I'm citing my comment:
looks like modifying getFilteredHtml to public Object getFilteredHtml(Object html) does the trick. The error message is simply misleading because it talks about freemarker.ext.beans.NumberModel when trying to say that a string is not expected when the input is a long (or some other numeric type)

Passing Instance to Shared Method in VB.NET

I have been thrown in at the deep end with an existing VB.NET project at work. I have never used VB.NET before so I am struggling a little. Does anyone know how to solve the following.
I need to pass an instance to client side and then pass it to a shared method in order to access instance methods from when the shared method.
The starting point is a fileupload control within the HTML of my Contacts.aspx file:
<asp:FileUpload ID="DocUpload1" runat="server" onchange="CallMe();" />
The onchange event calls a javascript method, see below, this uses AJAX PageMethods to called a Shared method in my code behind
This is the script code which is in my Contact.aspx file
<script language="javascript">
function CallMe() {
// call server side method
PageMethods.GetContact(0, CallSuccess, CallFailed, null);
}
// set the destination textbox value with the ContactName
function CallSuccess(res, destCtrl) {
}
// alert message on some failure
function CallFailed(res, destCtrl) {
alert(res.get_message());
}
</script>
This is an example class of the type of thing I want to do, I think I need to use the "instance As Contacts" as an input parameter to the WebMethod function but I don't know how to pass the instance into it:
This is the class within my Contacts.aspx.vb file.
Partial Class Contacts
<System.Web.Services.WebMethod()> _
Public Shared Function GetContact(ByVal instance As Contacts) As String
Return instance.GetContactName() 'This is an instance class which I need to call.
End Function
'This is my instance class which I want to call from the Shared Class.
Public Shared Function GetContactName() As String
Return "Fred Bloggs"
End Function
End Class
If anyone knows the solution please could they update the code as I probably won't be able to understand if you just give a description. I just hope I am along the right tracks.
If I understand you correctly, you want to access a class (your instance) created in the ASP.Net page life-cycle from your PageMethod - eg created during initial page load or file upload etc.
This is not directly possible as PageMethods do not go through the full page life-cycle (they are essentially webservices). So you need to pass some sort of identifier to the client that, when passed back to the server in the PageMethod, can be used to re-create or retrieve your instance.
eg During the initial page load:
session("ContactID") = instance
Your PageMethod might look something like:
Public Shared Function GetContact(ByVal key As String) As String
Return HttpContext.Current.Session(key).GetContactName()
End Function
where the parameter key is the same key you used to store your instance in the session state.
In your javascript:
function CallMe() {
// call server side method
PageMethods.GetContact('ContactID', CallSuccess, CallFailed, null);
}

Resources