Accessing MainWindow Properties from other classes - c#

I can modify/access controls and MessageBox with ease in the MainWindow.xaml.cs file of my WPF application. However, when I create some folders and add classes there, I no longer have access to MessageBox nor do I have see the control names in the Intellisense dropdown.
(This seems quite elementary but I'm new to C# and WPF)

You must be missing a namespace. You could do something like this:
MainWindow.cs:
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace YourNameSpaceHere
{
public partial class MainWindow : Window
{
internal static string message_ = string.Empty;
MainWindow()
{
InitializeComponent();
SetupMessage();
}
private void SetupMessage()
{
message_ = "Hello World!";
}
}
}
OtherFile.cs :
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls; //are you missing this namespace?
namespace YourNameSpaceHere
{
public class Messaging
{
Messaging()
{
MessageBox.Show(MainWindow.message_);
}
}
}
Note that I am using the same namespace in each file and by using the internal keyword the message can be accessed by anything as long as it is in the same namespace. I hope this helps!

Does your classes have the right using statement in that would allow you access to the class you are attempting to access within the new classes you've created?

Related

Begginer problem with class in another file [duplicate]

This question already has an answer here:
'Class.function' doesn't exist in current context while using a self made NuGet Package
(1 answer)
Closed 5 months ago.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
}
}
}
And another class in another file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rock_Paper_Scissors
{
internal class ComputerGenerateSign
{
public string computer;
Console.ReadKey()
}
}
Why Visual Studio code have a problem ?>"Error IDE1007 The name 'Console.ReadKey' does not exist in the current context"
Use 'Console.ReadKey();' in the body of a method, That way it will recognize. You didn't write a method inside the 'ComputerGenerateSign' class like the class 'program' has the main method. hope this helps.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rock_Paper_Scissors
{
public class ComputerGenerateSign
{
public void method1()
{
public string computer;
Console.ReadKey();
}
}
}
You have made function call in class which was not allowed. In class you can define properties, fields and functions.
You need to call that function inside a function body.
Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rock_Paper_Scissors;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
ComputerGenerateSign obj=new ComputerGenerateSign();
obj.WaitForUserInput();
}
}
}
And another class in another file
using System;
using System.Text;
namespace Rock_Paper_Scissors
{
public class ComputerGenerateSign
{
public string computer;
public void WaitForUserInput()
{
Console.ReadKey();
}
}
}

missing using directive assembly reference error in c# vscode

When I try to compile a simple application I made in VSCode with 2 .cs files one with a class, and the other with the main method, I get an error: cannot find type or namespace "MyClass". I do not understand why this happens, as I have put both the main method and class declaration in the same namespace.
MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test.src
{
public class MyClass
{
public void testFunc() { Console.WriteLine("Hello World!"); }
}
}```
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test
{
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.testFunc();
Console.WriteLine("Hello World!");
}
}
}
I want the console to print Hello World!, but instead I get this error.
classes_test.src
and
classes_test
are not the same Namespace.
In order to set your class visible to the main method, you have to use the following using statement.
using classes_test.src;
This is a quite common mistake, especially for those who are c#-newbie.
Including a namespace, DOES NOT include the subnamespaces

Why i'm getting Inconsistent accessibility: field type .... is less accessible than field ? And how to fix it?

Error 1 Inconsistent accessibility: field type 'Youtube_Manager.Ffmpeg' is less accessible than field 'Youtube_Manager.ScreenShot.fmpeg'
In a class top i added:
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
namespace Youtube_Manager
{
public class ScreenShot
{
#region Global Variables
public static Ffmpeg fmpeg;
Then i'm calling to use fmpeg in another class in a timer click event:
ScreenShot.fmpeg.Close();
And i'm getting in the class ScreenShot on the fmpeg the error:
Error 1 Inconsistent accessibility: field type 'Youtube_Manager.Ffmpeg' is less accessible than field 'Youtube_Manager.ScreenShot.fmpeg'
And this is the top of the class Ffmpeg:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using DannyGeneral;
namespace Youtube_Manager
{
class Ffmpeg
{
NamedPipeServerStream p;
String pipename = "mytestpipe";
System.Diagnostics.Process process;
string ffmpegFileName = "ffmpeg.exe";
string workingDirectory;
public Ffmpeg()
{
Even if i change the variable fmpeg from static to public i'm getting on it the same error.
Top-level C# classes are internal by default. Internal has a lower visibility than public. Use public class Ffmpeg to make the class public.

Is it possible to create objects of MainPage class in silverlight webapplication using c#

I am c# web application beginner using silverlight 5. before i was working on c# console applications and there i had body of code working like this.
namespace check
{
public class main
{
public void FunctionDefinition1()
{
//Inside something
}
}
public class second
{
public static void Main(string[] args)
{
main object = new main();//this is how the objects were created and here the constructor was called.
object1.FunctionDefinition1(); //I was calling the FunctionDefinition1 like this here
}
}
}
But when i tried to create a web application in c# silverlight (asp.net web application). I don't know how to call a function definition from other class and how and where the objects are created in that class ?
I have created a small project names as "Fun". see the code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Fun
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
public void merge(int head)
{
MessageBox.Show("check1");
}
}
public class she
{
MainPage obj1 = new MainPage();
obj1.merge(1); //It is not working gives red line, means error.
}
}
Could some one please explain me using this code silver light c# code that:
(1) How the Objects of the classes are created (I mean constructor is initialized when we create the object of that class, Is it same here, If yes then why it is giving red line in VS 2010 here on merge() function call using Object of MainPage class).
(2) what does this InitializeComponent(); do ?
(3) Why there is no public static void Main(string[] args) function inside it ? So how the control passes by compiler.
(4) Suppose if i inserted one button in GUI so now the code changed to this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Fun
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
public void merge_sort(int head)
{
MessageBox.Show("check1");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
}
}
public class she
{
MainPage obj1 = new MainPage();
obj1.button1_Click(....);//It don't recognize "obj1" object. Why ?
}
}
Now is it possible to call the button1_Click() function from another class "she" ? Thanks if some one could explain me how the compiler's control go on this c# code whereas i am not able to see any " public static void Main(string[] args)" function.
Big thanks for answering these question.
UPDATED
I think I found your error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Fun
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
merge_sort();
}
public void merge_sort()
{
she Object1 = new she();
Object1.DoMethod(2);//Now this will do your work.
}
}
public class she
{
public void DoMethod(int head)
{
//Do what do you want
}
}
}
Please Explain how do access the Class she?
2). InitializeComponent();
In my words I think it has the all your output, and also it's exists in the compiled assembly .
3). public static void Main(string[] args);
In WPF Silverlight WP8 application do not need this, Because when App.Xaml is build Main method will automatically generated.If you look at the project properties you'll find there's a setting for you to choose the startup object. So if you want, you can provide your own class that implements Main().

C# web project complaining about a class

I created a new project (web project in C#).
Created a new folder in my project called App_Code.
I then proceeded to add a class with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
And in one of my pages default.aspx in the code behind I tried to do:
int i = BL.JustSomeTest();
I am not getting any intellisense when I type in BL.. It says I am missing an assembly or reference. Or it will say The name BL does not exist in the current context.
But do I have to include a reference if the class file is in the same project? I even tried to Build my project and it at first generated a dll file with the name of my solution file, Shipper.dll but as soon as I add this reference it says The name BL does not exist in the current context.
In my default.aspx page I've tried to add a using statement
using Shipper.
But as soon as I do that my namespace BusinessLayer is not shown...
Im confused?
Edit
Here is default.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;
namespace Shipper
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaults();
int i = BL.JustSomeTest();
}
}
}
}
Here is my BL.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
The error reads The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?
your...
public static int JustSomeTest()
{
return 1;
}
...is out of the 'class' - you cannot have methods defined for the namespace alone.
(at least from your example, it might be just a typo but then you'd need to give us a working example)
your method was outside a class. That's a no-no. Try this instead:
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
Also, if you're expecting BL to pop up, make sure the namespace is referenced (using Shipper.BusinessLayer). Why is this a static class, though? I think you probably don't want that unless you're making extension methods.
Try to delete the namespace declaration:
using System;
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
This forum thread on Wrox might be also useful.
try
using Shipper.BusinessLayer;
Things to try
Verify the namespace is the one you believe it is by viewing the properties of the target project in the solutions explorer.
In Visual Studio use the Object Browser and locate the namespace and verify the class can be seen. If the object browser can't see it, neither can the code.
First thing is make sure you have added a reference to the Shipper project, not the shipper DLL.
Then make sure the Shipper project is re-built, best bet to do a clean and build.
Then check your intellisense again, I suspect you are referencin an oleder version of the Shipper dll.
Try making your class not static but leave your method as static. (only because I never use static classes, but it doesn't appear to make any difference)
Or if you are seriously having problems and can't work it out install a copy of Resharper and it will probably help!

Categories

Resources