Tworzenie bazy danych

0

Cześć,

Troszkę się pogubiłem w konstruktorach, listach i klasach. Chciałem utworzyć konstruktor z 9-cioma polami. Imię, nazwisko,adres....kraj pochodzenia.

Więc go utworzyłem.

    public class studentsInformationSheet
    {

        public string firstName { get; set; }
        public string lastName { get; set; }
        public DateTime birthdate { get; set; }
        public string addressLine1 { get; set; }
        public string addressLine2 { get; set; }
        public string City { get; set; }
        public string stateOrProvince { get; set; }
        public string zipOrPostal { get; set; }
        public string Country { get; set; }




    } 

Następnie w Main chciałem przykładowo dodać jeden wpis i go wyświetlić:


 List<studentsInformationSheet> studentsInformations = new List<studentsInformationSheet>();
         //   GetStudentInformation(); 
            studentsInformations.Add("John","Smith","12.12.1222","Zachlapana","5","Warsaw","Mazowieckie","90210","Poland");
            foreach(studentsInformationSheet si in studentsInformations){
                
                
                Console.WriteLine("First Name = {0}, Last Name = {1}", si.firstName, si.lastName);

            }

Aczkolwiek napotkałem na błąd:

Error 1 No overload for method 'Add' takes 9 arguments C:\Users\AdolfHitler\Documents\Visual Studio 2013\Projects\ConsoleApplication5\ConsoleApplication5\Program.cs

Za pomoc dziękuję

0

Nie utworzyłeś nigdzie konstruktora, który ma przyjmować 9 parametrów, więc istnieje tylko konstruktor domyślny.

To co zrobiłeś to klasa posiadająca 9 właściwości.

1

Witam,

I dobrze Ci podeja, skąd operator który spodziewa się twojego typu ma wiedzieć jak go zbudować jak podajesz mu ciąg "swoich" miennych zamiast jedną twojego typu. No i generalnie zapomniałeś o konstruktorze

var list = new List<TwojTyp>();
var itemTwojegoTypu = new TwojTyp("var1", "var2");
list.Add(itemTwojegoTypu);

A bez konstruktora możesz to zrobić mniej więcej tak

var list = new List<TwojTyp>();
var itemTwojegoTypu = new TwojTyp{
firstName ="John",
lastName ="Smith",
birthdate ="12.12.1222",
addressLine1 ="Zachlapana",
addressLine2 ="5",
City ="Warsaw",
stateOrProvince ="Mazowieckie",
zipOrPostal ="90210",
Country ="Poland"};
list.Add(itemTwojegoTypu);

Pozdrawiam,

mr-owl

0
mr-owl napisał(a):

Witam,

I dobrze Ci podeja, skąd operator który spodziewa się twojego typu ma wiedzieć jak go zbudować jak podajesz mu ciąg "swoich" miennych zamiast jedną twojego typu. No i generalnie zapomniałeś o konstruktorze

var list = new List<TwojTyp>();
var itemTwojegoTypu = new TwojTyp("var1", "var2");
list.Add(itemTwojegoTypu);

[...]

Wklejając ten kod mam error:

Error 1 The type or namespace name 'TwojTyp' could not be found (are you missing a using directive or an assembly reference?) C:\Users\AdolfHitler\Documents\Visual Studio 2013\Projects\ConsoleApplication5\ConsoleApplication5\Program.cs

Wszędzie w tutorialach mam normalne listy typu string, int ale tych nie mogę znaleźć - wówczas już bym nie pytał.

0

The type or namespace name 'TwojTyp' could not be found
I ten komunikat na prawdę nic Ci nie mówi?

0
some_ONE napisał(a):

The type or namespace name 'TwojTyp' could not be found
I ten komunikat na prawdę nic Ci nie mówi?

Mówi mi, że nie może odnaleźć namespace albo typu. Typ ustalam np

string TwojTyp;

ale nie pomaga. A namespace to przestrzeń nazw ale dodając <Program.TwojTyp>

 nie pomaga.
0

Witam,

Może http://www.centrumxp.pl/dotNet/312,06-Klasy-i-obiekty.aspx na coś cię naprowadzi...

Pozdrawiam,

mr-owl

0

Lista musi przechowywać obiekty określonego typu.

Zamiast tego "TwojTyp" co @mr-owl podał w przykładowym kodzie to musisz po prostu wpisać nazwę typu, który lista ma przechowywać, czyli.... studentsInformationSheet(btw. nazwa klasy jest zła, bo pojedyncza instancja przechowuje informację o JEDNYM studencie, a nie o studentach, ale już mniejsza o to).

Przypomnę jeszcze tylko, że w kodzie, który umieściłeś w pierwszym poście nie ma utworzonego konstruktora o którym pisałeś i pewnie zaraz będzie to powodować kolejne błędy.

0
using System;
using System.Collections.Generic;

namespace ConsoleApplication11
{
    public class studentsInformationSheet
    {
        
        public string firstName { get; set; }
        public string lastName { get; set; }
        public DateTime birthdate { get; set; }
        public string addressLine1 { get; set; }
        public string addressLine2 { get; set; }
        public string City { get; set; }
        public string stateOrProvince { get; set; }
        public string zipOrPostal { get; set; }
        public string Country { get; set; }
    } 
    class Program
    {
        
        static void Main(string[] args)
        {

            List<studentsInformationSheet> studentsInformations = new List<studentsInformationSheet>();
            //   GetStudentInformation(); 
            studentsInformations.Add(new studentsInformationSheet() {firstName="John", lastName="Smith", birthdate=new DateTime(2012,2,2), addressLine1="Zachlapana", addressLine2="5", City="Warsaw", stateOrProvince="Mazowieckie", zipOrPostal="90210", Country="Poland"});
            foreach (studentsInformationSheet si in studentsInformations)
            {
                Console.WriteLine("First Name = {0}, Last Name = {1}", si.firstName, si.lastName); 
            }
            Console.Read();
        }
    }
}

1

Witam,

Zobacz czy to jest jasne?

namespace ConsoleStudentInformationsApp
{
    using System;
    using System.Collections.Generic;

    public class StudentInformationsSheet
    {
        public string FirstName { get; private set; }

        public string LastName { get; private set; }

        public DateTime DayOfBirth { get; private set; }

        public string AddressLine1 { get; private set; }

        public string AddressLine2 { get; private set; }

        public string City { get; private set; }

        public string County { get; private set; }

        public string PostalCode { get; private set; }

        public string Country { get; private set; }

        public StudentInformationsSheet(
            string firstName,
            string lastName,
            DateTime dayOfBirth,
            string addressLine1,
            string addressLine2,
            string city,
            string county,
            string postalCode,
            string country)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.DayOfBirth = dayOfBirth;
            this.AddressLine1 = addressLine1;
            this.AddressLine2 = addressLine2;
            this.City = city;
            this.County = county;
            this.PostalCode = postalCode;
            this.Country = country;
        }

        public override string ToString()
        {
            return string.Format("First Name = {0}, Last Name = {1}", this.FirstName, this.LastName);
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var studentsInformations = new List<StudentInformationsSheet>();

            studentsInformations.Add(
                new StudentInformationsSheet(
                    "John",
                    "Smith",
                    new DateTime(2012, 2, 2),
                    "Zachlapana",
                    "5",
                    "Warsaw",
                    "Mazowieckie",
                    "90210",
                    "Poland"));
            foreach (var student in studentsInformations)
            {
                Console.WriteLine(student);
            }

            Console.WriteLine("Press any key to exit...");
            Console.Read();
        }
    }
}

Pozdrawiam,

mr-owl

0

Dobrą praktyką jest konstruktor ustawiający wartości i prywatne settery, czyli wersja @mr-owl. Publiczne settery zazwyczaj prowadzą do powstania jakiegoś syfu.

Całość wyglądałaby dużo lepiej, gdyby Adres był oddzielnym obiektem, wtedy nie trzeba byłoby 9 argumentów w konstruktorze, co też jest bardzo niefajne.

0

Jestem zachwycony pomocą. Jest szybka i profesjonalna. Z tym drugim u mnie ciężej i muszę chyba przejść do kursów paradoksalnie dużo bardziej zaawansowanych lub też do "przepisywania" tutoriali jak stworzyć grę(unity)/aplikację dość zaawansowaną aby się obyć w tym wszystkim gdyż jest to troszkę czarna magia.

Dziękuję, jutro rejestruje konto i postaram się poszerzać wiedzę i się nią dzielić tak jak daliście mi przykład!

1 użytkowników online, w tym zalogowanych: 0, gości: 1