I never made android or apps before so this is new. I picked C# because I have coded in that language before. Here is goes.
I created a view that where the user can enter a fullname, email, and phone number. All 3 are strings. The problem I have is that even when I coded before. I hated using classes. They suck. I don't know if this forum has an option for me to highlight code. Here's some code.
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your application here // Set our view from the "main" layout resource SetContentView (Resource.Layout.addContact); Button btnSaveContact = FindViewById<Button> (Resource.Id.btnSaveContact); btnSaveContact.Click += delegate { DBContact newContact = new DBContact(); //I can't seem to run this class. ? newContact.SaveContact(Contacts); };
Here you can see what I'm trying to do and it doesn't seem to want to work. I have a botton on this view. "btnSaveContact". when the user clicks this button I need to to save the info from the 3 text boxes.
here's the class that I thought would save it. and well like I said. This doesn't work. I know it's been a while sense I coded. It shouldn't be this hard.
public static void SaveContact (Contacts contact)
{
using (var conn = GetConnection ()) {
conn.Open ();
using (var cmd = conn.CreateCommand ()) { if (contact.Id < 0) { // Do an insert cmd.CommandText = "INSERT INTO CONTACTS (fullname, email, phone) VALUES (@fullname, @email, @phone); SELECT last_insert_rowid();"; cmd.Parameters.AddWithValue ("@fullname", contact.Fullname); cmd.Parameters.AddWithValue ("@email", contact.Email); cmd.Parameters.AddWithValue ("@phone", contact.Phone); contact.Id = (long)cmd.ExecuteScalar (); } else { // Do an update cmd.CommandText = "UPDATE CONTACTS SET fullname = @fullname, email = @email, phone = @phone WHERE Id = @Id"; cmd.Parameters.AddWithValue ("@fullname", contact.Fullname); cmd.Parameters.AddWithValue ("@email", contact.Email); cmd.Parameters.AddWithValue ("@phone", contact.Phone); cmd.ExecuteNonQuery (); } } }
I have 2 other methods that I use for creating the SQLite database. If you need to see them all so. not a problem. let me know
Posts
I didn't see your "Contacts" variable in the first code snippet.
In this form i think you want to give a class to the method. This standalone form is allowed only by define:
Contacts contact;
If you really want to get this type you can use the typeof operator, for example by starting activity:
StartActivity(typeof(Activity2));
What you need to create a class the contains your "info"?
Fine then you need a class
`class Contact
{
string fullName;
string email;
string phone;
}`
The class is finished. Then you need the information from the layout and send it as a parameter to the method:
`string fullName = FindViewById(Resource.Id.etFullName).Text;
string email = FindViewById(Resource.Id.etEmail).Text;
string phone = FindViewById(Resource.Id.etPhone).Text;
Contact contact = new Contact(fullName, email, phone);
DBContact newContact = new DBContact(); //I can't seem to run this class. ?
newContact.SaveContact(contact);`
In your method you can get your infos through the properties.
Sorry, Here's my class that I use.
class Contacts
{
public long Id { get; set; }
public string Fullname { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
This is the class that I use. Like I said the problem is that when I compile. I get the same error.
here's one
C:\Users\Joe Moody\Documents\Projects\ContactManager\ContactManager\addContact.cs(5,5): Error CS0176: Member 'ContactManager.DBContact.SaveContact(ContactManager.Contacts)' cannot be accessed with an instance reference; qualify it with a type name instead (CS0176) (ContactManager)
and the last one
C:\Users\Joe Moody\Documents\Projects\ContactManager\ContactManager\addContact.cs(28,28): Error CS0118: 'ContactManager.Contacts' is a 'type' but is used like a 'variable' (CS0118) (ContactManager)
I didn't think to post the error messages. See the idea is when the user clicks the button. Then the class should run and save the info.
Like this..
DBContact newContact = new DBContact();
newContact.SaveContact(Contacts);
Let me know
Ok but you use the Contacts as a type, not like a variable.
Contacts Contacts = new Contacts(0, "Kevin Bacon", "[email protected]", "555-310"); //or Contacts Contacts = new Contacts(); // default settings DbContact newContact = new DBContact(); newContat.SaveContact(Contacts);
You define your SaveContact() method as static. But you create an instance from DBContact class. This is the another problem.
I screenshoted my code snippet.
Thanks Cortez, Let me try that and see what happens
Sorry, I still get an error. Some new errors.
here's one
C:\Users\Joe Moody\Documents\Projects\ContactManager\ContactManager\addContact.cs(59,59): Error CS1061: 'Android.Views.View' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'Android.Views.View' could be found (are you missing a using directive or an assembly reference?) (CS1061) (ContactManager)
C:\Users\Joe Moody\Documents\Projects\ContactManager\ContactManager\addContact.cs(5,5): Error CS0176: Member 'ContactManager.DBContact.SaveContact(ContactManager.Contacts)' cannot be accessed with an instance reference; qualify it with a type name instead (CS0176) (ContactManager)
Your first error you receive because the compiler doesn't find the Text property. Have you casted it? Or is it an EditText really by Id?
The second error i explained it more times: to the SaveContact method you give a class, not a variable or instance. Can you show this two code snippet with the references?
Your 1st question is I have no idea, sense I never heard that term before. and as for the 2nd question. I need to know what parts of the code you need to see. Any way let me paste what I have.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Mono.Data.Sqlite;
namespace ContactManager
{
[Activity (Label = "AddContact")]
public class AddContact : Activity
{
}
The above code in on the addContact view
here is the class.
using System;
namespace ContactManager
{
class Contacts
{
public long Id { get; set; }
public string Fullname { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
And last here is the code for the save class
public static void SaveContact (Contacts contact)
{
using (var conn = GetConnection ()) {
conn.Open ();
The last code is in a class called Contacts. and the code below is just the code for saving the entry.
private static string db_file = "contacts.db3";
Thanks for your help.
My first question is ? What do you heard before? Cast or casting. It is very simple. With my english i cannot explain it exactly but in your FindViewById you do this:
Button btnSaveContact = FindViewById<Button> (Resource.Id.btnSaveContact); string fullName = FindViewById (Resource.Id.etFullname).Text;
In the first row you casting your View to Button, with . In the second row, and this is your first exception, you have only a view. If you casting it to EditText the .Text property will be available. You know that with this Id(etFullname) you get an EditText but you need to cast it to EditText that the compiler know about it to. If you casting it to another type but is it not compatible you get an exception.
But it is working. Then you need to complete these rows:
string fullName = FindViewById (Resource.Id.etFullname).Text;
This code is ok now:
btnSaveContact.Click += delegate { Contacts contact = new Contacts(-1, fullName, email, phone); DBContact newContact = new DBContact(); newContact.SaveContact(contact); };
But from your another code snippet(third) you set the SaveContact as static:
public static void SaveContact
In this case you need to call this method like this:
Contacts contact = new Contacts(-1, fullName, email, phone); DBContact.SaveContact(contact);
Ahh i hate this complex forum. So you can cast your view to button with
<Button>
in the first row. After then in the second row you should casting your EditText with<EditText>
.Thanks Cortez, All you had to say was change this
Button btnSaveContact = FindViewById<Button> (Resource.Id.btnSaveContact); string fullName = FindViewById (Resource.Id.etFullname).Text;
Into this to fix the 1st Problem.
Button btnSaveContact = FindViewById<Button> (Resource.Id.btnSaveContact); string fullName = FindViewById <EditText>(Resource.Id.etFullname).Text;
and this would fix that error. How ever I'm still getting the 2nd error.
C:\Users\Joe Moody\Documents\Projects\ContactManager\ContactManager\addContact.cs(5,5): Error CS0176: Member 'ContactManager.DBContact.SaveContact(ContactManager.Contacts)' cannot be accessed with an instance reference; qualify it with a type name instead (CS0176) (ContactManager)
and this is how I call it.
`
btnSaveContact.Click += delegate {
`
Joe, you drive me crazy
. As i suggested you at the previous post: you defined your SaveContact() method as static:
public static void SaveContact
You should call it the correct mode:
btnSaveContact.Click += delegate { Contacts contact = new Contacts(-1, fullName, email, phone); //DBContact newContact = new DBContact(); //newContact.SaveContact(contact); DBContact.SaveContact(contact); // correct way };
You dont need an instance when you specified your method as static.
Cortez, I'm sorry. because I'm blind in my right eye. When I read I read over words. So you code I didn't even see. Thank you it. Works.
I think your right. Xamarin needs some thing better then Wordpress. This PHP code sucks. I never liked Wordpress. I mean the forums section that we are in. It just sucks. 1/2 the time it doesn't work. and that includes adding code to our posts.
Ohh Sorry Joe. I didn't know that your right eye not working. Sorry for that. I am just wondering why you don't just copy paste the code and test it.
I have never programming in PHP. As i remember only once for an exam in the university. But i didn't understand anything. I like the Xamarin. I could write codes in Eclipse too in Java, but Xamarin Studio is much comfortable and simplier for me, because i wrote codes in C# before.
I happy to hear your code works.
Best Regards.
That's no problem, You didn't know about my health issues.
I have done coding in PHP. and it's ok. I guess.
The reason why I'm using Xamarin Studio is that I only have the VS Express installed on this PC and so I can Use Xamarin in Visual Studio.
See I have done coding in C# and some in C++. I couldn't under stand C++. So I started C# and like the code better. Some of it was easy to under stand.
and Way I'm trying to set up a try statement in side my button. So that if the save contact fails. It will show the result on a label.
Thanks Cortez, and take care
Yeah this is my first reason why i installed Xamarin Studio. For documentation i need to show a code snippet. VS i cannot get, but the Xamarin Studio i could downloaded and installed easly. And yeah i can write codes in console application
.
I have done both, Code in C# in console and Windows Form.
mmm... sorry but when im using SqliteConnection getConnection()... i have an error which says the following:
Type 'System.Data.Common.DbConnection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089'. (CS0012) ("nameofclass")
I would thank ur help..
If you type SqliterConnection and you right click on it have you got a "Resolve" element on the top? Or just type in at the top: using Mono.Data.Sqlite. I cannot find the System.Data. I added the System.Data to the References folder.