|
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.
|
|
|
|
|
What have you tried?
/ravi
|
|
|
|
|
I found this code for a word count, but I want to loop pages and search the header, body, and footer.
using System;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("C:\\word.doc");
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
Console.WriteLine("Word {0} = {1}", i, text);
}
// Close word.
application.Quit();
}
}
|
|
|
|
|
|
Hi I'm new to programming and I'm still confused most of the times to figure out the logic behind those problems that I'm working with. I feel like I'm so stupid. I wonder how other programmers come up to those kinds of solutions. Please give me some tips on how to improve my programming skills.
|
|
|
|
|
Everyone here was once a "beginner;" don't feel alone
Suggestions:
1. decide on an initial language to learn: I recommend C# used in the context of the .NET framework.
2. get a good book on the language you've chosen to learn, and get a programming environment (IDE) set up where you can start experimenting with the language. I recommend using the free editions of Visual Studio for your IDE. For a book: how about Charles Petzold's ".NET Book Zero," which is free: [^].
3. Start studying, start experimenting, download the code samples (if available) in the book you are using, run them, and experiment with them.
4. very important: learn how to debug ... to set break-points, to observe the value of variables, and the "state" of "objects."
5. get so busy learning, experimenting, and analyzing the results of your experiments, that you never have time to think about whether or not you are a "beginner," or, if you "understand programming."
cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
It's not too bad, once you get your head around the process.
Start by breaking the task into smaller chunks - for example, it your have to get two values from the user, add them together and print the result (and yes, I know it's trivial) then you would brak it into three bits:
1) Get values
2) Add
3) Print Then look at the first one "Get values" and break that into smaller bits:
1) Get values
1.1) Get a value
1.2) Store it somewhere
1.3) Get a value
1.4) Store it somewhere else
2) Add
3) Print And you start to see that 1.1 and 1.3 are the same job - so they can be done with the same code. You then continue this until each of the tasks you have is pretty simple - and then implement them!
There is a technical term for this - reductionism - and it's a process we use all the time in real life without really realising it: "Make tea" for example reduces to "boil water", "brew tea", "add milk and sugar", "combine" - and each of those breaks down into simpler actions we take without thinking.
Another way to get started is to do the job manually, and think about the processes you are involved in to do it. This can give you a good idea of the overview, and you can then start reductionism to separate out the simpler tasks.
It's a skill - and like all skills it only comes with practice - so try it. You'll get it wrong (we all did, and we all do) so be prepared to throw it away and start again, but you'll get better as you go along.
Good luck!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
These days, the main emphasis seems to be on the "newer" analysis and design tools: state diagrams; sequence diagrams; activity diagrams; etc.
I don't think any of these tools is a substitute for a plain "old" flowchart and / or decision tree when it comes to a particular piece of non-trivial logic.
A simple flowchart can help one to better understand a particular process (and how to program it).
If you're not familiar with flowcharts or decision trees (or some of the other "older" tools), you should take the time to learn about them if you are serious about programming. Even the "old" stuff can be useful in a particular situation.
|
|
|
|
|