Using a class in WPF - c#

When I'm developing a ConsoleApp there is no problem to use classes in my Main that I've created in separated files (Project Menu --> Add Class). But later, when I try to do it in WPF that class is not recognized. I have made sure that the namespace it's the same both in my "MainWindow.xaml.cs" and my Class Canal.cs. When I define that same class but inside MainWindow.xaml.cs everything works fine, but due to the extension of the code I prefer separate it.
MainWindow.xaml.cs:
//using
namespace Tcomp
{
public partial class MainWindow : Window
{
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{ //Stuff but I can't use class created outside of MainWindow.xaml.cs
}
}
}
Canal.cs
//using
namespace TComp
{
public class Canal
{ //some propreties here
}
}
Thanks.

Create the class inside a library project not on a console app. And on your WPF project, add a project reference to the library project you have created.

#mcxiand already answered your question. I would like to add another option: you can use a
public partial class MainWindow : Window
and add it to as many files as you want containing your code, thus there will be no need to create additional class library. The key word here is partial, which allows the code encapsulated in this class to spread over multiple files (.cs).

You must either instantiate the Canal class:
var myClass = new Canal();
and then you can use the properties from it. Make myClass a private member of your MainWindow.xaml.cs and you can access it anytime. Or the second way, make Canal class static and then you can access it from everywhere. Hope this helps.

Related

Main Menu driven programme for different Projects

For an assignment i had different DataStructures (in C#) that I needed to implement. That part was easy. But I am also required to make a main menu driven programme for all those projects. So i searched a bit and found out a way. For example i had a project named linkedlist with namespace as Implementing_LinkedList and another project queue and namespace as Implementing_queue. To make a menu driven programme i just created another project named Assignment with namespace assignment. Mainconsole. Then i changed the namespace of linkedlist project to Assignment.Implementing_LinkedList and similarly with queue.Also made all the methods and classes public. Then i just called the main method of linkedlist inside main method of assignment as follows
Implementing_LinkedList.Program.Main();
Main method is inside a class named program in linked list project and it worked. Now i want to know how this worked because specifically .MainConsole part and if there is any other way to acheive this.
(Also i am pretty new to Stack overflow so pardon me for the extra long question but i didn't want to skip any details)
You don't have to rename namespaces or anything. Each program's Main() function is static and it can be called from any other function using a fully qualified name.
Program1.cs
namespace Project1
{
public static class Program1
{
public static Main(string[] argc)
{
// code here
}
}
}
Program2.cs
namespace Project2
{
public static class Program2
{
public static Main(string[] argc)
{
// code here
}
}
}
MenuProgram.cs
namespace MenuProject
{
public static class MenuProgram
{
public static Main(string[] argc)
{
// call all projects in sequence
Project1.Program1.Main();
Project2.Program2.Main();
}
}
}
This assumes the menu project has all the sources of the sub projects included. If they are separate project files, then you need to Add Reference... to those projects from the project file references.

In Visual Studio C# project do files aware the other file content without putting a using statement?

Say If we use two separate files called a.cs and b.cs in a C# project using VISUAL STUDIO, my question is does one file aware of the other WITHOUT putting a using statement about the other file. ie In the file a.cs can we use a class that is already defined in b.cs but not putting a using b.cs; statement in the beginning of the file?.When we compile altogether will the project know each file content and won't raise any error?
I guess you are on the wrong track here. Files don't interact with each other. But the classes do. Namespaces are used to refer to the class that are meant to be used. You can change the file name to anything several times, it won't affect your project. Moreover you can put many classes inside the same file name under the same namespace and you won't have to use using.
Just consider this scenario, Namespace are the area code, and the phone numbers are the classes. Being in the same area already, you don't have to use the area code to call a different number that exists in the same area. But if you are dialling a number outside your area, you would want to use the area code. Basically by adding area code(namespace) infront of the number, you are applying using to refer to the other number(class). Hope you got the idea.
Edit: Explaining programmatically
Suppose this is your Area
using something;
using someotherthing;
namespace MyMainNamespace
{
private class MyMainClass
{
private void blahblah { ... }
}
private class ClassABC
{
private void blahblah { ... }
}
private class ClassXYZ
{
private void blahblah { ... }
}
}
See, in the above example, to interact with the MyMainClass, ClassABC & ClassXYZ. you don't have to use using MyMainNamespace;. Because they all lie in the same area MyMainNamespace. But there exists a class in another namespace like shown below:
using something;
using someotherthing;
namespace SubNamespace
{
public class SecondaryClass
{
public void apple{ ... }
}
}
If you want to access SecondaryClass which lies in SubNamespace(different area) you would have to use using SubNamespace; in your main area. Like:
using something;
using someotherthing;
using SubNamespace; //add the namespace
namespace MyMainNamespace
{
private class MyMainClass
{
private void blahblah {
...
// Now you can use methods & functions that exist in `SecondaryClass`
SecondaryClass secondary = new SecondaryClass();
secondary.apple();
....
....
}
}
}
Hope this is enough to get the idea by now
Also, it doesn't matter that these namespace(MyMainNamespace & SubNamespace) lies in the same file or different file. You NEVER REFER TO THE FILENAME(filename.cs) by applying using. You ALWAYS REFER TO THE NAMESPACES.
If the C# code in a.cs and b.cs are inside the same namespace, then no using statement should be needed. If the 2 cs files use different namespaces, then you will have to put a using statement for the namespace of the code you want to reference.

uwp inheriting from base page giving compile errors

I am trying to inherit from a base page instead of Normal Page control of XAML but Visual Studio is giving me compile time errors that OnNavigatedTo method not found to be overridden.
I am doing this because I have multiple pages with a lot of similar code and I want to write that code just once, so I want to write that code in parent class and then inherit all pages from that parent class, I followed guidelines as stated on multiple resources on internet and below is my code.
Parent Class:
public class VideoParentPage : Page
{
}
Child Class (AllVideoPage.xaml.cs):
public sealed partial class AllVideosPage : VideoParentPage
{
public AllVideosPage() : base()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
SetBanner();
}
}
XAML in file AllVideosPage.xaml:
<local:VideoParentPage
xmlns:local="using:Fluent_Video_Player.Views.Shared"
...some unrelated XAML code...
</local:VideoParentPage>
Note: I have used all necessary using statement in C# classes.
You should try cleaning the solution Build > Clean Solution, closing VS, removing the remaining bin and obj folders and then compiling the app again.

How to resolved namespace conflicts?

I am using one base dll named as KSWeb.dll. It has main namespace is KSWeb.This dll has main Base class Under KSWeb.KSS.Base.
Now I have added this as reference in of one of the class library project. I want to give namespace to this class library also KSWeb.I have added a new class named as NewCK which inherits from KSWeb.KSS.Base.
When I am doing coding means KSWEB and then process dot(.) then it can found KSS and then found Base also.
My problem is when I build this class library then it gives me error like KSWEB.KSS not found and then I press alter + tab KSWEB.KSS not found.
Why is it so? please refere below code.
Now in this class library project I have create a class like
namespace KSWeb
{
partial class NewCK : KSWeb.KSS.Base
{
}
}
for my point of view it is compulsory to use KSWEB namespace in class library
Please suggest in c#.net or in vb.net.
//Use a namespace alias
using Other = KSWeb.KSS;
namespace KSWeb
{
partial class NewCK : Base
{
//You can now use Other as below
Other.Method();
}
}

Static Class Problem multi project

I have two applications and 1 class for database process.
First Application is a Windows forms application like
public class Form1 :System.Windows.Forms.Form
{
public Form1()
{
}
}
And my second application is a Windows control library application like
public class MyControl :System.Windows.Forms.UserControl
{
public MyControl()
{
//
//Some Code
//
}
}
and my class is static class like
public static class Deneme
{
public static void Connect()
{
//
//SomeCode
//
}
public static void CreateTable(string SqlCommandP)
{
//
//Some Code
//
}
}
I compile the control library application and get a .dll file. After that, I'm adding this file to Windows application project toolbox. I can use this control.
My problem is that:
I am using static class in a Windows application and control library application maybe I will make another control libary application.
How can I use this static class from one project it have to be one in a project?
If the static class Deneme is to be shared between the 2 applications, you should place that static object in it's own library which both the WinForm project and the control project will reference.
Reference one project to the other? It seems like you're forgetting the Project Reference somewhere. Being more specific with the question might be helpful, though.

Categories