I have a question about classes with including interfaces. I have about 12 classes with all the same interface in it. Now, I need to list all classes. In the feature there could be 1000 new classed to create. But because they are responsible for devices some times some classes are obsolete.
Ok. No problem to list all classes by using
dim ClassList as new List(Of MyInterface)
ClassList.Add(New MyFirstItem())
ClassList.Add(New MySecondItem())
ClassList.Add(New MyThirdItem())
ClassList.Add(New ...)
then
For each item as MyInterface in ClassList
'Print info
next
that what I am doing NOW.
I want to ask if it is possible tell visual studio or my application that I have one solution folder with all classes written in and to add automatically these items to the list. Or do it like plugins. I want to avoid the lines above. And I want to avoid creating a complete new project just for some lines of code in each class.
ClassList.Add(New ...) 'would like to avoid this to do manually
just to create an instance and dispose it immediately again for writing the class information on screen.
Hope I could explain my question
Regards
Assuming all of your classes are in their own files named their class name in the directory you could do a little app that would output your lines to a text file like this:
String[] Files= Directory.GetFiles("PATH", "*.cs");
foreach (String file in Files)
{
WriteLine("ClassList.Add(new " + file.Replace(".cs", "") + ");");
}
Related
Is it Possible to set Nodes in the Projectexplorer for each Partialclass in Visual Studio?
Example:
Creating a Class-File in Visual Studio with 2 Classes inside, leads to a node for every class.
Creating a Class-File in VS with a Partial class, doesn't result in a new Node for each Partial class.
Is it Possible to have somehting like MurderAllHumans.Constructors and a MurderAllHumans.Methods Node?
I know it is possible to Achieve something simmular with Folders and multiple .cs files, but i am looking for a way to do this in one file.
Thanks in advance
No, this is not possible. The Class view will only show one entry for each class and as two files with a partial class is still only one class, there will only be one entry.
Generally it is advised to have only one class per file, and I am not sure I understand why you want to declare the same class partially twice in the same file either.
Maybe you could solve part of your wish but making one region for constructors and another for the methods etc. like this:
This will however not be reflected in the Class View.
I've got a small problem (maybe it isnt even a problem)
I am making an application in Ruby, and the folder/file structure goes something like this:
OrderSet/
..item.rb
..file.rb
..order_object.rb
OrderGet/
..item.rb
..files.rb
..order.rb
As you can see I got two item.rb files, they are both different in class structure. Now I need to create an OrderSet/item.rb object, how do I specify it needs to look in OrderSet and does not get the OrderGet one?
I have to make clear, all files are required in the main rb file.
I have tried doing OrderSet.Item (the class is called Item inside the item.rb) but it complains about an ininitialized constant OrderSet
Thanks in advance!
[edit]
I have also tried to make modules out of it, maybe I don't understand the concept correctly, but I have tried it with OrderSet.Item.new (OrderSet as module name)
You could use a module to create a namespace - that way each set of classes would be encapsulated to what they do (the folder name from your example). So classes in OrderSet would be wrapped in a module for example OrderSet/item.rb would become:
module OrderSet
class Item
# methods and properties
end
end
Then you could use it like
new_order_set = OrderSet::Item.new
RubyMonk has a lesson called Modules As Namespaces which has more details and examples you can run in your browser.
Suppose I'm following TDD and using VS/ReSharper. I start out writing a test like this:
[TestFixture]
class FooFixture
{
[Test]
public void ShouldDoSomething()
{
var foo = new Foo();
}
}
At this point, Foo doesn't exist. ReSharper suggests the option of creating the class, but it puts it right next to my test class in my test project, not in my real project. So I have it create the class, then move it to a new file, then move the file to the right place, and finally fix the namespace. This seems like a lot of work.
Is there a more efficient way to quickly create the Foo class and put it in the right place? It seems like the 'right place' could be guessed from the namespace of my test project.
Move types into matching class refactoring is used for this purpose.
You're expected to generate a number of business logic types in the current test class and then move them to matching files/namespaces in one go.
Note that this refactoring is available in the text editor and on Solution Explorer nodes, meaning that you can batch-apply it to a heck lot of files.
Help! My fingers are falling off from typing so much.
I have a lot of objects that have sane names to them. The Database names are not so sane, and i'm stuck defining my property names in all my projections.
For example:
from f in foo select new MyClass() {MyID = f.ID, MyName = f.f, MyTime = f.t}
Etc.. now, multiply this by hundreds or even thousands of business object methods that materialize data into various classes with mismatched field names and and dozens of properties in most fields andit's a lot of typing.
So, i'm wondering if there is any way (maybe via Attributes or something else) that allows you to define a default mapping for the class so that even if the fields mismatch I can simply say:
from f in foo select new MyClass()
Any solutions? Or am I stuck typing my fingers off?
EDIT:
Upon further reflection (pun semi-intended), I realize that this is precisely what L2S is for, and I can rename the fields in the L2S Data classes to whatever I need.
Sometimes the easiest answers are right in front of us.
Well, one obvious option is to go to the DBML designer and change the names of the properties in the generated classes. They don't have to be the same as the ones in the database.
Just go into the designer, click on a property and change the Name part. (The Source property is the database column name.) Rebuild the project, and the names will have changed.
Alternatively, if you're always transforming from one source into the same type, create a method in a partial class for the source data type which transforms into the target one - or vice versa. So you could write a Foo.ToMyClass() method, or MyClass.FromFoo(Foo foo).
Just one other suggestion - AutoMapper would allow you to register mappings once and then just call a shared method to map from one object to another
I hope this is not something very trivial and obvious.
I have some similar programs that I am working at. In each program I have to implement future about saving projects. I came with following design :
Project
-- Program1Project
-- Program2Project
The base class Project :
class Project
{
public:
void NewProject();
void SaveProejct();
void OpenProject();
protected:
virtual void New();
virtual void Save();
virtual void Open();
};
The virtual functions are reimplemented in the derived classes cause only the specific program knows how ( which objects to save to disk ) to actually save the project.
Also part of saving new or opening a project is showing the SaveAs/Open dialog from which the user will select where to save/open the project.
For example, the NewProject() is implemented in terms of the New method:
void Project::NewProject()
{
1. // Show dialog for whether to save existing project
2. // check whether the project was already saved
3. // if yes, only overwrite the existing project
4. // if no, show SaveAs Dialog
5. // ...
6. this->New();
}
Line 1 to 5 is code that all of my programs need, i.e the flow and order in which the dialogs are created and checks are performed is the same.
I was thinking whether the actual code that creates the dialogs should be placed in the Project::New and Project::Open methods. After some thinking I decide that it is not good solution cause the Project class is model class, and model class should not create GUI. So, I was thinking maybe the best place to write the code from line 1 to line 5 , is in the Save/Open buttons event handlers of the specific program. But that means that I will have to duplicate it for each program .
So the question is how should I separate the creating of dialogs which will be the same for all of my programs from the actual saving/opening of the projects in way that do not require duplicating of code ?
The GUI should refer to the model, not the model to the GUI, and that pretty much keeps the ties of the model free of the GUI. There's really no way to keep the GUI totally free of the model. At some point, you're going to have some sort of depedency, even if the implementation is hidden away from the GUI.
You could create a seperate class that acts as a controller between the model and the views. The controller could be a membe of the project for example and on line 1 you could call this->viewController->showDialog(callBackForYes, callBackForNo, callBackForCancel)
The viewController class could directly echo out the gui, or use view classes for different gui components.