Set Item Focus in Listview Wpf

C# using others code

Add the library to your solution

Copy the IntervalTreeLib directory into your solution directory. Then, right-click your solution, and add existing project. Point it at IntervalTreeLib.csproj in IntervalTreeLib, and click Open. That should add the IntervalTreeLib project to your solution.

Add Existing Project Screenshot

Add a reference to the library in your project

Then, in your project, add a reference to the IntervalTreeLib proejct:
- Right click the References folder, and Add Reference. Click the Projects tab, and select IntervalTreeLib.

Add Reference Screenshot

Select Project Reference Screenshot

Use the classes in your code

To use classes from the library in your source then, you need to either add:

using IntervalTreeLib;

void Foo() {
IntervalTree<int, int> tree = new ...
}

Or, refer to them by their full name:

IntervalTreeLib.IntervalTree<int, int> tree = new ...

Taking over someone else's code

Usually the best way is to start working on the code fixing small bugs. The more true time you spend with it is the only way to learn the code base. There is no magic way to learn a code base. It will take weeks, months or possibly years depending on the complexity. However for most generic business systems a ramp up time is about 6 months of code knowledge and 6 months separate of industry knowledge to truly understand it all.

How do you add someone else's project to your current project?

I would recommend that for third-party code, you do not integrate their source code directly into your own project, but an assembly reference to the compiled foreign code. This makes it easier to separate your code from third-party code, and update the third-party code later on if a new version becomes available.

So this is roughly what you do.

  1. First, create an assembly from the third-party code (the color picker source code):

    1. Download the source code.
    2. Open the Visual Studio solution (.sln) hopefully present in the download.
    3. Create a Release build. (This might require you first changing the build configuration to Release via the Build menu in Visual Studio.)
    4. After a successful build, there should now be the color picker DLL in the bin\Release folder.

 
2. Next, add the created assembly (.dll) into your own project and reference it:

  1. Put a copy of that DLL into your own solution/project's directory.
  2. In your own project, add an assembly reference to that assembly (via the Solution Explorer window's References node's context menu).

What does using System mean in C#?

The using System line means that you are using the System library in your project. Which gives you some useful classes like Console or functions/methods like WriteLine.

The namespace ProjectName is something that identifies and encapsulates your code within that namespace. It's like packages in Java. This is handy for organizing your codes.

class Program is the name of your entry-point class. Unlike Java, which requires you to name it Main, you can name it whatever in C#.

And the static void Main(string[] args) is the entry-point method of your program. This method gets called before anything in your program.

And you can actually write a program without some of them in DotNet 5 and later as they now support top-level functions.

How to make the program go back to specific line of code C#

You need to put your querying in a loop that you only leave when the correct choice is made. Because you're using a switch, which uses the keyword break after every case, and I'm advocating a loop, which uses break to leave, we need to structure things slightly differently because we can't get out of the loop by issuing a break inside a switch case

while(true){ //loop forever unless we break
Console.Write("Enter a number (1-3): ");
string keyChoice = Console.ReadLine();

//Respone to the preferred key choice
switch (keyChoice)
{
case "1":
Console.WriteLine(" You fumble ...");
break; //break out of the switch but not the loop

case "2":
Console.WriteLine(" You choose ...");
continue; //restart the loop

case "3":
Console.WriteLine(" You choose ...");
continue; //restart the loop

default:
Console.WriteLine(" That isn't a valid key number, enter 1, 2 or 3");
continue; //restart the loop
}
break; //break out of the loop
}

I've modified your switch to have a default case; before your code would not have been able to handle the user entering garbage


We can also control a loop with a variable that we set when we want to stop looping:

    bool keepLooping = true;
while(keepLooping){
Console.Write("Enter a number (1-3): ");
string keyChoice = Console.ReadLine();

switch (keyChoice)
{
case "1":
Console.WriteLine(" You fumble ...");
keepLooping = false; //stop the loop from running next time
break;

case "2":
Console.WriteLine(" You choose ...");
break;

case "3":
Console.WriteLine(" You choose ...");
break;

default:
...
}
}

Or you can ditch the switch/case and use if and break to exit the loop:

while(true){ 
Console.Write("Enter a number (1-3): ");
string keyChoice = Console.ReadLine();

if(keyChoice == "1"){
Console.WriteLine(" You fumble ...");
break; //exit the loop
} else
Console.WriteLine(" You choose ...");
}

This just issues the "wrong key" message if the user enters either the wrong key, or garbage.


Try not to see your code as "go to point X if" but more like "repeat this section of code while some condition isn't met" - it's a subtle difference, but one that will encourage you to think about the looping you need to make


ps; your life will get somewhat simpler if you make a method that asks questions and gives the response back:

public static string Ask(string question){
Console.WriteLine(question + " ");
return Console.ReadLine();
}

Use it like:

string keyChoice = Ask("Enter a key 1-3:");

We can improve things to prevent users entering garbage:

public static int AskNumber(string question, int lower, int upper){
Console.WriteLine(question + " ");

int result; //variable for the result
bool isNumber = int.TryParse(Console.ReadLine(), out result); //try turning the string into a number

//while not a number was entered or number was out of range
while(!isNumber || result < lower || result > upper) {
//repeat the question
Console.WriteLine(question + " ");

//try parse their input again
isNumber = int.TryParse(Console.ReadLine(), out result);
}
return result;
}

This is another example of code that is "loop until a desirable condition is met" - the desirable condition being the user enters a valid input

Use it like:

int keyChoice = AskNumber("Which key? Enter 1, 2 or 3", 1, 3);

You can be sure the response will be 1, 2 or 3 so you don't have to handle garbage in each switch etc

Use existing source code in another project/namespace

Declare the SharedClass in a common namespace, instead of two different namespaces. You could link the file into the projects instead of including it physically.

From msdn:

You link to a file from your project in Visual Studio. In Solution Explorer, right-click your project, and then select Add Existing item Or, you can type Shift+Alt+A. In the Add Existing Item dialog box, select the file you want to add, and in the Add drop-down list, click Add As Link.

namespace Khargoosh.MathLib.Common   { public class SharedClass { ... }  }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }

or

namespace Khargoosh.MathLib          { public class SharedClass { ... }  }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }

A completely other way of handling that would be to use the T4 template with some logic to create the file dynamically. Content of the *.tt template files (not *.cs files!):

namespace library1
{
<#@ include file="MyCommonClass.cs"#>
}

And in the other library

namespace library2
{
<#@ include file="MyCommonClass.cs"#>
}

The class file itself would not declare a namespace.

Figuring out the right language for the job: branching out from C#

Python/Perl/Ruby/PowerShell are great supplements to C#/VB.NET. If your boss hands you a text file and says insert it into the database once or twice, then any of Perl/Python/Ruby (I'm not sure about powershell but I imagine it is not that much more difficult) should be fine to parse it. Either way, for your main applications you will probably be stuck in C#. You can use one of the more dynamic languages to do code generation in C#.

Since you are in a Microsoft Environment, probably your best chance at getting your solution accepted is PowerShell. Next to that I'd say IronPython or something else that integrates with the CLR. But main issue is that for someone else to maintain what you do, they would have to know whatever language you are using. MS in the future has plans to use PowerShell a lot more, so it is probably easier to justify PowerShell then say Python/Perl/Ruby.

If you are just processing a text file for one time use. Or creating a one time code generator to generate all the code and then intend to maintain the generated code, then it doesn't matter. You are the one who will consume the results and if you save time using Perl then more power to you. But if you are doing something that will be used over and over again (like an active code generator where you change the templates and run the generator instead of maintaining the generated code) then other developers working on what you did will need to know the language you used. It is much harder to argue learning Perl/Ruby/Python in a Microsoft Shop. But PowerShell seems like the easier argument. I think the MS grand plan is that eventually applications will expose more functionality for power shell through commandlets. Assuming this happens then PowerShell is even more of a no-brainer because it will expose tons of scriptable functionality that you won't get any other way.

How to use class from other files in C# with visual studio?

According to your explanation you haven't included your Class2.cs in your project. You have just created the required Class file but haven't included that in the project.

The Class2.cs was created with [File] -> [New] -> [File] -> [C# class] and saved in the same folder where program.cs lives.

Do the following to overcome this,

Simply Right click on your project then -> [Add] - > [Existing Item...] : Select Class2.cs and press OK

Problem should be solved now.

Furthermore, when adding new classes use this procedure,

Right click on project -> [Add] -> Select Required Item (ex - A class, Form etc.)

Make reference to C# code from multiple projects

Create a Class Library, add the file, build the project, and reference the DLL created from the build. Add the using statement to each file that will reference it. Also if it errors and the DLL is in the Project you and Right Click on the object -> Resolve and it will add the using for you.

Is there ever a reason to use goto in modern .NET code?

Reflector is not perfect. The actual code of this method is available from the Reference Source. It is located in ndp\fx\src\xsp\system\web\security\admembershipprovider.cs:

        if( passwordStrengthRegularExpression != null )
{
passwordStrengthRegularExpression = passwordStrengthRegularExpression.Trim();
if( passwordStrengthRegularExpression.Length != 0 )
{
try
{
Regex regex = new Regex( passwordStrengthRegularExpression );
}
catch( ArgumentException e )
{
throw new ProviderException( e.Message, e );
}
}
}
else
{
passwordStrengthRegularExpression = string.Empty;
}

Note how it failed to detect the last else clause and compensated for it with a goto. It is almost certainly tripped-up by the try/catch blocks inside the if() statements.

Clearly you'll want to favor the actual source code instead of the decompiled version. The comments in themselves are quite helpful and you can count on the source being accurate. Well, mostly accurate, there's some minor damage from a buggy post-processing tool that removed the names of the Microsoft programmers. Identifiers are sometimes replaced by dashes and the code is repeated twice. You can download the source here.



Related Topics



Leave a reply



Submit