c# cannot access textboxes from same class - c#

i have two separate questions; however, since they are very similar i will ask them in one posting.
what is the reason i cannot reference the textboxes from here? i created another file in my project and put
namespace EnterData.DataEntry
{
public partial class WebForm1 : System.Web.UI.Page
{
to make it go into the same namespace and partial class as the webform. but i cannot access the textbox!
public partial class WebForm1 : System.Web.UI.Page
{
public class LOMDLL.Main_Lom_Form PopulateMainForm()
{
//populate class
LOMDLL.Main_Lom_Form TheForm = new LOMDLL.Main_Lom_Form();
try
{
TheForm.lom_number = lom_numberTextBox.Text.ToInt();
TheForm.identified_by = identified_byTextBox.Text;
TheForm.occurrence_date = occurrence_dateTextBox.Text.ToDateTime();
//TheForm.pre_contact = pre_contactTextBox.Text; //need to create this texdtbox
//TheForm.pre_practice_code = pre_practice_codeTextBox.Text; //create this
TheForm.report_by = report_byTextBox.Text;
TheForm.report_date = report_dateTextBox.Text.ToDateTime();
TheForm.section_c_comments = section_c_commentsTextBox.Text;
TheForm.section_c_issue_error_identified_by = section_c_issue_error_identified_byTextBox.Text;
TheForm.section_d_investigation = section_d_investigationTextBox.Text;
TheForm.section_e_corrective_action = section_e_corrective_actionTextBox.Text;
TheForm.section_f_comments = section_f_commentsTextBox.Text;
}
catch (Exception e)
{
}
i get this error:
Error 20 Cannot access a non-static member of outer type 'EnterData.DataEntry.WebForm1' via nested type 'EnterData.DataEntry.WebForm1.LOMDLL' C:\Documents and Settings\agordon\My Documents\Visual Studio 2008\Projects\lomdb\EnterData\DataEntry\DAL.cs 68 38 EnterData
on all textboxes
what is the reason that i cannot access the textboxes from here?

Did you mean to nest classes here? If you intended to declare a method that returns a Main_Lom_Form(), try this:
public LOMDLL.Main_Lom_Form PopulateMainForm()
If you intended to call that method against a member of WebForm1 called TheForm, then instantiate that outside of the call to PopulateMainForm:
LOMDLL.Main_Lom_Form TheForm = new LOMDLL.Main_Lom_Form();
public void PopulateMainForm()
{
// snip
}

here there is an error:
public class LOMDLL.Main_Lom_Form PopulateMainForm()
are you declaring this class inside the webform class? anyway the class declaration is wrong.
for the last point of your question, when you have this:
public static class Main_Lom_Form
imagining you moved out from the other webform class, what is the namespace you have around it (before)? Just play around moving the class inside same namespace than class WebForm1 and you will not need to put the namespace before the class name, you will be able to create it like this:
var obj = new Main_Lom_Form();
in case it would make sense, but I doubt ;-)

I think you have a few issues with your code. The first one that pops out is this:
public class LOMDLL.Main_Lom_Form PopulateMainForm()
That is not a valid line of C# code. I'm assuming you actually meant to write:
public LOMDLL.Main_Lom_Form PopulateMainForm()
Secondly, if you have defined Main_Lom_Form as static, you can't instantiate it, it's a static class. You can address this:
public class Main_Lom_Form
I think its a combination of the above two issues which is causing the compiler to have a stroke.

Related

c# Calling method in partial call from another class

I'm making c# app where I need to access method in partial class from another class.
To be more specific I want to add items to listview from class that is different from partial class but in same namespace.
I tried like this:
public partial class Aa : Form
{
public static void UpdateListView(string[] array)
{
if (ProcessNetUsageList.InvokeRequired)
{
ProcessNetUsageList.Invoke(new Action<string[]>(UpdateListView), array);
}
else
{
ListViewItem item = new ListViewItem(array[0]);
for (int i = 1; i < 4; i++)
item.SubItems.Add(array[i]);
ProcessNetUsageList.Items.Add(item);
}
}
}
and then access it from another class like:
class Test
{
test()
{
ProgramName.Aa.UpdateListView(someArray);
}
}
But its giving an error because static method can only access static variables,and my listview isnt static(i created that listview in vs designer).
If i remove static keyword from method in partial class then i cant access it.I tried to create instance of partial class but without success.Any idea is welcome
note:Im using Invoke in my UpdateListView method because later that will be running on new thread
The nature of an object-oriented language is that objects don't have universal access to modify other objects. This is a good thing.
You've provided relatively little code so it's hard to provide a perfect answer here, but there are a few paradigms that resolve this issue.
One is to pass the instance to your test class, like this:
class Test
{
test(ProgramName.Aa form)
{
form.UpdateListView(someArray);
}
}
Or, if class test actually contains the ListView, you can pass that to a static method in Aa.
class Test
{
ListView someListView;
test()
{
ProgramName.Aa.UpdateListView(someListView, someArray);
}
}
Ultimately, you should think about the logical relationship between these objects to determine how these objects should communicate.
Remove the static keyword from UpdateListView, as you have done before. In test(), you need to instantiate Aa before you access UpdateListView.
Aa temp = new Aa()
You can then access the method by using
temp.UpdateListView(someArray);

c#, How can I read a ComboBox value in a different class than Form1

I have a ComboBox in my Window application
namespace MyProject
{
public partial class Form1 : Form1
{
Now I want to use the selected value in my ComboBox in my HelpClass
namespace MyProject
{
class HelpClass
{
Something like this, but this will not work but you get the idea I hope.
string var;
var = comboBox1.Text;
Anyone know how I can do this?
You shouldn't, only the UI class should access it's controls.
You could however pass the value from the textbox into your helper class. Possibly via the constructor,
var helper = new HelpClass(comboBox1.Text);
Your helper class would look like this
namespace MyProject
{
class HelpClass
{
private string textboxValue;
publuc HelpClass(string value)
{
this.textboxValue = value;
}
...
Then you can use this.textboxValue where ever you need the text in your helper class.
Make your ComboBox public in Form1 class
You can use a property with a public getter to allow access to the data, without necessarily allowing direct access to the control.
namespace MyProject
{
public partial class Form1 : Form1
{
public string VarSomething
{
get{ return comboBox1.Text; }
}
...
Probably read a bit about "properties" and "getters" and "setters" if you're not already familiar. Also naming a variable "var" will probably be confusing if you use the keyword anywhere.

No constructors defined

I have some code base which has is calling the following:
SetHazardDataService();
namespace Analytics.Foo.DataServices
{
class HDB:IDataService
{
}
}
With a member function declared in another class/file
using Analytics.Foo.DataServices
public void MyDataService()
{
var DbDataSvc = new HDB();
}
originally, I see the same definition used elsewhere but with (no idea if that works):
protected void MyDataService()
I included the public method in my class
I'm now trying to recreate that functionality, but I get the following issue:
The type Analytics.Foo.DataServices.HDB' has no constructors defined
I'm not sure what the issue is - any suggestions for why this is the case. There is no constructor that I can see. Plus I'm not able to see the other code working/but it doesn't give the same issue.
You need to create a constructor to class HDB, like this:
namespace Analytics.Foo.DataServices
{
class HDB:IDataService
{
public HDB()
{
}
}
}

.Net - How to create UserControl that implements an interface? Error with LoadControl

I have a number of UserControls that I want to each have some basic functionality. Below is my implementation :
public interface IMyTabInterface
{
void LoadData();
}
public partial class MyFirstTab : System.Web.UI.UserControl, IMyTabInterface
{
public void LoadData()
{
...
}
}
Then, in another page's code behind, I try :
protected void LoadFirstTab()
{
Control uControl1 = Page.LoadControl("/Controls/MyFirstTab.ascx");
Control uControl2 = Page.LoadControl("/Controls/MySecondTab.ascx");
// Want to do something like this, but don't get this far... :
var myFirstTab = (IMyTabInterface)uControl1;
var mySecondTab = (IMyTabInterface)uControl2;
myFirstTab.LoadData();
mySecondTab.LoadData();
Panel1.Controls.Add(myFirstTab);
Panel1.Controls.Add(mySecondTab);
}
I know much of this is not correct yet, but I am getting an error on the LoadControl() line :
'MyFirstTab' is not allowed here because it does not extend class 'System.Web.UI.UserControl'
even though the class clearly extends the UserControl class. Whats going on here? Is there a better way to do this?
This will work:
var myFirstTab = (IMyTabInterface)uControl1;
var mySecondTab = (IMyTabInterface)uControl2;
myFirstTab.LoadData();
mySecondTab.LoadData();
Panel1.Controls.Add((Control)myFirstTab);
Panel1.Controls.Add((Control)mySecondTab);
What is going on?
Your myFirstTab and mySecondTab variables are of type IMyTabInterface, since this is what you declared them as (this is what allows you to call LoadData on them).
However, in order to add an item to the controls collection, these need to be of type Control (or one that inherits from it) - this is achieved by the cast.
Another option:
var myFirstTab = (IMyTabInterface)uControl1;
var mySecondTab = (IMyTabInterface)uControl2;
myFirstTab.LoadData();
mySecondTab.LoadData();
Panel1.Controls.Add(uControl1);
Panel1.Controls.Add(uControl2);
Here you use the original Control types you started with, so no need to cast.

C# - Adding to an existing (generated) constructor

I have a constructor that is in generated code. I don't want to change the generated code (cause it would get overwritten when I regenerate), but I need to add some functionality to the constructor.
Here is some example code:
// Generated file
public partial class MyGeneratedClass
{
public MyGeneratedClass()
{
Does some generated stuff
}
}
The only solution I can come up with is this:
// My hand made file
public partial class MyGeneratedClass
{
public MyGeneratedClass(bool useOtherConstructor):this()
{
do my added functinallity
}
}
I am fairly sure this will work, but I then have a lame unused param to my constructors and I have to go change them all. Is there a better way? If not that is fine, but I thought I would ask.
If you're using C# 3 and can change the generator, you can use partial methods:
// MyGeneratedClass.Generated.cs
public partial class MyGeneratedClass
{
public MyGeneratedClass()
{
// Does some generated stuff
OnConstructorEnd();
}
partial void OnConstructorEnd();
}
// MyGeneratedClass.cs
public partial class MyGeneratedClass
{
partial void OnConstructorEnd()
{
// Do stuff here
}
}
Would your environment allow you to inherit from MyGeneratedClass rather than have it as a partial class. You could then override the constructor?
Assuming you can't change the generator output, unfortunately, your options are a bit limited, and not ideal considering what you're looking for. They are:
Inherit from the generated class. The child class will implicitly call the parent's construtor.
Use a static method as an initializer

Categories