ModelState does not contains AddModelError() [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was working with a ModelState.AddModelError() recently. But there is a problem. I have 2 files in one when I type ModelState. intellisense shows the AddModelError() method. But in other file its not. I am getting the error there. ModelState.AddModelError() is defined in the System.Web.Mvc.Controller.The first file contains the Controller class within System.Web.Mvc. But I could not find Controller
class in the second file even though its using System.Web.Mvc. Can anyone help with this?

It doesn't use, it actually inherits from the Controller class. The one in which you could use that must be a Controller which inherits from the Controller parent class.
And the one in which you couldn't use that method must be some other class which obviously didn't inherit from the Controller class.
In order to use some of the functionalities including this, your class has to inherit the parent Controller class.
Hope this clears the idea.

Related

How to compare a String object attribute to a string in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am writing a celebrity buzzfeed style quiz and I am having an issue trying to compare the ATTRIBUTE OF AN OBJECT (which is a string I checked using the GetType method) to a user inputed string. (yes I have used the type check method to test the type of the attribute, also I define the attributes as type string in the class). I know you can do this in python with == and in Java you can do this with the .compare method, but I am somewhat new to using objects in C# and so I'm not sure if there is a specific method to use.
code i am trying
Celebrity class
Celebrity Class 1
Celebrity Class 2
I have already tried the String.Equals method as well as == operator to check and both give me the same error. In the picture I commented out the other conditional statements to make error checking easier. But basically this method takes a list of celebrity objects as well as a list of strings that were entered by a user, and in the end returns a Celebrity object (as per the quiz this will be the winner).
main.cs(58,17): error CS0119: Expression denotes a method group', where avariable', value' ortype' was expected
Error message
Please let me know if this is impossible, or if this would work if the objects were not in a list.
c.getStarbucks looks like it should be a method call i.e. c.getStarbucks(), and would cause the compilation error you are receiving.

Calling a class in another way [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am just a beginner and I am trying to learn other alternatives.
Is there another way to call a class from another class. For example, I have a class called Test, can you call it in another way from this one = Test example = new Test();
You can have a Type Factory class which can create an instance for you dynamically. Read some articles about type factory.
Its a very common way to dynamically create instances of a type.
Lets say you have a program that generates text files. As parameter you can ask for a specific template, the program should then browse through your classes of type text generator and create an instance matching your requested template.
If you need to create an instance of this class i.e this is not a static class then
Test example = new Test();
is the correct way to call this class.
If this was a static class e.g public static Test {... then you could call this class without creating a new instance of it e.g Test.SomeMethod Example Link

Tiny Csv Parser - Parse negative int [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Found this csv parser: https://github.com/bytefish/TinyCsvParser
Following his example but I even added one int property.
By setting a negative value to that int, it gave me one unvalid row.
Couldn't find any solution for this simple? problem.
The type converter was using the wrong NumberStyle as default. I have fixed the problem and added a Test case to the project: https://github.com/bytefish/TinyCsvParser/issues/2.
You could also instantiate a custom converter with the right NumberStyle (see WithCustomConverter, Example), but I suggest simply updating to the latest version (0.6), which is also updated in NuGet.

The best way to pass values between classes in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
First case:
If I have a set/get method for a value inside the class A, I can
set that value from the class B and use it inside A.
Second case:
I can just pass the value in a traditional way like Method(value);
Please, explain me which way is better.
I appreciate your answers.
Properties (what you call the set/get method) are essentially a "syntax sugar" on top of regular C# methods. There will be no performance difference between using properties and using regular methods.
Generally, though, you should prefer properties to methods for readability, i.e. when they present an appropriate semantics to the readers of your class.
Setters and Getters should be used for general properties of classes, used across several methods.
A parameter to a method call is appropriate for a variable tied to that one method (though possibly stored and used elsewhere, for instance if it is part of initialisation).
As always, do what looks best and works well in your context. If the using code feels awkward, look for another way. If it feels right, it's probably OK.
The goal of Object oriented programming is to have your data and operations together.
The goal is to reduce coupling between different kinds of objects so that we can re use the classes.
Never expose the data inside the class to the outside world but provide interfaces to do so

Return a String to Controller from a View [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My project structure looks like this:
Solution-->Project-->Views-->Name.xaml-->Name.xaml.cs
-->Controllers-->NameController.cs
I'm trying to get string name from the Views folder. name is an .xaml file, and the string is in the back code.
I'm trying to do a simple get and return in NameController.cs to get string name. String name is the text from a textbox.
When I attempt this:
public string StringName
{
get
{
return Views.Name.Namebox.Text;
}
}
I get this error:
An object reference is required for the non-static field,
method, or property 'Project.Views.Name.Name'
Thank you for all of the help.
Edit:
Here's some pictures of my current code.
Textbox code--
Underlying code--
Controller code <-- This code in unfinished on purpose. It doesn't even see ProductId as an object.
The error is pretty clear about what's going on: you're trying to access an instance value as if it were a static one.
So the question now becomes:
How do I access an instance value?
The answer to which likely won't solve your problem:
Instance values can only be accessed via an instance of the object.
So in the end, what you're really asking is this:
How do I get an instance of my ProductIdView object?
You can go about it a number of different ways, but in the end it all comes down to you calling new ProductIdView() somewhere and then putting that reference someplace where it'll be in scope when you need it.

Categories