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.
Related
I am fairly new to C# programming (and programming in general). I want to use a variable in two different methods I thought I needed to declare the variable just inside the class but I keep getting this error message "Error: A field initializer cannot reference the non-static field, method, or property" I am sure it is a relatively simple error on my part, but how do I fix this?
After researching online for awhile I think I am on the right track of understanding classes but my understanding is obviously lacking.`
public partial class MainPage : ContentPage
{
public string path = diceNumber.SelectedItem.ToString();
public MainPage()
{
InitializeComponent();
}
private void DiceRollResult_Clicked(object sender, EventArgs e)
{
if (path == "One")
{
DisplayAlert("One", "You Lost", "Close");
}
else if (path=="Two")
{
DisplayAlert("Two", "You Lost", "Close");
}
else if (path == "Three")
{
DisplayAlert("Three", "You Won", "Close");
}
// The else if statements are just to show you how I am using the code.
A few things for you to understand here:
If you assign a value to a class-level variable, it is executed before any other part of the class. So diceNumber.SelectedItem won't even exist at the time when path is being initialized. That is the cause of your error. You can only use static fields or values to assign to a class-level variable for initialization (because static members do not need an instance).
Then there is a logical mistake you're making. Even if it were possible to assign diceNumber.SelectedItem to your variable upon startup, you probably don't want to do that, because then it would only be executed once upon startup. What you actually want it to do is to check currently selected value at the time of click and then respond accordingly. Therefore you should move your path variable inside the click handler because I don't see you're using it anywhere else.
Lastly, if you need to access this value in other functions too, you can create local variables inside those functions just like this:
string path = diceNumber.SelectedItem.ToString();
in all the functions where you need this. No need of a global variable.
In essence, diceNumber (which is probably a UI control) itself is a global class-level variable. So it will do everything that your path variable is doing for you. Not sure if this is WinForms or WPF or something else, but you can always see the declaration of these UI controls as class-level variables in the code-behind.
This seems to be a Xamarin.Forms application, its not a good practice to use a variable in several methods you would generally use a class and instantiate that class throughout the lifespan of your program.
You can access a variable from any other class or form by making it a static variable.
public static string path = diceNumber.SelectedItem.ToString();
Now in any class or in any method, you can access the variable by
var s = MainPage.path;
The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of an object unless it is explicitly passed in a method parameter.
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.
I am trying to learn passing a list between two C# forms using constructors as shown below. On the first form I did:
List<Cat> myCatList;
//list populating function...
private void btnDisplay_Click(object sender, EventArgs e)
{
df = new DisplayForm(myCatList);
df.Show();
this.Hide();
}
On the next form, I tried to receive the data as shown below:
List<Cat> catList;
public DisplayForm(List<Cat> catList)
{
InitializeComponent();
this.catList = catList;
}
But I always get an error on the second form constructor saying:
Error 1 Inconsistent accessibility: parameter type 'System.Collections.Generic.List<_05_WindowsFormsAppCat.Cat>' is less accessible than method '_05_WindowsFormsAppCat.DisplayForm.DisplayForm(System.Collections.Generic.List<_05_WindowsFormsAppCat.Cat>)'
Any ideas?
The List part is a complete red herring here. You'd get exactly the same problem if your constructor had a Cat parameter instead of a List<Cat> parameter.
Your Cat type is probably internal, because you haven't declared it as public. Therefore you can't use it in the signature of a public member such as this:
public DisplayForm(List<Cat> catList)
Options:
Make Cat a public class
Make your DisplayForm constructor internal
Personally I'm all for keeping things as private as is practical - although for small projects it won't make much difference, particularly for apps which are just a single assembly anyway. Most developers tend to err on the side of making everything public, which is a mistake IMO, but it's a judgement call. Both of the above options will work fine... but you should at least think about whether you want any other assembly to know about the Cat type (or indeed whether you want code in other assemblies to be able to call that constructor).
Where did you declare the Cat class? It must be publicly accessible to the DisplayForm class. You may have to add the public keyword to its declaration.
This is known as the accessibility of a type or a member.
Here is a reference of the different levels and their default values:
http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx
I'm guessing that your Cat type was either in another assembly (project) as your DisplayForm, in which case it was by default not visible to classes in that project, or that you defined it as a nested class of your first Form class which would have made it private and accessible in the scope of that Form.
I have created folders in my project named Classes, Forms, and Models.
Let's say my project is named ABC, so the folder hierarchy is:
ABC
Classes
Forms
Models
In \Models\, I have a class named ApplicationModel.cs, which contains a public method named GetApplications().
However, when I call that method from elsewhere in the same ABC project, I get, "The name 'GetApplications' does not exist in the current context"
I've added:
using ABC.Models;
to the calling class, but it makes no difference. I right-clicked GetApplications() to hopefully see "Resolve" there, but no go.
What must I do to access my own public method?
It would be helpful to see the definition of GetApplications() and the code that's attempting to call it, but I assume it's either a static or an instance method of the ApplicationModel class. In either case, you may have made your code aware of the namespace of the ApplicationModel class with the using statement, but the method must either be called on the class or an instance of the class, like so:
If GetApplications is a static method,
var applications = ApplicationModel.GetApplications();
If it's an instance method:
var appModel = new ApplicationModel(); // or, retrieve the instance from elsewhere...
var applications = appModel.GetApplications();
One way or another, you must refer to the class containing GetApplications in order to call it. If this doesn't help you solve the problem, please edit your question to contain the definition of the method, and the calling code.
Sounds like you're using a static function. Did you forget the static keyword?
A static function "runs" from a class, not an object:
public static string[] GetApplications()
It is hard to give definitive advice without some code on how you are trying to call that method. I can think of two possible ways:
either you are trying to call the method via the ApplicationModel class(ApplicationModel.GetApplications()), in which case you need to declare the method static
or you need to call the method on an object, but you are using the type -- in this case declare/create an object of type ApplicationModel and call the method on that object; (e.g. ApplicationModel model = new ApplicationModel(); model.GetApplications();)
Looks like the class is not marked as public.
You class should be
namespace ABC
{
namespace Models
{
public class ApplicationModel //class needs to be public is accessed outside the namespace
{
}
}
}
As you can see, the class names I used are default names of classes generated by Visual C#. How can I go about changing values in a TextBox named "textBox2" (this TextBox is placed in the Form1 design already) from the "Program" class? I have tried a lot of things and every thing I tried results in this error (or similar to): An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.textBox2'
Please, if you can, try to keep your answers simple, thank-you.
First, you generally shouldn't access controls such as textboxes from your Program class. Instead you should do this in the Form1.cs file. Form1 is a class, and it has a protected field for textBox2 so it is inaccessible outside the class. If you want to change the value from Program.cs, you should add a public function to Form1 that sets the value of textBos2.Text.
Secondly, you appear to be just typing class names instead of the name of the instance. The difference is Textbox is a class, textBox1 and textBox2 are instances. Textbox.Text is invalid because you need to specify WHICH textbox you are trying to get or set the text for. It's the same with Form1.textBox2. Form1 is a class and there can be many of them. You must specify the name of the instance of the form to access its public members.
UPDATE:
I'm just going to give you a brief explanation of the difference between a class and an instance of a class, static fields and non-static fields. Please forgive any wordiness.
When you create a new windows forms application Visual Studio will create a Form1 type for you. Form1 is a class. Program then uses Form1 to create a form instance and show it. The code would look something like:
Form1 form = new Form1;
In this case, form is the instance. You can create multiple instances of Form1. Each instance will have the textBox2 you created, which is an instance of the Textbox class. Just like you have to do textBox2.Text to get the text of the second Textbox you created on the form, you must specify form.textBox2 (or your public method that sets the textBox2.Text value). Form1, even though it has a number after it is a class, and form is the instance. They have the same relationship as Textbox and textBox2.
Non-static members are accessible to an instance. Static members are accessible to the class. A static member can not access a non-static member unless it is through an instance.
You'll need to have an instance of your Form1 to do this.
Form1 frm = new Form1();
Then you'll have to build a public method to access to your textbox, cause it's a private member.
Form1.cs:
public void UpdateText(string newValue)
{
this.textbox2.Text = newValue;
}
Finally:
frm.UpdateText("new text");
You have to create a new instance of the Form1 class.
You can't "reach" a non static var without new()
var form1 = new Form1();
form1.textBox2.Text= "aaa";
Make textBox2 public or internal. To do so adjust the Modifier property of it from the designer (Properties). Then do this in Main from "Program" class:
Form1 f = new Form1();
f.textBox2.Text = "sdfsdf";
Application.Run(f);
This is absolutely a bad design anyways.. Tell us why you would want this, we would be helpful in dealing with the real problem.