Mitja Bonca 557 Nearly a Posting Maven

We are talking about structs now and your example, this is what you are asking:

SPoint sPoint1 = new SPoint();
 sPoint1.X = 999;
 SPoint sPoint2 = sPoint1;
Mitja Bonca 557 Nearly a Posting Maven

You did a mistake in your last statement, I thnik you had this in mind: "adding to a CLASS variable makes it a mistake".

Answer: (read my post carefully ones again from the beginning). Its because your variable "cPoints" it is only a reference to a class.


You have to do it like this:

namespace Jan11Struct
{
    class Program
    {
        static void Main(string[] args)
        {
            CPoint[] cPoints = new CPoint[10];
            SPoint[] sPoints = new SPoint[10];

            //cPoints[7].X = 20; //runtime error
            cPoints[7] = new CPoint(20);
            sPoints[7].X = 20;
            Console.WriteLine("Class value: {0} .... Struct value: {1}.", cPoints[7], sPoints[7].X);
            Console.ReadLine();
        }
    }
}

class CPoint
{
    public int X;
    public CPoint(int _X)
    {
        this.X = _X;
    }
}
struct SPoint
{
    public int X;
}

Hope this helps explaning the difference between the class and the struct.
Mitja

NewOrder commented: Now i understand +0
Mitja Bonca 557 Nearly a Posting Maven

A class is a reference type. When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.

Mitja Bonca 557 Nearly a Posting Maven

Momerath is right. your "myobject" is only a reference to in instance of a class "Class1" some info here.

Your 2nd example is called inheritance and polymosfism. You create one base class, and derived class(es) from it. Take a look at here.

Mitja Bonca 557 Nearly a Posting Maven

If so, then take jugosoft`s example, a bit modiffied. As simple as that. like:

'SELECT COUNT(*) FROM TableName WHERE Login = @login AND Password = @password'

This is your example, and its a good one, even there is no need to use a Password in the query.
, so you can only do:

'SELECT COUNT(*) FROM TableName WHERE Login = @login'
 //and Login is your UserName column in the db

You quited in your 1st post:
However this query always returns the same result no matter is the parameters exist or not.

If the userName passed to the parameter exist in the db, it will return 1, if the userName does NOT exist, it will (it has to) return 0.
If there is anything else then this, there is something wrong with your db creation.

I hope this helps explaining your issue.
Mitja

Mitja Bonca 557 Nearly a Posting Maven

so in this case you will always get zero, or at maximum one output - because you want to count how many users has "exactly this" name, and "exactly this" password.

I dont get it really what are you trying to count here. If All the code is good, there SHOULD have been only one person with name and password - if not, your code does not do a comparison when new user want to register (if the username already exists, the new users has to pick another one). Otherwise, having login with multiple users with the same userName is pointless.
What do you think?

Mitja Bonca 557 Nearly a Posting Maven

Try and let me know, but remember, when working wit dgv control, best "tool" for populating it, is a dataTable. A good example you have here.

vedro-compota commented: ++++++ +1
Mitja Bonca 557 Nearly a Posting Maven

Which array exactly? Or string[], ArrayList, generic List ?
And how many columns has your dgv?

Just for your info, for populating dgv, its best practice to use DataSet (or DataTable). Its specially designed for that, becuase you can create columns and rows, its actually a copy of dgv. In arrays you CANNOT have any columns, so populating its hard - or better said - useless.

vedro-compota commented: +++++++++ +1
Mitja Bonca 557 Nearly a Posting Maven

my from? Are you talking about form?
And I really dont understand what are you saying.
Look, where do you havea code of those loops? It is in Form1 (or in some other form, it doesnt matter).

What you have to do is the following:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Jan09Exercise
{
    public partial class Form1 : Form
    {
        delegate void MyDelegate(string msg);
        public Form1()
        {
            InitializeComponent();
            labelShow.Text = "";
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(DoingTimeConsumingStuff));
            thread.Start();
        }

        private void DoingTimeConsumingStuff()
        {
            for (decimal i = 0.0001M; i < 1000000000; i++)
            {
                UpdatingMyControl(i.ToString());
            }
        }

        private void UpdatingMyControl(string strValue)
        {
            if (this.labelShow.InvokeRequired)
                this.labelShow.Invoke(new MyDelegate(UpdatingMyControl), new object[] { strValue });
            else
                labelShow.Text = strValue;
        }      
    }
}

CAREFULY look into this example, which shows three important things:
1. creating a new thread
2. on this thread then runs a long time consuming loop (without a new created thread the form would be unresponsive
3. Use of delegates, which are required to update the control while threads are busy.

What you have to do is to accomodate this code to yours.

Hope it helps,
Mitja

Mitja Bonca 557 Nearly a Posting Maven

'select count(login) from mytable'

Mitja Bonca 557 Nearly a Posting Maven

When ever you are not sure how long is gonna be the array, you never use a "basic" arrays like: string[], int[], bool[], or what ever of these kind.
ddanbe has perfectly right, use a generic list, which is in my opinion my far the best type of array. If offers so many posiblilities to work with.

or you can even use an ListArray - for this you have to create a new namespace (System.IO)

Mitja

Mitja Bonca 557 Nearly a Posting Maven

The code above will never work, for many reasons:
1. as ddanbe said, C# needs Main() method to get started (so no Go() method - change the name to Main)
2. About _gameDirectory = FindGameDirectory() ? Where did you innisiate it? Its from no where now.
You cannot call in instance from outside of the static method, so you have to create a new one in the static Main method - where it all begins.
3. Whats this method: AddAsmToRootPatchFile(); ? Is it a static or now? If its not, it wont compile.

this for beginning
Mitja

Mitja Bonca 557 Nearly a Posting Maven

Yes, exactly. Main method as short as possible, and so the other methods as well. Its always good to use a particuar method only for one thing, and one thing only. You never know when you are gonna use it again (it happens many times, that you use a method many times during opertions).
Happy coding,
bye, bye
Mitja

Mitja Bonca 557 Nearly a Posting Maven

Please take both examples, in the upper post and this one in here.
The same example would be with using keyword "out":

static void Main(string[] args)
        {
            int value = 2;
            List<string> list;
            CreateList(out list, value);

            for (int i = 0; i < list.Count; i++)
                Console.WriteLine("{0}", list[i]);
            Console.ReadLine();
        }

        private static void CreateList(out List<string> list, int howMany) 
        {
            list = new List<string>();
            for (int i = 0; i < howMany; i++)
            {
                string sItem;
                Calculate(out sItem, i);
                list.Add(sItem);
            }
        }

        private static void Calculate(out string item, int i)
        {
            switch (i)
            {
                case 0:
                    {
                        item = "one";
                        break;
                    }
                case 1:
                    {
                        item = "two";
                        break;
                    }
                case 2:
                    {
                        item = "three";
                        break;
                    }
                default:
                    {
                        item = "none";
                        break;
                    }
            }
        }
NewOrder commented: Thank you +0
Mitja Bonca 557 Nearly a Posting Maven

Using of out method parameter its good too. You can use it if you want in your example, but its pointless to pass the parameter into the method, that will be returned as well. Mostly you pass parameters that you need in methods, and return those that you need in the previous method, like a simple example:

static void Main(string[] args)
        {
            int value = 2;
            List<string> list = CreateList(value);

            for (int i = 0; i < list.Count; i++)
                Console.WriteLine("{0}", list[i]);
            Console.ReadLine();
        }

        private static List<string> CreateList(int howMany)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < howMany; i++)
            {
                string sItem = Calculate(i);
                list.Add(sItem);
            }
            return list;
        }

        private static string Calculate(int i)
        {
            string item = null;
            switch (i)
            {
                case 0:
                    {
                        item = "one";
                        break;
                    }
                case 1:
                    {
                        item = "two";
                        break;
                    }
                case 2:
                    {
                        item = "three";
                        break;
                    }
                default:
                    {
                        item = "none";
                        break;
                    }
            }
            return item;
        }

I hope you get the point. SUM: you don`t need to pass the parameter into the method from which will be the same returned. Actually you can, but this is not the proper way if you get me. But if you want you still can do it, and use out method parameter.
Just that!

Mitja Bonca 557 Nearly a Posting Maven

The loops have so much work, that the form is not responding. Thats is whole the code is going on in one thread. There are two solutions, or to use BackGroundWorker, or you put the loops into another thread, so the main thread of the form will be free of that code, and so it will be responsive.
The last option (of creating new thread) is easy and faster for beginners.

Mitja Bonca 557 Nearly a Posting Maven

Its hard to tell what can be wrong - obviousely the code hangs somewhere in between, in some loop. That means that the code came into an infinitive loop - it will circle forever. Why? I cannot tell you... but can you please share you code here, so we can help you find the error in the code?

Mitja

Mitja Bonca 557 Nearly a Posting Maven

Becuase you are using static methods, you can only use the objects inisde of them, and whats is passed from one to another. This is what you can do:

class MyClass
{   
    public static void Main(string[] args)
    {        
       Pieces[,] = ChessBoard();
    }

    public static Pieces[,] ChessBoard( )
    {
        Pieces [,] pieces = new Pieces[9,9];
        //code to fill the pieces (which you have alreay)
  
        return pieces;
    }
}

This will do it.
Mitja

Mitja Bonca 557 Nearly a Posting Maven

This can help you out:
1. the currency depending on the culture,
2. the currency in your wanted format without any cash mark

int iValue = 2879068;
string sValue1 = String.Format("{0:C}", iValue);
string sValue2 = String.Format("{0:#,#.}", iValue);

but in case if you want to have some cash mark, simply do:

{0:$#,#.}//or
{0:#,#.€}

Hope it helps,
Mitja

Mitja Bonca 557 Nearly a Posting Maven

How will you choose 3,5 or 7 option? You have to do an option to let the user chooses it, and then its simple - get the number user has choosen, and pass it to the method which will create the field of buttons to play:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jan08Exercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.comboBox1.Items.AddRange(new string[] { "3x3", "5x5", "7x7" });
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string item = comboBox1.GetItemText(comboBox1.SelectedItem);
            if (item != String.Empty)
            {
                string sNumber = item.Substring(0, item.Length - 2);
                int value = Convert.ToInt32(sNumber);
                CreatingField(value);
            }
        }

        private void CreatingField(int value)
        {
            for (int x = 0; x < value; x++)//loop relating to rows
            {
                for (int y = 0; y < value; y++)//loop relating to columns
                {
                    nandcButton[x, y] = new Button();//Create the Buttons
                    nandcButton[x, y].Name = "nandcButton" + x + "," + y;//name and coordinates and set up number, number to show
                    nandcButton[x, y].Width = 60;
                    nandcButton[x, y].Height = 60;
                    nandcButton[x, y].Left = nandcButton[x, y].Left + nandcButton[x, y].Width + (x * 60);
                    nandcButton[x, y].Top = nandcButton[x, y].Top + nandcButton[x, y].Top + 50 + (y * 60); //Centre the button 
                    nandcButton[x, y].Click += new EventHandler(Button_Click); //links the above button spec.
                    //Add them to the container
                    nandcHolder.Controls.Add(nandcButton[x, y]);
                    nandcButton[x, y].Text = " "; //declares text
                }
            }
        }
    }
}

Hope it helps a bit.
If this is not something you wanted, …
Mitja Bonca 557 Nearly a Posting Maven

Maybe this will do? :

string filename = "f:\\date.txt";
            using (StreamReader stream = new StreamReader(filename))
            { 
                string line;
                while ((line = stream.ReadLine()) != null)
                {
                    string[] array = line.Split(' ');
                    int a = Convert.ToInt32(array[0]); //1.
                    string b = array[1];               //2.
                    string c = array[2];               //3.
                }
            }
Mitja Bonca 557 Nearly a Posting Maven

Is the string seperated by something? By whitespaces maybe?

Mitja Bonca 557 Nearly a Posting Maven

I dont find anything that can be connected with if,else loops in your code, ok it could be, but it would be too complicated.
In my opinion fawi`s approach is the right one, I will only modify it a bit, to suit your needs:

bool flag = false;
            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    if (nandcButton[i, j] == "0")
                        flag = true;
                }
            }
            if (flag == true)
            {
                //the requirment has been fulfiled!
            }

Hope this helps,
Mitja

johnt68 commented: Thanks for the help +1
Mitja Bonca 557 Nearly a Posting Maven

Maybe this might help you out:

private void Form1_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.MyPic.ico; //MyPic is the icon in your resources!
        }
Mitja Bonca 557 Nearly a Posting Maven

He was saying the same thing as I was (no worries).

best regards,
Mitja

Mitja Bonca 557 Nearly a Posting Maven

This is some other solution:

void ReversingMethod()
        {
            string str = "This is a string that needs to be reversed";
            Array array = str.Split(' ');
            Array.Reverse(array);
            str = null;
            for (int i = 0; i < array.Length; i++)
            {
                str += array.GetValue(i) + " ";
            }
            MessageBox.Show("This is a reversed string:\n" + str);
        }
Mitja Bonca 557 Nearly a Posting Maven

This is what I meant:

// read CSV file
        private DataTable BuildDataTable(string fileFullPath, char seperator)
        {
            StreamReader myReader; //put a new instance of StreamReader here, so it will be accessable in whole method
            const int EOF = -1;

            DataTable myTable = new DataTable("MyTable");
            DataRow myRow;
            try
            {
                myReader = new StreamReader(fileFullPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening streamreader: " + ex.Message);
                return new DataTable("Empty");
            }
            try
            // Open file and read first line to determine how many fields there are.
            {
                string[] fieldValues = myReader.ReadLine().Split(new Char[] { seperator, ';' });
                // Adding the first line of data to data table (columnnames?
                // Create data columns accordingly
                for (int i = 0; i < fieldValues.Length; i++)
                {
                    myTable.Columns.Add(new DataColumn(fieldValues[i].ToString().Trim()));
                }
                //Now reading the rest of the data to data table
                while (myReader.Peek() != EOF)
                {
                    fieldValues = myReader.ReadLine().Split(new Char[] { seperator });
                    myRow = myTable.NewRow();
                    for (int i = 0; i < fieldValues.Length; i++)
                    {
                        myRow[i] = fieldValues[i].ToString().Trim();
                    }
                    myTable.Rows.Add(myRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error building datatable: " + ex.Message);
                return new DataTable("Empty");
            }
            finally
            {
                if (myReader != null)
                {
                    myReader.Close();
                }
            }
            myTable.AcceptChanges();
            return myTable;
        }

If this is not it, please let me know.
What bothers me, are those 2 chatch bloces. Do you have any issues with them? If not, its ok.

ddanbe commented: For the effort! +8
Mitja Bonca 557 Nearly a Posting Maven

Why dont you put a StreamReader object out of any loops, so it can be accessible to any other sub brackets, loop, etc, like:

StreamReader myReder;
            DataTable myTable = new DataTable("MyTable");
            DataRow myRow;
            try
            {
               myReader = new StreamReader(fileFullPath);
            }

            //code continues..
Mitja Bonca 557 Nearly a Posting Maven

This can be some kind of a leading way for you. There is plenty of way how to implement your issue, but one of my best and very efficient is to use while loop.
While some value (usually is a boolean) is not the contrary of the pre-set one, the loop is going on to infinity, until some goal is not reached. In your case this goal is when the user enters something (what ever you want).
You can even put some code for validating age, tel.numbers, and stuff like that - I actaully did an example for checking the integer - users age (but if you would like to know more, please start a new thread).

This is your code:

class Program
    {
        static void Main(string[] args)
        {
            bool bDataOK = false;
            string value = null;

            Console.WriteLine("Please write your name:"); 
            while (!bDataOK)
            {                
                value = Console.ReadLine();
                if (value != String.Empty)
                    bDataOK = true;       
                if(!bDataOK)
                    Console.WriteLine("You forgot to enter you name...");  
            }

            //now when the user has entered his bame, we can continue with the code:
            Console.WriteLine("Please enter your age:");
            bDataOK = false;
            while (!bDataOK)
            {                
                value = Console.ReadLine();
                if (value != String.Empty)
                {
                    //now checking for the age (if user really entered an integer:
                    bDataOK = CheckingAge(value);
                    if (bDataOK)
                        bDataOK = true;
                }
                if(!bDataOK)
                    Console.WriteLine("You forgot to enter you age...");  
            }
        }

        private static bool CheckingAge(string sAge)
        {
            int iAge = 0;
            bool bChecking = int.TryParse(sAge, out iAge);
            if (bChecking)
                return true;
            else
                return false; …
Mitja Bonca 557 Nearly a Posting Maven
Mitja Bonca 557 Nearly a Posting Maven

What are you trying to get with using Read() method?
This method returns an integer (not a string) so that it can return -1 if the end of the stream has been reached (like you did in your example), but I still dont get what are you trying to get - if you want to get the string inside the file, it will simply not go.
This why you have to use ReadLine(), or method like adapost has shown you in post above.

I hope this helps clearing the issue here.
best regards,
Mitja

Mitja Bonca 557 Nearly a Posting Maven

I have chnaged a code a bit, and now it works:

private void ReadingFile()
        {
            using (StreamReader sr = new StreamReader(@"C:\1\map.txt"))
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    if (line != String.Empty)
                        label1.Text = line + Environment.NewLine;
                }
            }
        }

I hope it does work for you too.
Mitja

Mitja Bonca 557 Nearly a Posting Maven

Would you mind giving me the content of the file? So I can try it on my own.

btw, where you read: Voronezh 20 20 2 4 5

Mitja Bonca 557 Nearly a Posting Maven

Depends what you want to do/use. It this case you dont even need to use get,set accessors, but actually you dont need to use get, set at all. You can read some explanation HERE.

Regarding your question, you can try this way:

private int hello = 5;

        static void Main()
        {
            Console.WriteLine("Enter val:");
            int val = Convert.ToInt32(Console.ReadLine());
            Program instance = new Program();
            Console.WriteLine("VALUE OF HELLO IS {0}", instance.getPrice());
            instance.hello2(val);
            Console.WriteLine("HELLO IS NOW: {0}", instance.hello);
            Console.ReadLine();
        }
        public int getPrice()
        {
            return this.hello;
        }

        private int hello2(int n)
        {
           return this.hello = n;
        }
Mitja Bonca 557 Nearly a Posting Maven

Where it hangs up? Or is it tryig to go into infinity in the while loop? If so, that means its still reading the file - mayne you read a file byte by byte (like I showed you on the previous example) and this is taking very long time - depends of the file lenght.
Otherwise I cannot see any other reason to hang up.
Take a look in a debug mode (put the break point and go line by line with pressing F11).

Hope it helps,
Mitja

Mitja Bonca 557 Nearly a Posting Maven

hmm... Did you save the data into a file in the same order then you are retreiving them back out?

the point is that you cannot just simply asign some binary data to something. You have to get the data into the text in some order, and then retreive them back in the same way:EXAMPLE!

If this is not enough its better to take a look into some other example, that you will learn how to use bytes and converted them back to the characters (strings, integers,...).

Give it some time and go through all the code slowely:
http://msdn.microsoft.com/es-es/library/system.io.binaryreader.aspx

Mitja Bonca 557 Nearly a Posting Maven

An example:

public class MyClass1
    {
        private int price1;
        public int price2;

        public MyClass1()
        {
            int newPrice1 = price1;
            int newPrice2 = price2;
        }
    }

    public class MyClass2
    {
        public MyClass2()
        {
            MyClass1 class1 = new MyClass1();
            class1.price2;
            //class1.price1 IS NOT ACCESSIBLE FROM HERE!
        }
    }
Mitja Bonca 557 Nearly a Posting Maven

Becuase the private field (price) is created inside a Program class.
Even you have called it from a static method, which only can access to the parameters inside of it, you created a new instance of a Program class, and used this instance to connect to the private field.

Pritave modifier are still accessed from inside of the class. If you create a new class, and create a new instance of a Program inside of it, then "price" field will not be accessed any longer.

Mitja

Mitja Bonca 557 Nearly a Posting Maven

Not in english? So this is useless then.
But there is the code you wanted - to read character by character:

using (StreamReader sr = new StreamReader(path)) 
            {
                //This is an arbitrary size for this example.
                char[] c = null;

                while (sr.Peek() >= 0) 
                {
                    c = new char[1]; //1 character at a time
                    sr.Read(c, 0, c.Length);
                    //The output will look odd, because
                    //only five characters are read at a time.
                    Console.WriteLine(c);
                }
            }

Hope it does some help to you.

Mitja Bonca 557 Nearly a Posting Maven

Try this:

this.dataGridView1["columnIndex", "rowIndex"] = new DataGridViewTextBoxCell();
Mitja Bonca 557 Nearly a Posting Maven

Can you some me the code with which you create a dgv and its columns?

Mitja Bonca 557 Nearly a Posting Maven

btw, why do you use a BinaryRader class, becuase you only read from a text file?
Why you dont use StreamReader instead, and you wont even need to convert byte into string, something?

An idea.
Mitja

Mitja Bonca 557 Nearly a Posting Maven

What exactly do you mean?
Here is some info about using commands:
http://www.daniweb.com/code/snippet303280.html

Mitja Bonca 557 Nearly a Posting Maven

You can do it like that:

using (StreamReader reader = new StreamReader(@"C:\myFile.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                label1.Text += line + Environment.NewLine;
            }
        }
Mitja Bonca 557 Nearly a Posting Maven

Why do you want to encode a text? A text its alredy encoded, dont you think. You only need to get it from the file into a label.

vedro-compota commented: +++++ +1
Mitja Bonca 557 Nearly a Posting Maven
Mitja Bonca 557 Nearly a Posting Maven

This error might occur becuase you attempted to insert a row containing a specific identity value into a table that contains an identity column.

Now, you must specify the column list of the Product column when u use the IDENTITY_INSERT ON.

Check for the colution here:http://www.sql-server-helper.com/error-messages/msg-8101.aspx

Mitja Bonca 557 Nearly a Posting Maven

I dont understand what exactly do you mean? Can you be a bit specific about this issue here?

Mitja Bonca 557 Nearly a Posting Maven

This is the parameter you have issues, right?:

da.Parameters.Add("@Available", SqlDbType.Bit).Value = textBox7.Text;

Bit can only have "true" or "false" values in th database.
What is your value in textBox7?

Its you have 1 or 0 do a conversation to true or false.

EDIT:
and instead of textBox6, in which you have date, you better use dateTimePicker ot monthCalendar. In this case there will never come to any inconveniances in capturing the right date value.

Mitja Bonca 557 Nearly a Posting Maven
//get the selected item:
string value  = listBox1.SelectedItem.ToString();
//delete it:
listBox1.Items.Remove(value);

there is another way to get the selecte index (thats an integer) and then remove with using method "RemoveAt", like:

int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
ddanbe commented: You got a point here. +8