Call method in partial class - c#

I'm using Visual Studios. I wrote a method in a form1.cs file in a partial class
private void TestMethod1()
{
}
I want to call this method in form2.designer.cs, in the same partial class. I tried this:
TestMethod1();
but I got the error method not found.
this is the form.cs
namespace classA
{
public partial class A : B
{....
private void TestMethod1()
{
}
}
}
this is the form.designer.cs
namespace classA
{
partial class A
{
private void InitializaCOmponent()
{
.....
}
(where I call my function)
TestMethod1();
}
}

If the situation is as you described, then the compiler should not generate the error message as it is valid code.
However, if you try to use the visual editor, and you insert the call in your code inside the InitializeComponent method you will get an error.
This is caused by the Form editor not being able to call functions that are defined within the class you are actually editing - it is a bit restrictive about what you can do within that scope.

Related

How to fix "Does not exist" when calling a static function from another class?

I want to call a static void function from another class, but it's said
The name [funcion name here] does not exist in the current context
Each class is in the same project, Framework 4.5.2
Its a public static void Function, in a public static class, don't see why it's not working.
The class where a function located, I want to call:
namespace Client.Modules
{
public static class Login
{
public static void Run()
{
// do something
}
}
}
The class where I want to call:
using Client.Modules;
namespace Client
{
public class Main
{
Login.Run(); // here
}
}
public class Main
{
Login.Run(); // here
}
That’s invalid: You can’t generally execute code outside methods. The only things that can go directly into classes are declarations. Put Login.Run() inside a method.

C# Base Class MyFrm could not be loaded

I have the following custom Form (MyFrm) which inherits from Form.
public class MyFrm<T>: Form where T: class
{
}
And following is my Form1:
public partial class Form1: MyFrm<CONTACTS_BASE>
{
public Form1()
{
InitializeComponent();
MyInitialize();
}
public void MyInitialize()
{
}
}
as can be seen, there is nothing exceptional, However, when right click and select view designer I get the following error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:
Form1 --- The base class 'MyGym.Controls.MyFrm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
when I remove the part below and edit my Form1 accordingly I get no errors when I go to the designer mode.
: Form where T: class
Why am I facing this issue? is there a fix for this?
Thanks
I believe you need to explicitly provide the InitializeComponent() method so that the Visual Studio IDE (Designer) works properly.
public class MyFrm<T> : Form
where T : class
{
public MyFrm() : base()
{
InitializeComponent();
}
private void InitializeComponent()
{
}
}
and then chain the constructors it together
public partial class Form1 : MyFrm<CONTACTS_BASE>
{
public Form1() : base()
{
InitializeComponent();
MyInitialize();
}
public void MyInitialize()
{
}
}
Note that : base was added to the constructor. However, in this example it's a bit overkill as the base constructor would already be called implicitly. I provided this addition, due to this answer. It states you must keep the constructor parameter-less in your base class.

initialized delegates in class diagram

I use Visual Studio 2015, and I have created a class diagram to have an overview of my most-used classes and their members.
I have a delegate defined in a class named UserMessage:
public delegate void ProcessUserMessage(UserMessage message);
I use this delegate in an other class:
public UserMessage.ProcessUserMessage ProcessUserMessage;
So far no problems.
Because I hate testing the callback for null every time, I hook up a no-op event handler at initialization, as suggested here:
public UserMessage.ProcessUserMessage ProcessUserMessage = delegate { };
But when I do that, and re-open the class diagram, it fails to load, saying:
Code could not be found for one or more shapes in class diagram 'ClassDiagram1.cd'. Do you want to attempt to automatically repair the class diagram?
The auto-repair doesn't work of course ;-(
Even when I place this initiatlization in the class' constructor, instead of at the declaration, the same error appears.
I fail to understand what's wrong. Any clues?
Update:
I created a blank project with just the failing code:
public partial class MainWindow
{
public UserMessage.ProcessUserMessageDelegate ProcessUserMessage = delegate { };
}
public class UserMessage
{
public delegate void ProcessUserMessageDelegate(string foo);
}
The strange thing is that the class diagram for MainWindow loads fine, but for UserMessage it fails. But I am not changing anythign for UserMessage.
It loads OK if I change class MainWindow to:
public partial class MainWindow
{
public UserMessage.ProcessUserMessageDelegate ProcessUserMessage;
}
Found the solution...
The anonymous no-op delegate must conform to the delegate definition, so all I had to add was add the argument ((string foo) in this example):
public partial class MainWindow
{
public UserMessage.ProcessUserMessageDelegate ProcessUserMessage = delegate (string foo){ };
}
public class UserMessage
{
public delegate void ProcessUserMessageDelegate(string foo);
}

Visual C# - Missing partial modifier

I get an error saying "Missing partial modifier on declaration of type 'projectName.Main'; another partial declaration of this type exists.
From what i can read about this error, its because i have classes with same name. It does work if i modify the main class to public partial class Main : Form but i want to know why it gives me this error. I need to initializeComponent within Main(), tried creating a method start() and then calling main.Start() in a load event, but then the form loads blank.
namespace projectName
{
public class Main : Form
{
public Main() // Method: Starts the main form
{
InitializeComponent();
}
public void Main_Load(object sender, EventArgs e)
// On load of main class, handle events and arguments
{
Main main = new Main();
main.getCurrentDomain();
}
public void getCurrentDomain() // Method: Get current domain
{
Domain domain = Domain.GetCurrentDomain();
}
} // Ends the main class
}
Assuming this is a Windows Forms app, the problem is that the Visual Studio WinForms designer has created another file (e.g. Main.designer.cs) with:
public partial class Main : Form
to contain designer-generated code.
Your partial class source is effectively merged with that - but it can only happen when both source files declare that the class is partial. Otherwise, you're just trying to declare two classes with the same name in the same namespace, which is prohibited by C#.
There is another file with the name .designer.cs. This file contains the partial definition of class and that is where your InitializeComponent function is defined. You need to add partial modifier to your class.

Project is not recognizing constructor contained in a different assembly

I have one assembly that looks like this:
namespace AssemblyOne
{
class MyFirstClass
{
public MyFirstClass(String param)
{
// Assign stuff
}
}
}
Inside another assembly, I am trying to create an instance of this class. So, naturally, I have tried this:
namespace AssemblyTwo
{
public partial class SomeForm : Form
{
private MyFirstClass mfcObject = new MyFirstClass("Some String"); // Error here.
}
}
I have added the other project as a reference and inserted the necessary using statement. However, the line above where I create this object is giving a compiler error:
'AssemblyOne.MyFirstClass' does not contain a constructor that takes 1 arguments.
This works fine when the two are in the same assembly. Why is it not recognizing the constructor?
Because MyFirstClass should be declared as public. Modify your code to become:
public class MyFirstClass
Otherwise, it defaults to be internal

Categories

Resources