I am not a seasoned pro at C# and am running into an error I cannot find a discrete solution to. Currently I have one class that is part of my namespace
namespace BlackBoxStudio
{
public partial class Project : Form
{
This Project class is what implements the various functions of my form. What I am trying to do is make a label.text field accessable to another .cs class within the same namespace. As so, I have made the corresponding fields public:
public string lb_InformationText
{
get { return lb_Information.Text; }
set { lb_Information.Text = value; }
}
To call these I get or set methods in my RsaFunctions class I make a new private project as so:
namespace BlackBoxStudio
{
class RsaFunctions
{
//private Project proj = new Project();
However the line that is commented our causes my vshost32.exe application to fail and the program dies. Why is this causing the vshost32.exe to fail when I un-comment the corresponding line? Or how could I better structure my classes so this does not happen? Any tips would be much appreciated about my strategy for making new classes and such.
Related
I am making an AI in WPF and i want to use a separate C# file for the AI. When I type start(); in one file I want it to find it from the other file and use it, and when I type myImage.visibilty = visibility.hidden so that it will hide the image from the xaml from the original c# file.
Here is my second C# file
using System.Windows;
namespace Hexapawn
{
public class AI2 : MainWindow
{
public AI2()
{
InitializeComponent();
//somecode
}
public void start()
{
//somecode
}
}
}
I have tried
using myproject.Myfile;,
using myFile;
but it isn't able to use methods from the other file or change the xaml.
How about a partial class. Your main window class is already a partial class. You can declare 1 more partial class of the same class in a separate file and use all the methods from it in your first class.
However, your AI2 class seems more like a Model to me which will deal with non-UI business logic. You can implement the MVVM pattern which is more suited for WPF-based applications and make your new AI2 class as the Model of MVVM.
I'm currently going through this tutorial:
https://tomassetti.me/getting-started-with-antlr-in-csharp/#note1
But I've issues at the point where I want to create the SpeakVisitor class, in the tutorial the class is inheriting from a class called SpeakBaseVisitor which was generated by ANTLR itself.
So I looked through my whole solution and I couldn't find this created class...
All I got are two pretty much empty class files called SpeakLexer and SpeakParser which got created after I selected Combined Grammar in the "Add → New Item" dialog.
SpeakLexer.cs
namespace _006_ANTLR
{
partial class SpeakLexer
{
}
}
SpeakParser.cs
namespace _006_ANTLR
{
partial class SpeakParser
{
}
}
Do you got any idea why there is no SpeakBaseVisitor automatically generated in my case?
Greetings! :-)
This question already has answers here:
Using a class file/reference it?
(4 answers)
Closed 6 years ago.
I am creating a WPF and I have also created a side menu for different processes I want to perform. Currently all my code resides in the mainwindoe.xaml.cs. I would like to break out my into seperate files. For example menuitem1 code in one file, menuitem2 code in another file, etc. I prefer this method as I feel it is cleaner and easier to maintain. However I have tried doing Project-->Add Page-->Class but I don't know how to reference the code in the new page. Any help would be greatly appreciated.
Thank you,
Kent
Well, in your class file you have the following:
namespace myNamespace
{
public class MyClass
{
public void MyMethod() { }
}
}
Let's assume that you have this in an assembly named MyDll.dll. You'd use it as follows:
You add a reference to MyDll.dll within the solution explorer
You include the namespace with using myNamespace;
Then you can use your class doing MyClass test = new MyClass();
If you don't add the namespace like Number 2., you'd use your class like:
myNamespace.MyClass test = new myNamespace.MyClass();
You can to put all files in the same or a abstraced namespace. And You have to work with classes in c#.
For example
yourapp.mainwindoe
yourapp.menuitem1
yourapp.menuitem2
Addionally You have to set the classes You Need to Access from another Namespace to at least internal security Setting.
namespace yourapp.mainwindoe
{
class YourClass
{
internal static YourMethod()
{
yourapp.menuitem1.YourOtherMethod();
}
}
namespace yourapp.menuitem1
{
class YourClassOther
{
internal static YourOtherMethod()
{
// do something here...
}
}
VS2012 for desktop .net framework 4.5 normal windows forms applications, not WPF
Hello, I tried to search for an answer, but I'm not sure of the correct terminology. I've managed to break my code, and can't understand what I've done wrong. (i didn't think i had changed anything, but ...)
I have a solution which contains 2 projects. The first project is an executable program, and the second is a DLL, which is loaded at run time and used by the first project.
the first project contains a form, and a static class with public static strings in the same namespace. (and some other unconnected classes). specifically:
namespace project1_namespace
{
static class settings
{
public static string some_words = "some words in a string";
}
class dll_callback{
//.. some public methods here
}
dll_callback dllcallback; // instance is initialised in the code (not shown)
Form form;
public partial class frm_splash : Form
{
private void frm_splash_FormClosing(object sender, FormClosingEventArgs e)
{
// this function actually loads the DLL, ensuring its the last step
//... some error checking code removed for brevity
Assembly assembly = Assembly.LoadFrom("c:\dllpath\project2.dll");
Type type_init = assembly.GetType("project2_class");
object init = Activator.CreateInstance(type_init, form, dllcallback);
//... some error checking code removed for brevity
}// end method
}// end form class
}// end namespace
when the form is closing, the method shown above is called which calls the second projects class project2_class constructor.
in project 2, the DLL, there is:
namespace project2_namespace
{
// how did i get this working to reference "settings" class from project 1??
public class project2_class
{
public project2_class(project2_namespace.Form1 form_ref, object callback)
{
settings.some_words = "the words have changed";
//... some more stuff
}
}
}
Now, i was experimenting with some code in an entirely different part of project2, and VS2012 suddenly started refusing to compile stating:
error CS0103: The name 'settings' does not exist in the current context
the standard solution to this appears to be to add a reference to project2, but that would create circular dependencies because project 1 calls 2 as a DLL.
I really honestly don't think i had changed anything relevant to this, but also clearly I have.
looking at it, i cant see how project 2 would have access to a class in project 1 without a reference, but the list of arguments to the project2_class constructor doesn't include one, and I am absolutely positive that it hasn't changed (and I cant change it for backwards compatibility reasons).
would really appreciate help with this, as its been a lot of work to get this working.
as a side note, I've definitely learned my lesson about not using source control. and not making "how this works" comments instead of "what this does" comments.
may dynamic help you? You can not get the setting string at complie time.
I am going through some WPF example I found.
I have a class here which is inherited from Application:
public partial class DataBindingLabApp : Application
{
private ObservableCollection<AuctionItem> auctionItems = new ObservableCollection<AuctionItem>();
public ObservableCollection<AuctionItem> AuctionItems
{
get { return this.auctionItems; }
set { this.auctionItems = value; }
}
}
As you can see this class have a property called AuctionItems.
Because it inherits from Application it also contains property called 'Current' which provides access to the Application instance (according to MSDN).
Then in the code I have:
((DataBindingLabApp)Application.Current).AuctionItems.Add(item);
I do not understand it.
Since we can have many classes which may inherit from Application then how we know that Application.Current actually contains object of class 'DataBindingLabApp'?
Thank you!
Because Visual Studio generates entry point in the partial generated class of custom application type(DataBindingLabApp in your case) by default (You can find it by searching in the root directory of solution).
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main() {
DataBindingLabApp app = new DataBindingLabApp();
app.InitializeComponent();
app.Run();
}
And after application has been ran Application.Current contains instanse of DataBindingLabApp.
Since we can have many classes which may inherit from Application
That isn't relevant. What matters is that there is only ever one instance of the Application class. The one-and-only application that's running. Be sure to distinguish types from objects.