C# visual Basics references not available - c#

I can't even get off the ground.I DO KNOW HOW TO ADD REFERENCES, (this."""mshtml""" NOT[working])
using System;
using system.text;
using mshtml; // i cant seem to access this
I'm confused on why when I type mshtml it's in capitals, MSHTML. The problem I'm having is I'm just using a variety of tutorials but can't seem to even start them as this is my first problem.
I have manually browsed when adding references to my application but it doesn't seem to work. I keep getting the capital letter version, except there is no capital letter version of MSHTML.dll in my directories on my PC. If for some reason I don't have this file can it been downloaded for free safely or are my tutorials just too old?
Also just to let you know I understand where these files are meant be, in regards to folders locations ect. I thought if I just add the reference in to my project it should just be there.

Thank you for pointing out that this is your first C# program. It helps us understand the level at which to try to answer.
First, I'd like to address a statement you made:
there is no Capital letter version of MSHTML.dll in my directories
It seems that you're confusing Namespaces with .dll names, which is something I struggled with myself at first. There's a full explanation here, but it may be too technical for beginner level developers.
In a nutshell, at the top of the file where your "using" statements are, you're telling the compiler where to look for certain classes and code by Namespace.
A Namespace is a logical grouping of code. For convenience and clarity, developers group similarly functioning code into Namespaces. For example, Data Access code is in the System.Data Namespace.
When adding a .dll you're adding an actual file reference. In a less-confusing world, .dlls would be named to reflect the Namespaces contained within them. However, it's not always that simple. It' is perfectly possible for me to create a dll named "DaveStratton.dll" that contains Booyah.Encryption, Simple.Functions or any other Namespace I want. There really is no correlation except by convention, and it's not enforced."
For example, if you look in the MSDN Library at the System.Data.SqlConnection class.
The Class name is actually SqlConnection, and it lives in the System.Data Namespace. The System.Data Namespace is contained in the System.Data.dll. (because the developers were following convention and did it this way for clarity.) Screenshot below:
If you look at other classes, you may find discrepancies.
For example, the System.Configuration.SettingsBase class: The SettingsBase class in in the System.Configuration namespace, but if you look at the assembly information, you'll see that it's in System.dll. And the System.Configuration.ConfigurationManager is in the System.Configuration.dll.
So, long story short, you need to know the Assembly (.dll) name when adding a reference in Visual Studio, but you need the class/assembly name when writing your code. In your using statement, you need capital letters because the Namespace is capitalized, ewven if the .dll isn't.

I think you have a misunderstanding by your terms 'capital letter version'.
The name of a dynamic link library, although usually indicative of its purpose and aptly named, isn't strictly tied to the contents of the assembly. You can call it what you want (within reason and limits of the system) and the code inside remains the same.
If you've added a reference to a 'MSHTML.dll' file, then it is very probable that a namespace of 'mshtml' is defined - I didn't say ideal, but probable. Casing within the code does matter though - so if, for instance, Intellisense is showing you a case-variant version of what you're typing then it's probably that. Either way, it won't be what you've been typing if you ignore it.
If you could reference the tutorials you were following then it would likely be easier to see what the actual problem was. Other than that, go ahead with the code that it provides.
As an aside, I'd recommend some reading up on Assemblies in .NET.

it sounds like you need to add a reference to your project first. Open up your solution, in the right project explorer, expand references, right click references to add a reference. Now find the Microsoft.mshtml and add that as a reference.

See here for your using problem, you need to add a reference (right click project, add reference) to this particular COM lib 1: How do I use MSHTML in VB.NET?

Related

Creating Instance of an unknown Class from a non-GAC Assembly

Sounds like a dumb question I know, but bear with me :-) I'm currently creating a PoSh module which contains a few custom commands. I had already written a PoSh Advanced Module previously to do what I want to do, but I've decided it's time to take the plunge and learn C# !
One of my commands needs to create an instance of a class which is contained in a third party SDK assembly. That assembly is not contained in the GAC. In my PoSh Advanced Function previously, I would query a registry key to confirm that the SDK was installed (and get the path to it), then I would use System.Reflection.Assembly.LoadFile to load the assembly.
In my C# version, my plan was to do something similar. I've managed to query the registry, confirm that the assembly exists etc and even load it. However, because the assembly isn't referenced in Visual Studio, it just throws loads of intellisense errors when I try to instantiate a class from that assembly. I initially suspected I might need to use something from the Activator class to get around this, but I've been through all the methods there and couldn't find anything that might help.
After a bit more pondering, I wondered if perhaps my approach is wrong, and maybe I shouldn't be doing the "manual" loading but instead allow .net handle all that for me, eg by adding a reference to the assembly. In that case however, how do I reference an assembly in VS without knowing where (or even if) it will be installed on the target/invoking machine ?
Or, if my original approach is correct, how do instantiate the class "manually" (or otherwise) without VS being so unhappy. I did consider adding a "temporary" reference to the assembly on my machine, but I think I'd have to remove that again before doing the retail build. And I'd also have to add temporary using directives I guess.
I have googled this quite a bit, but haven't found anything that might help me at all. So I'd really appreciate any guidance anybody can provide. Maybe I should be looking at something else entirely, like App Domains ?
Thanks in advance
After loading assembly use CreateInstance method and store result in dynamic type variable:
dynamic test = assembly.CreateInstance("Full.Type.Name");
You'll not get intellisense, but compiler will assume that this variable supports any operation. Beware, invalid operations will result in errors only at runtime.

Two namespaces of the same class

we are changing the name of our product, so i also want to rename the namespaces of our
framework-classes. But now i have the problem, that i don't know in which programms and scripts our namespaces are used. Is there a way in c#, to locate the same class in two different namespaces?
I know the solution, that i could inherited from my classes in the new namespace, but this is a very bad solution i think. So I have no idea how to solve this problem, because simply renaming all namespaces doesn't help and will cause a lot trouble.
Thank you!
If external scripts are referencing your assembly using the old namespace names then those names will have to remain in your assembly in you wish to continue to use those scripts. If you also want to create new namespace names to reflect the new name of your product, those names will also need to be hardcoded into your assembly. This will inevitably lead to problems!
I would recommend one of the following:
Leave the namespace names as they are.
Rename the namespaces in full and update the Python scripts at the same time.
I would definitely not recommend the faux 'inheritance' method, or any other solution which results in duplication within the assembly.
You could search the whole project / solution of course, but that seems sort of messy and time-consuming too, if you've got more than a trivial project.
Are you using Resharper? For this type of task, you definitely should be. If so, there is a chance this could at least help you on your way:
Rename the folders your source files are in in the Visual Studio Solution Explorer (this should in theory be easier than looking at each source file one by one, right?).
Now open one source file that you know will have the wrong namespace due to a renamed folder. It should appear with a blue squiggly line, as in the picture below.
Use the Resharper tip (pyramid to the left, or Alt + Enter) to open the context meny thingy also shown below.
Select Find all issues of this type in scope, and select Solution as your scope. That might at least help you get an overview of which classes you need to change the namespaces for, and go through them and change them systematically.
As for your scripts, I would guess that you best bet is to do a plain text search for the old namespaces - possibly a search and replace. Perhaps you can include your scripts in a VS solution, and use the built in search there to scan and fix them. That might at least ease the pain a little..

Could I create a global namespace hierarchy in C++ that is similar to C#'s to assist developers using our code?

Is it possible to create a namespace hierarchy in C++ that resembles how it works in C#. For instance if I were to need a type to deal colors within C#, I could dive down through the namespaces to get the appropriate type by using the:
System.drawing.color;
type. In this case, it appears the namespaces are resolved at a project level and determined by the namespaces that the type is contained within. For the developer that this code targets, they get all of this in their auto-complete and aren't left searching through folders and files for that type. In C++ I might have to first include the appropriate header file, at which point I feel like we've already gone through the trouble of grepping source code for the appropriate types and finding which file includes those types. In C++ it would look like:
#include "Core/Color.h"
Color c = new Color();
The IDE offers me little help here. I have to do a directory search for the correct type. The directory paradigm seems to break down unless everyone specifically uses the right filenames and directory structure, which isn't always the case.
To fix this, it looks like not only would I have to come up with a namespace hierarchy for all of my types, which isn't such a large problem, but I'd have to also come up with a header hierarchy to eliminate the problem of constantly grepping the code to find the correct files that include those types.
Does a master header hierarchy present a problem for the compiler, preprocessor, or resulting compiled code since we'd essentially have every other header up the chain (up to a point of course) included in new files?
In the end I want a way to assist the developers who use this code by giving their IDEs a way to dive down to all the types without having to do all of the grepping that we currently have to do. There may be a way to quickly do this within IDEs already, at which point I wouldn't need to utilize the language itself to solve this sort of development problem, however I can't seem to find it.
See the following SO discussion and how this was handled by one of the SO users
C++ namespaces advice
http://www.adamjamesnaylor.com/2012/10/23/NestedNamespacesInC.aspx

C# using every namespace in the project

In each my .cs file I have tons of "using" lines including other namespaces, like that:
using Game.Models.Main;
using Game.Models.DataStore;
using Game.Manager.MapManager;
using Game.Network.Player;
What if in the beginning of every new .cs file I include "using" statements for all existing namespaces in my code? Are there any downsides of that? Like performance or anything I can't think of?
Why I want that: in Eclipse you can press Ctrl+Space and you'll see the list of definitions starting with the typed characters. In Visual Studio however, you can see that only if the class you're typing is among the "using" namespaces. So when you write a new class you have to type every other class name in full.
It would probably slow down visual studio's intellisense, but as for a compiled product, it would not affect the resulting binary by having superfluous using statements.
If you want to remove superfluous using statements then you can right-click on the using statements, and go to "refactor" > "sort and remove"
There is not much downside except if you ever have classes with the same name in different namespaces (like Color in System.Drawing.Color and System.Windows.Media.Color) than adding all namespaces can either cause compile errors or complete change of behavior of the code.
Normally you only want to include the namespaces you are using. This way you can avoid naming collisions and programmer confusion.
As far as IntelliSense is concerned, if you want something from another name space just start typing in the namespace, IntelliSense will list the namespaces and so you can drill down to what you are looking for.

Where or how I can get the full list of C# reserved words/methods/namespaces/... for autocomplete

I'm doing an autocomplete editor for C# language, and need to get all the words/methods/namespaces/proprieties in C#.
Didn't found anything useful in google.
Also tried with reflection but can't get all items like namespaces after System or other namespaces.
Is there a dictionary with all this on internet, or is there a method to do it with reflection?
for exemple:
User is typing System.
The autocomplete found the System as a namespace and showing all the types/methods and namespaces inside it.
or user is typing Bitmap (if I will not find the Bitmap as a root type, then I will try all the combinations of the using XXX.YYY, like XXX.YYY.Bitmap...)
Thanks
P.S. Please don't recommend me MSDN because I already know about it and this will be the last and worst option, to parse recursively all information on MSDN and save it in a database.
As per #Steve Wellens' comment, there is difference between C# and .NET type names. You have two very different problems to deal with:-
Gaining knowledge of C# - will allow your editor to know about C# keywords, etc. This can be found in the C# language spec, as per #Cody Gray's answer. This does not vary according the context of the particular file you are editing (unless you want your editor to have the option to be able to restrict to older version of C# in which case you will need to build in knowledge of previous versions of the spec).
Gaining knowledge of the types available in the current editing context. For this, you need to know which namespaces have been declared in using statements in the current file and which libraries have been referenced by the project containing the current file. There is no point trying to find out all this information globally for every single library available since the amount of information will be too huge and continuously changing. You could, perhaps, build in knowledge of all type names available in the GAC. In the case of a partial typename, e.g. Bitmap, a simple implementation would use the using statements contained in the file to attempt to determine which type name is being referred to by examining the relevant assemblies referenced by the project containing the current file (conflicts can occur and will need user resolution, e.g. prefixing the partial type name with some more elements of the actual namespace). This is how the Visual Studio editor works. A richer implementation can examine all assemblies referenced by the project containing the current file plus all those contained in the GAC and, if required, suggest either addition of the full name space to the type name or the addition of a using statement. This is how Resharper works.
Did you try the MSDN documentation, for both the .NET Framework and the C# language? This is the closest you'll come to a "directory with all this on [the] internet".
You might also peruse the C# language spec.

Categories