|
|
You seem adverse to writing a device driver (probably the "normal" solution); I was suggesting the simulator as the seed for some other solution. My fault if I did not state that explicitly (sigh).
If you want to reject it outright, that's up to you. I never suggested it be used "as is".
|
|
|
|
|
|
Quote: Also, maybe you forgot but we are in the C# forum.... Not the C++ one.
Yeah; I wonder why I bother.
|
|
|
|
|
Datasource:
ID Description ParentID
1 Continent Null
2 Country 1
3 Province 2
4 City1 3
5 Suburb1 6
6 City2 3
7 Suburb2 6
8 Suburb3 6
9 Suburb4 4
10 House1 7
11 House3 9
12 House4 8
13 House5 8
14 House6 7
The output should be in this manner
Output:
--Country
---Province
----City1
----- Suburb4
------ House3
----City2
----- Suburb1
----- Suburb2
------ House1
------ House6
----- Suburb3
------ House4
------ House5
Please help
|
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MatsTest
{
class Program
{
static void Main(string[] args)
{
List<Area> areas = new List<Area>();
Area area = new Area();
area.ID = 1;
area.Description = "Continent";
areas.Add(area);
Area area2 = new Area();
area2.ID = 2;
area2.Description = "Country";
area2.ParentID = 1;
areas.Add(area2);
Area area3 = new Area();
area3.ID = 3;
area3.Description = "Province";
area3.ParentID = 2;
areas.Add(area3);
Area area4 = new Area();
area4.ID = 4;
area4.Description = "City1";
area4.ParentID = 3;
areas.Add(area4);
Area area5 = new Area();
area5.ID = 5;
area5.Description = "Surbub1";
area5.ParentID = 6;
areas.Add(area5);
Area area6 = new Area();
area6.ID = 6;
area6.Description = "City2";
area6.ParentID = 3;
areas.Add(area6);
Area area7 = new Area();
area7.ID = 7;
area7.Description = "Surbub2";
area7.ParentID = 6;
areas.Add(area7);
Area area8 = new Area();
area8.ID = 8;
area8.Description = "Surbub3";
area8.ParentID = 6;
areas.Add(area8);
Area area9 = new Area();
area9.ID = 9;
area9.Description = "Surbub4";
area9.ParentID = 4;
areas.Add(area9);
Area area10 = new Area();
area10.ID = 10;
area10.Description = "House1";
area10.ParentID = 7;
areas.Add(area10);
Area area11 = new Area();
area11.ID = 11;
area11.Description = "House3";
area11.ParentID = 9;
areas.Add(area11);
Area area12 = new Area();
area12.ID = 12;
area12.Description = "House4";
area12.ParentID = 8;
areas.Add(area12);
Area area13 = new Area();
area13.ID = 13;
area13.Description = "House5";
area13.ParentID = 8;
areas.Add(area13);
Area area14 = new Area();
area14.ID = 14;
area14.Description = "House6";
area14.ParentID = 7;
areas.Add(area14);
var parent = areas.FirstOrDefault(s => s.ID.Equals(1));
var parentids = areas.Select(x => x.ParentID).Distinct().ToList();
foreach (var i in areas)
{
if ( i.ParentID == 0)
{
Console.WriteLine("-" + i.Description);
List<Area> children = areas.FindAll(s => s.ParentID.Equals(i.ID)).ToList();
foreach (var c in children)
{
Console.WriteLine("--" + c.Description);
List<Area> children2 = areas.FindAll(s => s.ParentID.Equals(c.ID)).ToList();
foreach (var c2 in children2)
{
Console.WriteLine("---" + c2.Description);
List<Area> children3 = areas.FindAll(s => s.ParentID.Equals(c2.ID)).ToList();
foreach (var c3 in children3)
{
Console.WriteLine("----" + c3.Description);
List<Area> children4 = areas.FindAll(s => s.ParentID.Equals(c3.ID)).ToList();
foreach (var c4 in children4)
{
Console.WriteLine("-----" + c4.Description);
List<Area> children5 = areas.FindAll(s => s.ParentID.Equals(c4.ID)).ToList();
foreach (var c5 in children5)
{
Console.WriteLine("------" + c5.Description);
}
}
}
}
}
}
}
Console.ReadLine();
}
}
}
|
|
|
|
|
That produces the output you require. Why do you want a recursive function?
|
|
|
|
|
Because it has to be done using a recursive function, I dont know why, it just has to work that way.
|
|
|
|
|
Did you check out my link in my other reply? And did you take on OriginalGriff's suggestion to simplify your set up code?
Are you still struggling?
|
|
|
|
|
Yes i checked out the link and im still struggling. I dont know how to get it right.
|
|
|
|
|
Ok. Caveat ... this is not necessarily the "best" solution.
First - here is my interpretation of your Area class - Note I have included the constructor suggested by @OriginalGriff
public class Area
{
public Area(int id, string description, int parentId)
{
Id = id;
Description = description;
ParentId = parentId;
}
public int Id { get; set; }
public string Description { get; set; }
public int ParentId { get; set; }
} This means I can do the data set up as follows
var areas = new List<Area>
{
new Area(1, "Continent", -1),
new Area(2, "Country", 1),
new Area(3, "Province", 2),
new Area(4, "City1", 3),
new Area(5, "Suburb1", 6),
new Area(6, "City2", 3),
new Area(7, "Suburb2", 6),
new Area(8, "Suburb3", 6),
new Area(9, "Suburb4", 4),
new Area(10, "House1", 7),
new Area(11, "House3", 9),
new Area(12, "House4", 8),
new Area(13, "House5", 8),
new Area(14, "House6", 7)
}; Now I need a recursive function. I chose to do this as an Extension of the List class but you could just have it as a private method within your program
static class Extensions
{
public static void PrintChildren(this List<Area> areas, Area a, int level)
{
Console.WriteLine("{0}{1}", new String('-', level), a.Description);
var childrenIds = areas.Where(x => x.ParentId.Equals(a.Id)).ToList();
if (childrenIds.Count <= 0) return;
foreach (var c in childrenIds)
PrintChildren(areas, c, level + 1);
}
}
Things to note - I'm passing the "level of recursion" down the stack to make it easier to work out how many '-' characters to insert. And the function "calls itself" if the node has any children at all.
To start off the process just use
areas.PrintChildren(areas[0], 0);
Console.ReadLine(); in your Main method.
It really is worth following this through in your IDE debugger - put a break point on
Console.WriteLine("{0}{1}", new String('-', level), a.Description); and look at your "Locals" window in the IDE. Try to follow what is actually happening. You can also right-click on the function name and "Show Call Stack on Code Map" to see what is happening.
This looks like homework, so remember your tutor may also spot this answer - make sure you do the debug bit so you understand what is going on.
|
|
|
|
|
Thanks a million. It works. I can learn alot from you....I really appreciate your effort.
|
|
|
|
|
Nicely done. +5
/ravi
|
|
|
|
|
And that's not even slightly recursive!
Try writing a method which looks at a node, and prints it and then calls the same method on all it's children. Add indenting afterwards - just get it working first.
But I'd start by changing your set-up code.
Create a Area constructor that takes an ID, a description, and a parent ID.
Then just do this:
List<Area> areas = new List<Area>();
areas.Add(new Area(1, "Continent", -1));
areas.Add(new Area(2, "Country", 1));
...
Easier to read...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
(Advanced) using LINQ...
PrintAreas( areas, parent, 1 );
static void PrintAreas( List<Area> areas, Area area, int indent ) {
Console.WriteLine( "".PadRight( indent++, '-' ) + area.Description );
areas.Where( a => a.ParentID == area.ID ).ToList()
.ForEach( each => PrintAreas( areas, each, indent ) );
}
|
|
|
|
|
Hallo I have this code
<form ng-submit="onSubmit()" class="ng-pristine ng-valid ng-valid-maxlength">
<input type="text" maxlength="100" class="searchboxfield ng-pristine ng-untouched ng-valid ng-valid-maxlength" name="searchboxfield" data-test="searchboxfield" ng-model="model.term" ng-change="model.submit=false" placeholder="Zoek op EAN, titel of referentiecode">
</form>
How can I click programaticliy on c#
|
|
|
|
|
You can't programmatically click buttons from server-code, you'll have to do it from javascript\jquery.
|
|
|
|
|
I'm trying to uppercase the second word in a string of two words (a first and last name).
int index;
string name = "Jeffrey steinberg";
index = name.IndexOf(' ');
name = name[index + 1].ToUpper();
The toupper function isn't working though. How would I uppercase just the s in steinberg?
|
|
|
|
|
Here's the method I use:
private void butLowerCase_Click(object sender, EventArgs e)
{
bool sentenceCase = (ModifierKeys & Keys.Control) != Keys.Control;
if (Clipboard.ContainsText())
{
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
StringBuilder sb = new StringBuilder();
string EOL = "";
for (int i = 0; i < lines.Length; i++)
{
sb.Append(EOL);
if (lines[i].Length > 1)
{
if (sentenceCase)
{
sb.Append(lines[i].Substring(0, 1).ToUpper());
sb.Append(lines[i].Substring(1).ToLower());
}
else
{
string[] words = lines[i].ToLower().Split(' ');
string sep = "";
foreach (string word in words)
{
if (word.Length > 0)
{
if (conjunctions.Contains(word))
{
sb.AppendFormat("{0}{1}", sep, word);
}
else
{
sb.AppendFormat("{0}{1}{2}", sep, word.Substring(0, 1).ToUpper(), word.Length > 1 ? word.Substring(1).ToLower() : "");
}
sep = " ";
}
}
}
}
else
{
sb.Append(lines[i]);
}
EOL = "\n";
}
Clipboard.SetText(sb.ToString());
}
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I also have something similar, except along with conjunctions, I skip articles and prepositions as well.
You have just been Sharapova'd.
|
|
|
|
|
Console.WriteLine( System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase( "Jeffrey steinberg" ) );
|
|
|
|
|
|
I am just learning C# and would like code to loop all pages in a Microsoft Word 2010 document and find if a string exists in the page header, body, and footer and print the results to a text file.
Example: Find if a certain date is in the header, body, and footer of each page of the Microsoft Word 2010 document. If the date is not found on page 5, then print date is not in the header of page 5.
|
|
|
|
|