Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

  • 3 weeks later...
Posted

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?

Posted
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.
"Nobody knows what I do until I stop doing it."
Posted

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

Posted

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?

Posted (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 by Drizzt109
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

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.

rustyd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...