|
You're trying to cast the objects inside the List, but you're actually casting the List itself. If you're using version 3.5 of the .Net Framework, then you can simply use return LoadChild(ID).Cast<City>();
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
WELL i have done LoadChild(ID).Cast< City >();
but Error occurs aggain: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<g1_onlinestoreobjects.city>' to 'System.Collections.Generic.List< G1_OnlineStoreObjects.City >'. An explicit conversion exists (are you missing a cast?)
C# Developer
|
|
|
|
|
This is because the extension method Cast() returns an IEnumerable<t>. Just cast it to the property's type and you're done
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
I did not understand
right that: (City)LoadChild(ID).Cast< City >();
but occuers Error again
C# Developer
|
|
|
|
|
LoadChild(ID).Cast<City>() as List<City>;
Besides that, you seem to have a small amount of confusion with List casting. When you cast a List to a City, you do not cast the elements in the List, only the List itself. In order to cast the elements inside the List, you need to use the Cast() method. This method returns an IEnumerable; because List implements IEnumerable, you can cast the List. This does not cast the elements, just the containing class
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
well it's works but there is some another problem like this:
Here is class where I invoked function into delegate
public class CountryDAO : BaseDAO< Country >
{
public override Country Load(int id) //I try to do lazyloading
{
Country country = base.Load(id); //just loads from database to Country class objects
if (country != null)
{
country.LoadChild = LoadCity;// here I invoked it and here occurs error
}
return country;
}
private List< City > LoadCity(int parentId)
{
CityDAO cDao = new CityDAO();
return cDao.LoadList("CountryID=" + parentId.ToString());
}
}
Error: 'System.Collections.Generic.List< G1_OnlineStoreObjects.City > G1_OnlineStoreDAO.CountryDAO.LoadCity(int)' has the wrong return type
what's the matter, why cann't compile it?
I repeat it what you said me to cast bu it is not works again
C# Developer
|
|
|
|
|
This is because you have the wrong return type. The delegate is of type List<Country> LoadChild(int id) , but the delegate you try to convert it to is of type List<City> LoadChild(int id) . That's the main problem, but it seems more architectural than syntactic. Either your delegate type is wrong, City does not inherit from Country, or City and Country don't implement a common interface. Personally, I wouldn't use a generic delegate at all; I'd go for something like this:
public delegate List<City> LoadCitiesDelegate(int countryId);
public class CountryDAO : BaseDAO<Country>
{
public override Country Load(int id)
{
Country country = base.Load(id);
if(country != null)
country.LoadChild = loadCity;
return country;
}
private List<City> LoadCities(int countryId)
{
CityDAO cDao = new CityDAO();
return cDao.LoadList("CountryID=" + countryId.ToString());
}
}
This probably wouldn't compile, but it's a general idea of the way I would personally do it
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Maybe it'll work, but in my project I have many classes and Functions with generics and it's almost impossible to change all
I think I solve this like this, I write it:
public delegate List< T > LoadChildHandler< T >(int parentId);
public class ObjectBase< T >
{
public int ID { get; set; }
}
public class Country : ObjectBase< Country >
{
public LoadChildHandler< City > LoadChild; // I just declare delegate in child class and there are not resist between City and Country, well works fine
public string CountryName { get; set; }
public List< City > Cities
{
get
{
return LoadChild(ID);
}
}
}
and so all I want it to do in a different way...
Now I must dclare this delegate in other classes too, it would be fine to do solve this problem, without declaring in other classes and not such torture
C# Developer
|
|
|
|
|
Hi
I have a Windows form called mainwindow in my project. After it completes what it does, it closes and then recreates itself. But as I have noticed lately it recreates but doesnt't close itself.
Here is my code:
private void RestartAll()
{
depo.multipleformlocation=this.Location;
depo.multipleform = true;
depo.ekrangoruntusuadedi = 0;
this.Hide();
mainwindow _mainwindow = new mainwindow();
_mainwindow.ShowDialog();
this.Close();
Thread.Sleep(Timeout.Infinite);
}
How can I close(destroy) the old form completely and recreate that form? When I don't completely close that old form, because I do this closing and recreating process more than once my App's memory usage gets much higher.
Thanks in advance.
|
|
|
|
|
I am having a little difficulty in understanding where the cause of your problem might lie.
Just to clarify things, what class contains the RestartAll method in the code you posted?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
RestartAll method is in the mainform class.
|
|
|
|
|
Try this:
private void RestartAll()
{
depo.multipleformlocation=this.Location;
depo.multipleform = true;
depo.ekrangoruntusuadedi = 0;
this.Hide();
using (mainwindow _mainwindow = new mainwindow())
{
_mainwindow.ShowDialog();
}
this.Close();
Thread.Sleep(Timeout.Infinite);
}
Using the using construct 'should' ensure that _mainwindow is disposed.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thank you. It seems to be working. Should I keep using the Thread.Sleep(Timeout.Infinite); line?
|
|
|
|
|
Try commenting it out and see if everything still works. As you said in your OP it was a temporary solution, and shouldn't be necessary. However, if you still have the old problem after commenting it, the fix hasn't worked and it's back to the drawing-board.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I have commented it and seems like it's still working.
Thank you for your help.
|
|
|
|
|
Excellent.
Try to remember the 'using' construct, whenever you create anything IDisposable. I don't always, particularly when I'm 'just trying something out' and forgetting nearly always causes me problems.
Good Luck!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi,
I don't understand what it is you are doing here. Either this code runs on the GUI thread, and your Sleep(Infinite) hangs it forever, or it runs on some other thread, and it is not allowed to touch any Controls.
I am afraid your approach is completely wrong.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Well, this method doesn't run on the GUI thread but Control.CheckForIllegalCrossThreadCalls is set to false , so all threads are allowed to touch all the Controls.
|
|
|
|
|
SimpleData wrote: so all threads are allowed to touch all the Controls.
That is absolutely false. All CheckForIllegalCrossThreadCalls=false; does is disable the checking (returning to the unhealthy situation that existed in .NET 1.0/1.1), it does not ALLOW anything. Controls are not thread-safe and should be touched only by the thread that created them, and since all Controls are linked somehow (through Control collections, Form Z-Order, etc) that basically means the initial/main/GUI thread is the only one that can touch them. Violating this rule may and will result in abnormal app behavior, typically the GUI freezing pretty soon (or after a possibly long while, but failure is guaranteed).
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
That's good to know, I didn't know that.
I will test my application's stability, maybe it won't be a problem.
|
|
|
|
|
It may pass your tests, but when released into the real world, your code WILL fail. It's just a matter of time before someone out there find the combination of circumstances that will make it happen.
|
|
|
|
|
Just because the property is there doesn't mean it should be used.
I concur with Luc, your approach is completely wrong and unsupportable. I really can't suggest a replacement because it isn't clear what you're trying to accomplish.
Also, when you show a form with ShowDialog, you are responsible for calling Dispose on it to free up unmanaged resources before the object goes out of focus and is destroyed. As it stands now, your code leaks system resources, specifically, window handles. Your code, if left running long enough, will eventually crash Windows.
|
|
|
|
|
What can I do to prevent this?
|
|
|
|
|
Again, we can't really tell you al alternate method without knowing what you're trying to do with this code.
|
|
|
|
|
I am coding an AntiCheat application which takes screenshots of the game and uploads to the server after the games is closed.
When the app is launched, first the login form appears, if the login info is correct, login from closes and mainform that we are talking about appears. App waits for the user to open a supported game. When it's opened it takes screenshots in random intervals. After the game is closed app uploads the taken screenshots.
After uploading them in order to start waiting for a new game to open, I close the mainform and recreate it. This recreation process is what we are talking about.
|
|
|
|