Pull content from tablen into a class [closed] - c#

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 7 years ago.
Improve this question
I sit in a problem that do that it will not udhente some content from the database into my class,
it must be such that I have to download content from the database.
error are
Provides for a specific object reference to the 'point.db' which is a field, a method or a property that is not static
i have try to this here
DataClassesDataContext db = new DataClassesDataContext();
public static string OpretBrugerPointAdd()
{
PointBonus pointbonusStart = db.PointBonus.FirstOrDefault(P => P.Id == 1);
if (pointbonusStart != null)
{
return Convert.ToString(pointbonusStart.point);
}
}
when I call it happens like this,
string pointAntal = point.OpretBrugerPointAdd();

You're trying to use non-static field from static method. That's not going to work.
You should instantiate new DataClassesDataContext within your method instead:
public static string OpretBrugerPointAdd()
{
using(var db = new DataClassesDataContext())
{
PointBonus pointbonusStart = db.PointBonus.FirstOrDefault(P => P.Id == 1);
if (pointbonusStart != null)
{
return Convert.ToString(pointbonusStart.point);
}
}
}
You also have to deal with a fact, that your method doesn't return anything when if statement condition evaluates to false. You should probably add return null (or other default value you wish) at the very end of you mehtod to fix that.

Related

Is there built-in standard method to act like If functional? [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 9 days ago.
Improve this question
I currently work on project to adapt several design patterns that I think, it pleasant in eyes to maintain. the one is nested less. In c++ I can achieve it; there's a standard method that can do it.
public IActionResult OnPostAsync()
{
if (ModelState.IsValid)
{
// do something validation again with persistence context
var query = context.Users
.Where(u => Username == this.Username &&
u => Password == this.Password)
.FirstOrDefault<User>();
// double nested again
if (query is not null)
{
return RedirectToPagePermanent("/dashboard");
}
//
}
return Page();
}
What I expected is the code will like this before I implemented it myself.
bool state = ModelState.IsValid;
null result = FooFunction(expected return type, pass context function like ContextFunction(username, password));
return FooFunction(IActionResult, RedirectToPagePermanent("/dashboard");
So FooFunction is standard common to do encapsulate situation like this.
In C++, this kind function does exist, so I think C# will have it, too.

Check for null and assign to a variable at once in C# 8 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Probably a trivial question, but I'm trying to get up to date with modern C# and I am overwhelmed with all the new features like pattern matching etc.
With C# 8, is there a new way to simplify the following common pattern, were I check a property for being non null and if so, store it in a var for use within the if scope? That is:
var item = _data.Item;
if (item != null)
{
// use item
}
I could think of this:
if (_data.Item is var item && item != null)
{
// use item
}
And this:
if (_data.Item is Item item)
{
// use item
}
Between these, I'd still pick the 1st snippet.
Also you can use empty property pattern:
if (_data.Item is {} item)
{
// use item
}
Null propagation.
var result = _data.Item?.UseItem()
or in a method
var result = UseItem(_data.Item?.Value ?? "some default value")
This pattern, where myVariable may be located outside of the method
if (myVariable == null)
{
myVariable = GetSomeFallbackValue();
}
return myVariable;
may be shorten as
return myVariable ?? (myVariable = GetSomeFallbackValue());
and the same with C# 8.0 new syntax
return myVariable ??= GetSomeFallbackValue();
The pattern is often used for lazy initialization in Property getter, and it's suitable for code written in expression form.
private MyClass myBackingField;
public MyClass MyProperty
{
get => myBackingField ??= new MyClass();
set => myBackingField = value;
}
The value for backing field will be instantiated on first call of the Property getter. Thus, the value of the MyProperty will never be null.
3 operations at once:
check for null
conditionally assign fall back value
and return (or use) the result

how to declare a list of object nullable asp .net [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 5 years ago.
Improve this question
Good Morning ,
I'm a beginner in asp .net
Acually I prepare the database using the first code approach
And my question is: How to declare a list of objects that can be null ?
Looking at your diagram, I'm guessing what you meant is that your Employer object may have 0 or more BonDeCommande object. As such List<BonDeCommande> is what you want (which may have an empty list -- rather than null)
public class Employer {
public string code {get; set;}
public string mdp {get; set;}
...
public List<BonDeCommande> bonDeCommandes {get; set;}
}
List<int?> aList = new List<int?>();
The question mark means that the value can be null
List it self can be null, so the object you do not have to specify that somehow else. After last line of example code, list "val" will contain object o =null.
List<object> val = null;
List<object> val = new List<object>();
object o = new object();
o = null;
val.Add(o);

I have a method which returns class type 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 6 years ago.
Improve this question
I'm new to programming and C# and i have this below question which i do not know what to call it as
I have a method as
public Account GetAccountDetails(){ //where Account is a class
return new Account{
text = GetNameOfAccount;
length = GetLengthOfAccountNumber;
};
}//end of method
I knew that i would be able to get the properties and fields from the above method and i can use it. But, what do you call the above method as some object initializers or anonymous type? How is it useful than explicitly defining an object for Account class and using the object for accessing the methods?
I think the method you mentioned is like a ordering the Set Menu of McDonald.
Imagine if you are working in McDonald and large number of people stand in front of you.
If Set Menu is not exist, every people order buger and coke and potato or other menu separately. It test your brain.
but in real world, some people order Set menu and some people order separately.
so you can memory them more efficiently.
It is also express to code.
public Order getSetA() {
return new Order {
Buger = Menu.CheeseBugger;
Drink = Menu.Coke;
SideMenu = Menu.Potato;
};
}
public Order getSetB() {
return new Order {
Buger = Menu.BigMac;
Drink = Menu.Water;
SideMenu = Menu.Pickle;
};
}
Order order = new Order();
RandomBugerForToday(out order.Buger);
RandomDrinkForToday(out order.Drink);
order.SideMenu = null;

How do I stub IQueryable<int> and IQueryable<String> [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 7 years ago.
Improve this question
How do I stub these methods:
ProductDAL:
public IQueryable<string> getCatNameList()
{
var db = new Database();
var categoryNames = db.ProductCategories.Select(c => c.Name);
return categoryNames;
}
public IQueryable<int> getCatIdList()
{
var db = new Database();
var categoryID = db.ProductCategories.Select(c => c.Categoryid);
return categoryID;
}
IProductDAL:
IQueryable<int> getCatIdList();
IQueryable<string> getCatNameList();
Been searching on stackoverflow, but no questions/answers that gets me any closer to understanding how I go about doing this.
In this case I would recommend you change your interface to a series of IEnumerable rather than IQueryable. There is little point having these methods as queryables as they have already been projected.
If you are just trying to stub them, use Enumerable.Empty<type>().AsQueryable().
Create and IEnumerable of whatever you need to return (can be a List or an array). Then convert it to IQueryable using AsQueryable extension method and pass it to whatever mocking/stubbing library you use.

Categories