Keep common functions in separate cs file? (C#) - c#

I have a common functions AppPath() that is used in several windows forms of my application, instead of declaring it in each form, I would like to place it in a separate cs file.
I have created a separate class cs file, which I gave a different namespace (modUtilities) and put all functions inside a modUtilities class.
namespace modUtilities
{
public class modUtilities
{
// all functions such as AppPath()....
}
}
But I can't figure out how to use function from modUtilities inside different windows forms where I need it. I am trying to use "using modUtilities", instead of creating new instance (modUtilities modU = new modUtilities())
Can someone help me?

if your methods are static within the class, you can do
using modUtilities;
then
var something = modUtilities.AppPath();
if not, you'll need to create an instance of the class.
modUtilities mod = new modUtilities();

Related

How do I use a separate C# file on one xaml file

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.

How to access android system settings with Unity in C#

I'm making an app and need to be able to check if settings like : Bluetooth/Phone Rotation/Flashlight/Plane Mode/GPS/Phone Brightness/Silent Mode, are activated on an android phone.
I haven't found any way to do it within Unity, using C#. I found ways to do it using Xamarin but none of them work with Unity (or maybe I haven't done it right), the only way I found is using Java and making it into a plugin and call it in a C# script. But I can't find a clear way to make this work. If this is the only solution could you please explain how to do it, all the documentation I find is from old versions from 2014.
I think there is a simple solution for this but I simply can't find it. And the manifest part is not a problem, I'll add the permissions needed.
In Java the methods you want to call should be public or static, you must build your java source as a library (in build.gradle: apply plugin: 'com.android.library'), and add the .aar to Unity's Assets/Plugins/Android/ folder.
Then you can instantiate your plugin in Unity like so:
// this class string is the package at the top of your Java class extended with the class name, e.g.:
// package com.yourcompany.you.package;
string classString = "com.yourcompany.you.package.className";
// Get the class
var tempAjc = new AndroidJavaClass(classString);
// Here you can call a static method on the class that returns an instance of the class if you want to pass some parameters upon creation
_androidObject = tempAjc.CallStatic<AndroidJavaObject>("CreateInstance",
new object[] {arg1, arg2});
// non static call on your new instance
_androidObject.Call("PassingMoreStuff", initParam);
// if you want to return something from Java to Unity:
int javaVal = _androidObject.Call<int>(methodName, parameters);

Using a referenced C# object in a seperate class/form

I've got a Windows Form program that creates a Config object which contains various configuration variables used by my program.
Within the main form, it contains a button to open a new configuration form, where it passes the Config object as a reference -
FormConfig button = new FormConfig(ref config);
button.ShowDialog();
Now in the FormConfig class, I can access the Config object within the main constructor
public FormConfig(ref Config config)
{
InitializeComponent();
// can access config.xyz OK here
}
However within the new form, I've got a button that calls another function that needs to access the reference Config object, however I'm struggling to find a clean way to do so.
I can create another Config object as part of the FormConfig class, and then copy the referenced Config to it in the main constructor, however then the original config object doesn't get updated.
How can I achieve this?
PS apologies in advance if this is already answered, but my searches have so far failed to find a solution, possibly because I'm not sure what the correct search terms should be.
And the solution thanks to #cmos, is to declare the Config class as static, which negates the need to use any referencing or passing objects between classes/functions -
public static class Config
{
public static bool SettingA = true;
}
Which means I can access and modify the Config object from anywhere within the same namespace with the following code, without needing to have a class instance -
Config.SettingA
Thanks to all those who helped point me in the right direction.

Access properties in App.xaml.cs from different project UWP

Like what I said in the title, how can we access the property in App in a different project? I want to access it from normal class like a service. Not in viewmodel. Hope we can do something like Application as App.
Access properties in App.xaml.cs from different project UWP
You may try to use one App.xaml.cs for the two projects. For example, if the second project wants to access App.xaml.cs in the first project without reference the first project, you may consider remove the App.xaml.cs which is belonged to the second project, and Add-ExitingItem to add the App.xaml.cs from the first project. In that case, the two projects will share the same App.xam.cs and then you can directly access the properties as Marian Dolinský mentioned.
Otherwise, the two projects may not be able to communicate with each other directly. If the above method is not suit for you, please detail why you need this feature and we may need to consider other ways without accessing the App.xaml.cs.
Method 1
You could cast Application.Current to App:
App app = (App)Application.Current;
app.YourProperty = something;
Method 2
Create some static property holding the reference of App. In my projects I do it by creating a new property called Current as follows:
// in App.xaml.cs
public static new App Current { get; private set; }
public App()
{
Current = this;
// Another code
}

How to make function "invisible" in a class library c#

I am creating a class library and have different functions inside. I also have a Console application that can access this functions once they reference the class library. I would like to know how to make a function "invisible" so the client won't be able to see it exist and they can only use it if they write it out perfectly.
I have this function in my class Library :
public string custommessage(string messagetosend)
{
string receivedmessage = CallServer(messagetosend);
return receivedmessage;
}
and basicly when I am in a different program referencing the library I dont want to see this function in my list of avaiable functions to chose from :
Append
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
to your method as an attribute. This will hide the function from intellisense.

Categories

Resources