Passing Variables From Cs File to another Cs File in C# - c#

I am trying to pass the m_paths value from the class : ExtractDescriptorsForm.Cs to VisualizeFrom.Cs ..
This is how the variable is defined in ExtractDescriptorsForm.Cs :
namespace MediaLab.TopSurf
{
public partial class ExtractDescriptorsForm : Form
{
// list of paths that point directly at images, or at
// directories that contain images
List<string> m_paths;
public List<string> Paths { get { return m_paths; } }
......}}
What I wrote in VisualizeForm.Cs :
List<string> j = ExtractDescriptorsForm.m_paths;
The error is :
an object reference is required for the non-static field,method or property 'MediaLab.TopSurf.ExtractDescriptorsForm.m_paths'

To start, I need to make sure you understand something very fundamental here:
Files have nothing to do with this!
You're working with class objects, not files. You can have more than one ExtractDescriptorsForm showing the on the screen at the same time, and there's no reason this form needs to be in a file named ExtractDescriptorsForm.cs. In fact, you could put both the ExtractDescriptorsForm and the VisualizeFrom class definitions in the same file.
Learn this, and learn it well, or you'll struggle to ever be effective writing code.
Case in point... for this issue, the VisualizeFrom object is trying to look for the Paths property in a ExtractDescriptorsForm object... but which object? You're using class name here, when you need to be thinking about instances of the class.
I see the comments that you're trying to create a new ExtractDescriptorsForm object. This is probably a mistake. You very likely already have an instance in your program somewhere that you're already using, possibly created by code that Visual Studio provided for you. You need to find that existing reference.

Related

Can't see a class in the same namespace in different file at compile time

My problem is as follows:
I have a namespace, let's say "Foo". There are already two classes in it, let's say "MyClass" and "SomeOtherClass". They are both in separate files.
I need to add a third class to this namespace, let's say "Data", in a third file.
To begin with I implemented the logic of "SomeOtherClass" without a problem.
But then the project evolved a bit and, for the sake of clarity, the logic within "SomeOtherClass" that was using "Data" needed to be moved into "MyClass".
But now whenever I try to compile I get a compiler error reporting that "The type or namespace name 'Data' could not be found".
What's strange is that if I try to type "Data" in the editor then intellisense prompts me with the valid options for that class, and it is the same if I start with "Foo.Data", the name is also display with the light blue color.
If I delete "Data" and add the class in the same file as "MyClass" everything is fine. If I re-add "Data" in its own file (without deleting the one in MyClass' file) then I have an ambiguity between "Foo.Data" and "Foo.Data" (and this is logical).
I've also tried to delete and recreate "Data" from scratch, but it didn't solve anything.
I've searched on the internet for a solution, but I could only find answers relating to project references or Target Framework being bad, but this does not apply in my case, since they are all in the same project and I can access "Data" in others projects lying around.
NB: I don't know if it's important, but "MyClass" and "SomeOtherClass" inherit from the same parent class.
1. MyClass.cs
namespace Foo
{
public class MyClass
{
private Data data; //Error at compile time - Type or namespace name 'Data' could not be found
}
}
2. Data.cs
namespace Foo
{
public class Data
{
//Some logic to handle the data
}
}
3. SomeOtherClass.cs
namespace Foo
{
public class SomeOtherClass
{
private Data data; //Actually works well
}
}
Ok, following #Grx70 advice, I've tried to rename many things.
Data, to Data2, with no effect. MyClass to MyClass2, with some effects.
By going throught the output given with the last change, I've found out that another project use the file MyClass.cs, and have a copy for all others files that are used in MyClass.cs.
By adding an Include in the csproj of this project for Data.cs, and copy/paste Data.cs file into its folder, it solved the problem.

Call object in another class in another file with Visual Studio

I have a public class A in a file called A.designer.cs and there is a textbox object called Textbox1 in there that I want to use. It is declared as:
public DevExpress.XtraGrid.Views.Grid.GridView textbox1
I want to use it in my b.cs file, which is of a public partial class b. How do I call the Textbox1 in class A?
I tried
A.Grid.GridView.textbox1.Text = "hi"
but it is giving me the error that the file
"does not contain a definition for "grid"".
It's a public instance member of class A so you need an instance A and you can then access its textbox1 field. You've already done this same thing a hundred times before with various other members of various other types. Why try to make it harder this time?
the scenario doesn't make much sense to me but if that's the case.. Do it as usual.
//get a instance of A, something like
A a = new A();
//access by:
a.textbox1
But really you may want to think about why accessing a gridview from outside.

Visual Studio plugin access type on a short cut menu

I try to write a plugin where I need to access the object where the user opened the short cut menu. (Explanation of short cut menu: http://msdn.microsoft.com/en-us/library/ms165623.aspx)
Example:
public class MyPublicClass
{
private void myMethod()
{
public MyClass class = new MyClass();
}
}
If the user right clicks on class than I need to know the type "MyClass". This should work for any type and any class.
Things I have done so far:
I know how to create a plugin.
Tried using CodeElementFromPoint. ( not working )
Tried using http://www.mztools.com/articles/2006/MZ2006009.aspx ( If iam using vsCMElementClass this just returns MyPublicClass but not MyClass.)
I need to know the type because I want to generate something via reflection.
( I am writing the addin with c#)
Thanks
I found out, that this is not possibly. This is mentioned somewhere in the docs too. The deepest point you can reach with FileCodeModel is the method level. Things inside a method can not be accessed.
But there is a nice solution. You can use the Roslyn compiler. With this new open source compiler you can access every type, every property etc. of the code.

referencing a variable from another file/project

I'm going to try to explain this as best I can. I'm working on an application that needs to get data from another program. I have them all in the same solution I believe, I have the application referencing the .dll that has the variable i want in it.
the code is kinda set up like this
the .cs file that has the varible i want is set up like this
public class myObserverClass
{
//then there is a bunch of functions and the variable i need is in one like this
static void functionWithMyVariable(ref something, int toTier){
string myVariable = some value;
}
}
In my main application i need myVariable but i'm not sure how to access it. I have it using the namespace of the second program.
Have your function return the variable value you want rather than returning void.
static string functionWithMyVariable(ref something, int toTier){
string myVariable = some value;
return myVariable;
}
Also note that you need to make functions and (object/class level variables) PUBLIC if you want to access them from other projects.
myObserverClass.functionWithMyVariable();
Will not work unless functionWithMyVariable is a PUBLIC function.
public string functionWithMyVariable(ref something, int toTier){
Referencing an assembly (the .dll) in both applications is having them share code, which is not the same as sharing data. Each program will create individual in-memory versions of the classes and structures found in the .dll. See Transfer large data between .net applications on same computer for more info on how to transfer data between .net apps.
If I am not mistaken you have dll of an application say App1 having Class say C1 which is public. In this class you have a variable say V1.
Now you refer this dll into your another application say App2 where you want to access above said variable V2.
Now if the class C1 and the variable V1 both are public then you can access these like we access variables of one class to another into a single application by creating its object, but the condition is that both class and variable should be public.
Hope this can help you :)

accessing an instantiated sealed class public member

i am trying to instatiate a public sealed class in my program,
the thing is, ...as i am still fresh C# .net not-yet Developer , i find this issue a little difficult ...
As for the problem in Question, you can skip straight to Program example, or read the folowing background:
DB_Schema is a helper namespace, i've Created, to deal with the data accessing
(it holds tables And SPs names ..etc')
one of its classes(below) deals with Stored Procedures, and this one holds names of SPs Parameters
public sealed class SProc
{
public sealed class GetCPAReport
{
public const string RecordNum = "#RecordNum",
CPAColumnName = "#CPAColumn_Name",
Value = "#value",
IsFreelance = "#isFreelance";
}
}
Usage in program:
within method for data access via SP
private DataTable Get_RefTable_OfUsersBy(string DepartmetID)
{
SProc.GetCPAReport SProcGetCpa = SProc.GetCPAReport();
SP_Params.Add(new SqlParameter(SProcGetCpa.IsFreelance, 1));
}
trying to access one of the instance (SProcGetCpa) members is not possible the way i tried .
i could just make class SProc + it's sub class UpdateCPAReport not sealed and ...
but as i was searching the question "can sealed class be instantiated?
well.. the answer is Yes ... though trying to find information on the error:
cannot be accessed with an instance reference; qualify it with a type name instead
yields no results, Nor an Example of Accessing Instantiated sealed class public member code
atleast not for fresh .net C#arpers like me
- Update
i wanted to avoid long lines and make custom short names for the strings that represents the stored procedure name
instead of
ParListEmployeeUsrs.SP_Params.Add(new SqlParameter(HTSPs.RobTC_CPA_Users_Names_JobPosition.IsFreelance, SelectedDepartmentID));
update 2
for future comers on this subject who seeks for an answer
as suggested by a dear friend of ours, here in StackOverflow
if you do have to make a short namings for your classes, when using them for current peoject :
just place this among the usings of your project
using system...
using restOf.net
//just add your own as follows !
using shortClassName = myHelperNameSpace.MyIncrediblyUnnecessaryLongHelperClassName;
GetCPAReport doesn't have any instance members. const members are implicitly static. In C#, you can't access static members through a reference as you're trying to at the moment.
You just want:
SP_Params.Add(new SqlParameter(SProc.GetCPAReport.IsFreelance, 1));
Personally I'd make GetCPAReport a static class, too. There's no point in instantiating it, as it just contains constants... so actively prevent it from being instantiated.

Categories