I have one textbox1 in form1, I have another class called ClassDemo(), this class does not have a form. I have one method in this class, called HelloWorld() inside hello world I have one string val variable.
I want to pass the textbox1 value to ClassDemo's variable when the HelloWorld(), method is called.
How can I do that?
You can try multiple ways here.
Make the method as parameterized method. Put a string parameter inside the method declaration and then whenever you call that method, pass that string value that you want to pass.
Store the string value in global variable or Session(only if needed). This wasy, your value can flow across the application and can be accessed in any area wherever its needed.
Pass by Value or Pass by reference. You can opt for either one in C# to pass variables/values across connected areas.
Hope this clears the idea.
Related
I'm really confused by now.
I've got a WinForm which holds a large Array
int[,] map = new int[1000, 1000];
I've also got a class containing a method "Draw".
The draw method now needs to get the value of a position in the array in the form. I tried doing the following thing:
In my Form class, I added
public int mapContentAtXY(Point mapPosition)
{
return map[mapPosition.X, mapPosition.Y];
}
Now if I try to perform
myInt = Ingame.mapContentAtXY(myPoint);
//Note: Ingame is the name of my Form
It says
Error 2 'Neu.Ingame' does not contain a definition for 'mapAtPositionXY'
This is really confusing, I just added that definition, it's also set as public. So why the hell doesn't it work?
You will need to pass the instance of the Form to the draw-class by using this when you create or call that instance of the draw-class. Then you can access that method through that form instance.
OR
Store the map not in the form, but in that draw-class. Move the mapContentAtXY method to the draw-class. Use the one instance of the draw-class in your form to update that map.
Your method mapContentAtXY is an instance method.
Change Ingame to this, if you're calling it from within your form instance.
myInt = this.mapContentAtXY(myPoint);
Otherwise use the form instance
myInt = frmInGameInstance.mapContentAtXY(myPoint);
We have a Gridview, which gets filled via a DataSource. The datasource's SelectMethod is a function in the BL class, which calls a Stored Procedure that returns 2 Result-sets. The first result set is supposed to fill the gridview, so it's returned via the "return" keyword, and that works fine. The second result set is for later use (we're not separating them into 2 SP's because the query applies the same logic to get both result sets, and it's a long logic, so we don't want to run it twice). Our question is how to save the second result-set for the later use.
We initially thought of creating a global member in the BL class, and saving the result-set to it. But the problem is that when we need to use the second result-set, we're not in the same class where the gridview is, and that other class creates an instance of its own of the BL class, and of course that object doesn't have the data (the data is in the object created by the aspx page in which the gridview resides).
So then we thought of passing an "out" parameter the datasource's SelectMethod, and the method would save the second result set to that out parameter. But that's not working. I think we don't know how to tell the method that a parameter is "out". The way we pass the other parameters is by defining a method that is attached to the OnSelecting event, and then we pass them so:
e.InputParameters["schoolCode"] = currentSchoolCode;
e.InputParameters["levelCode"] = currentLayer;
Etc. However, we don't know how to tell the method that a parameter is "out". When we googled a little, all we could find was how to tell the method if a parameter is an output parameter - that is, passed to the SP as an output parameter. But that is not the case here.
So we will be very grateful if you help us find the solution for:
"Telling" the method that a certain parameter is "out", in order to get the value into it.
thanks.
Hmm... not quite understand your concern, but, how about this? pass your dataset to presentation layer and then:
dataset.Tables(0)
dataset.Tables(1)
or
DataReader.NextResult()
i have a problem, the functionality I'm looking for exactly is:
I have a grid and datagrid, according to the line to select the datagrid there will be to introduce a user control or other user controls are different pictures I've made polylinesegments, bezier cuadratic ... to introduce the call will name, which build on a string, but I have no way to call it correctly.
This is what I do and it works by putting the full name:
d48.Children.Add(new tratsPintados.end148());
But put the string, tells me not find the path in the project, what I want is to find the path inside the string.
d48.Children.Add(new thestring());
Any ideas?
If you need to instantiate some class based on its name (without real reference), you will need to use Reflection.
Maybe you can do some lookup by name for the class you need, and then use Activator.CreateInstance to call its default constructor.
I hope this is what you want, the question text is quite confusing to me.
using System.Reflection;
public object GetObjectFromString()
{
string objectName = "WpfApplication1.uc1";
Type newType = Type.GetType(objectName, true, true);
object o = Activator.CreateInstance(newType);
// do what you want with the 'o' variable, maybe cast it to the type you want.
}
I have inherited a small windows form based program written in C# that uses a 'constant' (See below), I need to modify this program so 'PROPERTY_NAME' can be "jobs" and "careers".
private const string PROPERTY_NAME = "jobs";
I'm guessing a constant isn't designed to change so should I need to change this. The line above is set once at the top of a class file and then PROPERTY_NAME is used throughout that file.
On the main form I would like to add two radio buttons 1 called 'jobs' and one called 'careers' and then change the PROPERTY_NAME in the class file based on which is selected. Would I need to pass the radio button status to the method in the class file? I recall reading that I can't simply read the radio button value from the class file.
Many thanks for your advice.
Jane
My best (and simplest) guess (I could elaborate into cleaner things but this is just for speed) without seeing any other part of the code would be to remove the const and add readonly so PROPERTY_NAME is just a plain old class member variable that cannot change outside of the constructor.
In the class's constructor, take in a string parameter, and have the code that creates an instance of this class pass in either "jobs" or "careers" (coming from the selected radio button probably) and set the PROPERTY_NAME variable.
EDIT:
Like Sasha says, another way would be using an enum but it depends what exactly is being done with PROPERTY_NAME as to whether this is appropriate for your application.
If the quantity you are representing changes ever throughout the history of the universe then do not make it a constant. Constants are things like the number of eggs in a dozen or the atomic weight of lead. Things like version numbers or the current price of gold change over time and therefore are not constant. Only make actually constant values into constant fields. The compiler will treat constant fields as constant for all time, which can introduce semantic errors if they change.
make an enum (my preferred way) and make it a readonly property. Set this property in the constructor. It isn't changeable after creation and should do what you need.
-sa
You cannot make constant having two values. It looks like that you need to make a field storing current property name and use it allover your form. And you will be able to init such field from the radio button.
How can I update and get values in a Windows Forms application while moving one form to other form (like cookies)?
I need to update the values to some variable and again I am going to refer stored values and need to do some calculations.
I have used cookies in ASP.NET but I am not able to find out the same concept in .NET Windows Forms (C#).
How can these issues be resolves?
You can use object references.
You can decalre a read/write Property for each variable you want to be available in another form and the use them for sharing your data.
One way to do this is to declare variables to be public, either in a global module or in any form.
public x as double
If it is declared in a module, you can access it with the variable name only. To access data declared in another form, use that form name with the variable: form1.x = 7
Another way is to declare a property in a form or other class.
A really simple way of getting cookie-like functionality would be to declare a static string dictionary in Program (Program.cs)
public static System.Collections.Specialized.StringDictionary SortOfLikeCookies = new System.Collections.Specialized.StringDictionary(); and read/write string values using Program.SortOfLikeCookies["Name"] = "Value";