cpopham Posted June 4, 2004 Posted June 4, 2004 I am creating a two tier application for database connectivity. I have all of my connections, dataadapters, etc in a single class named ReqTrakData (ReqTrakData.vb) On a form a user makes a selection from a combo box. I save this selection to a Public variable (mstrSelectedNum) which has been declared at the beginning of the form. Now I want to able to use this variable in a function in my ReqTrakData class. How can I use this vairable in my ReqTrakData class? Chester Quote ____________________________________________ http://www.pophamcafe.com I am starting a developers section, more tutorials than anything.
Administrators PlausiblyDamp Posted June 4, 2004 Administrators Posted June 4, 2004 http://www.xtremedotnettalk.com/showthread.php?t=83092 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
IxiRancid Posted June 21, 2004 Posted June 21, 2004 Ok... many of you link this article, but has anyone tried the thing? I tried it, but passing the whole form (Me) doesn't work, the error is somewhat like "too many parameters in As New statement". Any idea? Quote
Rick_Fla Posted June 21, 2004 Posted June 21, 2004 Yes, I have used this to pass variables and form objects to another form. You have to make sure that in the second class, that you create another Sub New and have it accept the calling class as a parameter. Quote "Nobody knows what I do until I stop doing it."
PWNettle Posted June 22, 2004 Posted June 22, 2004 I like the mechanism Bucky illustrates - passing in a parameter to the new object's constructor - but there is another way OOP-ish way that's less dependable. If you create a private variable in your 2nd form (the one you want to pass the value to) and "wrap" that variable with a public property accessor, you could then assign the value you want to pass when you instantiate your second form in your first form. The thing is, when you do it this way, you are not forced to set the value - you can instantiate the form and never set the property for the value that needs to be passed - so it's really not optimal OOP design. Objects should be self-protecting of their data. If your 2nd form *needs* this value that your passing then receiving it via the constructor is the more solid OOP way to do it - because a compile error will occur if you don't pass in the parameter. If the code setup in the link isn't working for you then you might want to reread it and follow the code examples very closely to make sure you're setting up your code the same as the example. Passing values via constructor parameters is common and it does work (nicely, I might add). It's interesting, in "old" VB we never had constructors and it kinda sucked - you either had to expose a propertly like I described above or go the route of making a public variable (ick!) - but I know long-time VB programmers who shy away from utilizing the power of constructors in .Net just because they're not used to them. One thing I personally don't like is how VB.Net does some of the OOP features. In C# (which is my .Net language of choice) constructors are obvious and have typical OOP constructor syntax - in VB.Net the syntax is kind of wacky, nonintuitive, and somewhat misleading - I think it's harder to grasp at a glance than typical constructor syntax. Anyways... Paul Quote
Drizzt109 Posted June 28, 2004 Posted June 28, 2004 does this work for C#? I've tried passing the whole form (which cotnains the main method) into another class so that it can manipulate the form, but can't get that to work. Any ideas? Quote
Administrators PlausiblyDamp Posted June 29, 2004 Administrators Posted June 29, 2004 (edited) When you say doesn't work - could you be a bit more specific? Also any chance you could post the relevant code? Edited March 16, 2007 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Drizzt109 Posted June 30, 2004 Posted June 30, 2004 (edited) Code Ok, but I guess the wierd thing is how C# uses memory. Does it copy things or point. It will point to a form object (such as a datagrid), but copies entire forms? Does this make sense, because that seems to be what's happening. I set a form object to the form I pass in, but it doesn't have any of the objects in the form (textboxes, datagrid, etc.), which seems like its making a copy or is an entirely new form. I'm not sure which. private void Form1_Load(object sender, System.EventArgs e) { InitializeCombos(); t.Elapsed+=new System.Timers.ElapsedEventHandler(ChangeTextBox); t.AutoReset = true; t.Enabled = true; CreateGrid(); rt = new Realtick.RealtickClass(); rt.BalanceUpdate = 1; rt.TradeUpdate = 1; order = new Order(rt, this); } using System; using System.Windows.Forms; using Realtick; using REALTICKXLib; namespace TryWithTimer2 { /// <summary> /// Summary description for OrderEntry. /// </summary> public class Order { private string saccount; private string sbuysell; private string ssymbol; private int lvol; private int ishowvol; private string sroute; private double dprice; private Realtick.Realtick rt; static int iRow; private Form form; public Order(Realtick.Realtick RealT, Form Display) { rt = RealT; form = Display; rt.OnNewTradeEx +=new IRealTickEvent_OnNewTradeExEventHandler(rt_OnNewTradeEx); } public string sAccount { get { return saccount; } set { saccount = value; } } public int OrderRow { get { return iRow; } } public string sBuySell { get { return sbuysell; } set { sbuysell = value; } } public string sSymbol { get { return ssymbol; } set { ssymbol = value; } } public int lVol { get { return lvol; } set { lvol = value; } } public int iShowVol { get { return ishowvol; } set { ishowvol = value; } } public double dPrice { get { return dprice; } set { dprice = value; } } public string sRoute { get { return sroute; } set { sroute = value; } } } } } } Edited June 30, 2004 by Drizzt109 Quote
Administrators PlausiblyDamp Posted June 30, 2004 Administrators Posted June 30, 2004 if you wish to have access to the features of a particular form then either declare the parameter as the form type (probably the best way) or within the code cast the form to the correct type. As to Does it copy things or point it depends on the object type - if it is a class then it's passed by reference, if a struct then a copy is passed. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Drizzt109 Posted June 30, 2004 Posted June 30, 2004 thanks, i pass the form as a form type in the constructor, but i still don't have access to any of the objects on the form. If it is referencing my form then it should give me access to the textboxes etc. on the form. if you wish to have access to the features of a particular form then either declare the parameter as the form type (probably the best way) or within the code cast the form to the correct type. As to it depends on the object type - if it is a class then it's passed by reference, if a struct then a copy is passed. Quote
rustyd Posted June 30, 2004 Posted June 30, 2004 Change how the objects on the form are declared I ran into this. I could access the form, but I could not access the textboxes or labels on the form. Expand the Windows Form Designer generated code region. Locate the specific objects you need to reference. Changing their declaration from Friends to Public made them visible in other classes. Quote rustyd
Drizzt109 Posted June 30, 2004 Posted June 30, 2004 thanks, that works in VB.NET but not in C# for some reason... Quote
Administrators PlausiblyDamp Posted July 1, 2004 Administrators Posted July 1, 2004 You need to declare the constructor to accept the correct kind of form variable: public Order(Realtick.Realtick RealT, Form1 Display) //Form1 rather than form Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Drizzt109 Posted July 2, 2004 Posted July 2, 2004 Thank You That worked, hallelujah. Thanks so much. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.