|
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.
|
|
|
|
|
Hi Gary,
I find your mention of "decision trees" very interesting; I have always wondered why they seem to have been "left-behind" in the last years.
I've been implementing my own decision-tree control in C#/.NET and intent to publish it here when it's "ripe."
An intellectual problem I see in implementing useful decision trees is to figure out a way to analyze the possibly numerous criteria so that you select-out the variables whose state-change has the maximum predictive power for the outcomes.
Imagine an air-fare discount decision-tree with some thirty possible variables, each having #n possible states: analysis of the possible decision matrix might lead to concluding that the most "powerful" predictive variable was the flyer's "age."
How to create an interface that assists "mere mortals" to write efficient decision-tables that are "pruned" so that the minimal effective set of rules/actions are used. That's the interesting problem, to me.
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.
|
|
|
|
|
the problem is only a problem as loong as u dont have the solution yet;)
"Divide and conquer". Break Down the problem so much Down that u only solve 1 problem at the time.
If u have to do that solution many times, look for "loops".
But i think that every programmer has his weak points.
But u will get stronger and stronger to see what to do.
Using a editor will give u some sort of feedback, to what u are missing.
And mistacks will be made , i dont think i ever 1 shottet a solution. Allways something Little thing that i overlook.
I my self find web programming slow, so slow that i have to only do 2-3 linie before i debug. Mayby because of ininspirence but havnt find a editor for it. It just Work or it dosnt.
And it is easy just to look and twist a few linies to see the result, than to look at 15 linie, and no where to start.
So just keep learning.
It is mostly just matematik and logik.
And speeking of logic, i still get - less than and greater than (<>)the wrong way.
Best regards
jan
|
|
|
|
|
Hello,I understand Shell Sort definition but I can not understand
this C# code:
public void shellSort()
{
int inner, outer;
long temp;
int h = 1;
while(h <= nElems/3)
h = h*3 + 1;
while(h>0)
{
for(outer=h; outer<nElems; outer++)
{
temp = theArray[outer];
inner = outer;
while(inner > h-1 && theArray[inner-h] >= temp)
{
theArray[inner] = theArray[inner-h];
inner -= h;
}
theArray[inner] = temp;
}
h = (h-1) / 3;
}
}
please help me thx!
modified 2-Oct-15 4:39am.
|
|
|
|
|