Error code - non static method requires a target [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 6 years ago.
Improve this question
My problem is an error while executing this code. This function work perfectly into an other utilisation.
This is my complete function code :
public List<t_heure_indemnite> GetAllHeureIndemnite(FirmMda f, bool justeActive)
{
List<t_heure_indemnite> lHi = new List<t_heure_indemnite>();
ce = new callistoEntities();
try
{
var load = from i in ce.t_heure_indemnite
where i.FIRM == f.Name
select i;
if (load != null)
{
List<t_heure_indemnite> liste = load.ToList();
foreach (t_heure_indemnite h in liste)
{
lHi.Add(h);
}
}
}
catch (Exception e)
{
MsgBox.Show(globale.AfficheErreur(e));
}
if (justeActive)
return lHi.Where(i => i.ACTIVE == true).OrderBy(j => j.ORE).ToList();
else
return lHi.OrderBy(j => j.ORDRE).ToList();
}
Exception is present while executing : Non static method requires a target.
It appears when load.ToList is called.
It's called into public method and during an import traitement into an other list.
I don't know where is problem. Can you help me please ?

It can happen when your you use a null-reference variable in a lambda expression. I would suggest to check if your variable ce.t_heure_indemnite is a null-reference.
if(ce.t_heure_indemnite != null)

Related

Problems with Unity Random Function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Good evening, I was working on a graphic adventure and I want to make a random function with a 4 digit code, turns out that as I am a very novice programmer, I crashed with an inconvenient
private void CheckResults()
{
if (codeSequence = RandomCode.RanCode.ToString())
{
SoundManager.PlaySound("nice");
}
else
{
SoundManager.PlaySound("wrong");
}
"RandomCode.RanCode" is a int void, and adding "ToString" will shot an error saying
"RanCode is a method which is not valid in the given context"
This is the RandomCode void:
public void RanCode()
{
Code = Random.Range(1111, 9999);
}
If anyone out there has any ideas or solves, I will be eternally grateful
There's no such thing as an "int void". A method either returns nothing (void) or returns a value (in your case you seem to expect int).
// Returns nothing
// |
// v
public void RanCode()
{
// assigns result of `Range` to `Code` property/field
Code = Random.Range(1111, 9999);
}
If you want to also return the value, you need to rewrite your method like this:
public int RanCode()
{
Code = Random.Range(1111, 9999);
return Code;
}
You do have another issue here:
RandomCode.RanCode.ToString()
To call a method you need (), so it should be this:
RandomCode.RanCode().ToString()
Also, for equality checks you want == not = (which is assignment).
I do wonder why you're assigning a value to Code here. If we use your existing code (without my fix above), I guess that perhaps you intended something like this instead?:
private void CheckResults()
{
RandomCode.RanCode();
if (codeSequence == RandomCode.Code.ToString())
{
SoundManager.PlaySound("nice");
}
else
{
SoundManager.PlaySound("wrong");
}
}

Why is C# console application not returning result on LINQ Select with data [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 5 years ago.
Improve this question
I'm writing a rather complicated database scanning utility using a console application, but for some reason I'm running into the most basic of problems when I attempt to do a simple LINQ SELECT on a database table with data.
When the database table has no data, the query returns control to the console application.
When the table has data, SQL server shows "SUSPENDED" and "ASYNC_NETWORK_IO".
Thank you in advance for any assistance.
Program.cs looks as follows:
class Program
{
static void Main(string[] args)
{
DataModels dm = new DataModels();
List<string> lsReturn = dm.ReturnDataFromTableX();
}
}
DataModels.cs looks as follows:
class DataModels
{
public List<string> ReturnDataFromTableX()
{
return _DataEntities.<TABLE_NAME>.Select(x => x.<COLUMN_NAME>).ToList();
}
}
First you can try
class DataModels
{
public IEnumerable<string> ReturnDataFromTableX()
{
return _DataEntities.<TABLE_NAME>.Select(x => x.<COLUMN_NAME>);
}
}
it will track the returned row in the table and if your processing is not really long, than it should be already enough
or alternatively ready block by block in that case connection will be opened to read portion of data each time it is really required:
var page = 0;
var step = 10;
var block = _DataEntities.<TABLE_NAME>.Skip(page * step).Take(step).Select(x => x.<COLUMN_NAME>).ToList();
while (block.Count > 0)
{
foreach (var item in block)
{
yield return item;
}
page++;
block = _DataEntities.<TABLE_NAME>.Skip(page * step).Take(step).Select(x => x.<COLUMN_NAME>).ToList();
}

What's wrong? I can't seem to close this code out [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I cant figure out how to close this out properly, no matter where i add a bracket it breaks the code.
I appreciate the help, thanks :)
int NewID = Convert.ToInt32(Adapter.InsertQuery()); // new relationship id
if (!Session.GetHabbo().Relationships.ContainsKey(Them))
Session.GetHabbo().Relationships.Add(Them, new Relationship(NewID, Them, 3)); // create the relationship
Session.GetHabbo().GetMessenger().UpdateFriend(Them, Session, true);
}
else
{
Habbo Habbo = PlusEnvironment.GetHabboById(Them);
if (Habbo != null)
{
MessengerBuddy Bud = null;
if (Session.GetHabbo().GetMessenger().TryGetFriend(Them, out Bud))
Session.SendMessage(new FriendListUpdateComposer(Session, Bud));
}
}
return false;
}
P.S I'm still new to this.
This seems like a simple fix, but if you format your code this sort of stuff usually sticks out. In Visual Studio you can hit Ctrl+K, Ctrl+D (in succession) and it'll format your code to the default.
int NewID = Convert.ToInt32(Adapter.InsertQuery()); // new relationship id
if (!Session.GetHabbo().Relationships.ContainsKey(Them))
Session.GetHabbo().Relationships.Add(Them, new Relationship(NewID, Them, 3)); // create the relationship
Session.GetHabbo().GetMessenger().UpdateFriend(Them, Session, true);
}
else
{
Habbo Habbo = PlusEnvironment.GetHabboById(Them);
if (Habbo != null)
{
MessengerBuddy Bud = null;
if (Session.GetHabbo().GetMessenger().TryGetFriend(Them, out Bud))
Session.SendMessage(new FriendListUpdateComposer(Session, Bud));
}
}
return false;
}
Alright. Now that that's done. You're, in general, missing your beginning brackets on your if-statements.
int NewID = Convert.ToInt32(Adapter.InsertQuery()); // new relationship id
if (!Session.GetHabbo().Relationships.ContainsKey(Them))
{ // Added here
Session.GetHabbo().Relationships.Add(Them, new Relationship(NewID, Them, 3)); // create the relationship
Session.GetHabbo().GetMessenger().UpdateFriend(Them, Session, true);
}
else
{
Habbo Habbo = PlusEnvironment.GetHabboById(Them);
if (Habbo != null)
{
MessengerBuddy Bud = null;
if (Session.GetHabbo().GetMessenger().TryGetFriend(Them, out Bud))
{ // Added here.
Session.SendMessage(new FriendListUpdateComposer(Session, Bud));
}
}
return false;
}
So if you add those two brackets noted by the // Added here comments, that should work.

Lacking await operators? [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 6 years ago.
Improve this question
Error comes up for the method requiring await operators but where do I put them and what is an await operator and async and Task? :)
public async void OnLocationChanged(Location location)
{
_currentLocation = location;
if (_currentLocation == null)
{
_locationText.Text = "Unable to determine your location. Try again in a short while.";
}
else
{
_locationText.Text = string.Format("{0:f6},{1:f6}", _currentLocation.Latitude, _currentLocation.Longitude);
as
}
}
I want specific help in the example above :)
Just remove asycn keyword from method's signature !
public void OnLocationChanged(Location location)

Check Public Static Bool in C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
im kinda new to C# and i got a code that uses public static bool.
But how do i check it?
I tried to do this
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
{
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
}
catch
{
return false;
}
}
private async void Form1_Load(object sender, EventArgs e)
{
await Task.Delay(5000);
if (CheckForInternetConnection() = true)
{
}
}
And it gave me an error.
I confirm the comments: it's only a syntax error due to the lack of the 2nd = char in the if(...) conditional block. I've tested your code in a console project on the run and, with the 2nd = it works fine.

Categories