.NET Thread

Cromewell

Administrator
Staff member
Please post any questions relating to .NET here. Please indicate which language you are using and also specify which external libraries, if any, you are using.

You can also use this thread to post code you wish to share.
 

Aastii

VIP Member
Little bit of background - doing a program for my computing A-level and I need a log in system for part of it.. It only needs to be there and I get a crap ton of marks (it links in with other stuff, hence the alot of marks for a simple algorithm. The algorithm/code part is only worth maybe 1 mark, but what it ties in with gets a lot extra). However, the program I am making will, once I've made it into a program rather than just part of my qualifications course, actually be used, so of course I need more security than just log in and make an account, because it is easy to just go to the usernames file. This is extra outside of the course basically.

It doesn't need to be super secure, the people using it won't be very computer literate at all, and there isn't any sensitive information held directly other than NHS numbers (the number used to identify you in the NHS here in Britain), but you can't actually do anything with someone's NHS number unless you can go to the huge database of all of them.

I'm running it in VB.net 2010, and up to now have already make the bones of the program, and enough to get me most all of the marks in it. What I have currently done for the log in system:

Code:
        uname = txtUser.Text
        pword = txtPass.Text
        counter = 0
        flag = 0

        'Open file for reading into Current array, then close the file once all files are in the array
        'Counter to track how many entries are in the file

        FileOpen(1, CurDir() & "\Users.txt", OpenMode.Input)

        While Not EOF(1)
            Input(1, Current(counter).username)
            Input(1, Current(counter).password)
            counter = counter + 1
        End While

        FileClose(1)

        'Compare the username and password input to the usernames and passwords in the users file
        'And change a flag accordingly to true if there is a match, else false if there is not

        For index = 0 To counter - 1
            If flag = False Then
                If Current(index).username = uname Then
                    If Current(index).password = pword Then
                        flag = 1
                    Else : flag = 0

                    End If
                Else : flag = 0

                End If

            End If
        Next

        'If the flag is true, close the form and load the main program, otherwise display a message that
        'the user name and/or password are incorrect

        If flag = True Then
            Me.Hide()
            frmHome.Show()
        Else
            MsgBox("Invalid Username or Password")
        End If



    End Sub

Which works, but as I say, isn't secure, and how to go about encrypting the username + password, I'm not sure
 

tlarkin

VIP Member
I don't know .NET really, but why not prompt for the user name and password? I am sure there is a way to get user interaction in .NET. Otherwise, putting usernames/passwords in apps/scripts is never a good idea. Even if you obfuscate it, not a good idea.
 

Cromewell

Administrator
Staff member
It looks to me that he is prompting for it, via a textbox. The encryption is for storing them in a database (or flat file as the case appears to be right now).
 

WeatherMan

Active Member
This is a bit of an off topic question, but I am doing Visual Basic for my Software Development Unit.

What's the difference between between Visual Basic & VB.net?

No this isn't coursework :p I am just curious.
 

Aastii

VIP Member
It looks to me that he is prompting for it, via a textbox. The encryption is for storing them in a database (or flat file as the case appears to be right now).

Close, it is actually 2 text boxes for input on a form that appears as the home for the program. You must log in before you are able to access anything, be it add data, edit or query the data.

And the data is indeed for storage. It will be stored encrypted, and when you put in the username and password it would be decrypted, checked against what has been typed in

This is a bit of an off topic question, but I am doing Visual Basic for my Software Development Unit.

What's the difference between between Visual Basic & VB.net?

No this isn't coursework :p I am just curious.

VB.NET is part of the .NET framework from Microsoft (C.NET, C#.NET, C+.NET etc). It has certain pieces of code and ways to manipulate data that are different to VB6, but they are essentially the same thing. Microsoft just changed some stuff, and grouped all of the languages together into a single package - Microsoft Visual Studio
 

Aastii

VIP Member
Alright, I've hit another wall

Trying to write to my file, I've used a single object to test if it will go in before I go putting 40+ pieces of data in, or rather the code for it, just to find I did it wrong and wasted my time :p

I've got a combo box with options for hospital's a patient is at to collect the data.

That is then read into a public array called template, which is of a structure called Patrec, which has a location for every different piece of data, so everything read into the array, then bulk written to the text file.

To write the hospital to the array:



Code:
    Private Sub cmbHosp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbHosp.SelectedIndexChanged
        template.hospital = cmbHosp.SelectedIndex
    End Sub

And then, at the end of putting the data in (spread over 3 forms for clarity):

Code:
 FileOpen(1, CurDir() & "\Hospital.txt", OpenMode.Append)

        Write(1, template.hospital)

        FileClose(1)
 
Last edited:

Aastii

VIP Member
Doesn't matter, I sorted it. The problem was using Input rather than WriteLine, and looking in the wrong directory :p
 

kobaj

VIP Member
C# question (not pertaining to .net in any way really, but C# uses the .net framework and I didn't feel like making a new thread...)

I'm toying around with C# generics <T>, and inheritance, and basically wondered if the following was a bad way of doing things,

Code:
namespace K_OS_SlimDX
{
    public class CustomVertex
    {
        public class TransformedColored : CustomVertex
        {
        }
    }
}

You'll note that TransformedColored inherits from CustomVertex...which has a TransformedColored that inherits from CustomVertex...which (and its recursive all the way down).

But it compiles, without a hitch. So I take it my code is ok?

For the record, I know the correct way is like below. But I really just want to know why the above works?

Code:
namespace K_OS_SlimDX
{
    abstract class CustomVertex
    {
    }
        public class TransformedColored : CustomVertex
        {
        }
}
 

Cromewell

Administrator
Staff member
the thing with creating the class as you did in #1 is that it only exists inside the scope of CustomVertex. I don't know how that inheritance will work though.
 

S.T.A.R.S.

banned
The "abstract" modifier is used in classes,methods and properties.Abstract modifier is used when you want the class to be a base class for other classes.
The modifiers which are not allowed in an "abstract" method declaration are: "static","virtual" and "override".
An "abstract" class has to provide implementations for all of its interface members and CANNOT be a static logically since you cannot even use the "static" keywords to create the static methods.
For example:

abstract class ComputerForum
{
public abstract void LifeSucks(); //This is a method...
}
class ThisForumRules:ComputerForum
{
public override void LifeSucks()
{
MessageBox.Show("It sure does :p");
}
}

And then somewhere such as in the simple button CLICK event you can execute the method by creating the new instance of the class called "ThisForumRules" by doing it this way:

ThisForumRules omgReally=new ThisForumRules();
omgReally.LifeSucks();


So in this example the ABSTRACT class called "ComputerForum" contains the public abstract void method called "LifeSucks" which we then used in the class called "ThisForumRules" which took it's basis from "ComputerForum" abstract class.And then in the class called "ThisForumRules" we simply wrote a simple code which will be executed each time the "LikeSucks" method is called.In this case it is a simple message box.

So in your case if you want to have class and then the class INSIDE that class,you should more use ABSTRACT class to store methods which later you can use in ANY classes and initiate them from any events by creating the new instances of those classes.




Cheers!
 

M1kkelZR

Active Member
i have a question about C#.

so we have to use 3 checkboxes and all of the check boxes has its own numerical value.

heres an example for what i have.

Code:
i private void btnBereken_Click(object sender, EventArgs e)
        {
            int beurs = Convert.ToInt32(chkBeurs.Checked);
            int kamers = Convert.ToInt32(chkKamers.Checked);
            int actief = Convert.ToInt32(chkActief.Checked);
            int som;
            if (chkBeurs.Checked)
            {
                beurs = 5;
            }
            else if (chkKamers.Checked)
            {
                kamers = 5;
            }
            else if (chkActief.Checked)
            {
                actief = 10;
            }
            som = 30 - beurs - kamers - actief;
            txtTotaal.Text = Convert.ToString(som);
        }

Problem is it wont calculate everything right.

it will calcualte it if i only check 1. but if i have more than 1 checked it will somehow think it has to make all of them (5,5 and 10) and make it 6. so it will be 30-6

help please?
 
Last edited:

Cromewell

Administrator
Staff member
The problem is your code can only hit one of the branches. Fortunately, it is an easy fix:
Code:
i private void btnBereken_Click(object sender, EventArgs e)
        {
            int beurs = Convert.ToInt32(chkBeurs.Checked);
            int kamers = Convert.ToInt32(chkKamers.Checked);
            int actief = Convert.ToInt32(chkActief.Checked);
            int som;
            if (chkBeurs.Checked)
            {
                beurs = 5;
            }
            if (chkKamers.Checked)
            {
                kamers = 5;
            }
            if (chkActief.Checked)
            {
                actief = 10;
            }
            som = 30 - beurs - kamers - actief;
            txtTotaal.Text = Convert.ToString(som);
        }
That said, the behaviour looks weird. You seem to be setting each variable to 1 or 0 depending on if it is checked then checking if it's checked and setting it to a new value.
 
Last edited:

M1kkelZR

Active Member
The problem is your code can only hit one of the branches. Fortunately, it is an easy fix:
Code:
i private void btnBereken_Click(object sender, EventArgs e)
        {
            int beurs = Convert.ToInt32(chkBeurs.Checked);
            int kamers = Convert.ToInt32(chkKamers.Checked);
            int actief = Convert.ToInt32(chkActief.Checked);
            int som;
            if (chkBeurs.Checked)
            {
                beurs = 5;
            }
            if (chkKamers.Checked)
            {
                kamers = 5;
            }
            if (chkActief.Checked)
            {
                actief = 10;
            }
            som = 30 - beurs - kamers - actief;
            txtTotaal.Text = Convert.ToString(som);
        }
That said, the behaviour looks weird. You seem to be setting each variable to 1 or 0 depending on if it is checked then checking if it's checked and setting it to a new value.

well the thing it was supposed to be is if you check the first one, the total which is 30 has to have 5 taken off, and same with the second and third but the third takes 10 off.

then i needed to make it read multiple checks but for some reason it will only do this 30 - 5 then it will se the other 2 checks as 1 instead of 10 or 5. my teacher explained it and i was like yeah right... im a game and server programmer not a simple calculation programmer lol

but atleast i now know what i did wrong. instead of if-if-if i did if-if else-if else. which means it will look for alternatives and change the values of the other checks as it cant figure it out.

thanks. i think you'll see alot of simple stuff that ill post here, just because i overthink everything. if the forum had a thanks or rep button youd get one straight away haha! so thanks man i appreciate the help
 
Last edited:

S.T.A.R.S.

banned
Keywords IF,ELSE IF and ELSE cannot be used all at the same time.
IF executes statements if it matches whatever you want to match.If not then it automatically goes to ELSE IF to see if it matches whatever you want to match.If not then it automatically goes to ELSE which always executes if NONE of your checks (IF or ELSE IF) match anything you want to match.For example:

if(textBox1.Text=="One")
{
//Execute the code if it matches 1 and skip the rest...
}
else if(textBox2.Text=="Two")
{
//Execute the code if it matches 2 and skip the rest...
}
else if(textBox3.Text=="Three")
{
//Execute the code if it matches 3 and skip the rest...
}
else
{
//Execute the code if first 3 things do not match whatever is that you want to match...
}



You can put ELSE IF posibilities as much as you want.
You can also use SWITCH keyword for the same purposes,but I rather use IF.



Cheers!
 

Cromewell

Administrator
Staff member
well the thing it was supposed to be is if you check the first one, the total which is 30 has to have 5 taken off, and same with the second and third but the third takes 10 off.

then i needed to make it read multiple checks but for some reason it will only do this 30 - 5 then it will se the other 2 checks as 1 instead of 10 or 5. my teacher explained it and i was like yeah right... im a game and server programmer not a simple calculation programmer lol

but atleast i now know what i did wrong. instead of if-if-if i did if-if else-if else. which means it will look for alternatives and change the values of the other checks as it cant figure it out.

thanks. i think you'll see alot of simple stuff that ill post here, just because i overthink everything. if the forum had a thanks or rep button youd get one straight away haha! so thanks man i appreciate the help

No worries. Simple stuff is fine, gotta start somewhere right?

I didn't want to rewrite your code before, just show you why you were only getting 1 of the checks to take effect. But out of curiosity, why not write that checkbox logic this way?
Code:
i private void btnBereken_Click(object sender, EventArgs e)
        {
            int som = 30;
            if (chkBeurs.Checked)
            {
                som -= 5;
            }
            if (chkKamers.Checked)
            {
                som -= 5;
            }
            if (chkActief.Checked)
            {
                som -= 10;
            }
            txtTotaal.Text = Convert.ToString(som);
        }
 

M1kkelZR

Active Member
No worries. Simple stuff is fine, gotta start somewhere right?

I didn't want to rewrite your code before, just show you why you were only getting 1 of the checks to take effect. But out of curiosity, why not write that checkbox logic this way?
Code:
i private void btnBereken_Click(object sender, EventArgs e)
        {
            int som = 30;
            if (chkBeurs.Checked)
            {
                som -= 5;
            }
            if (chkKamers.Checked)
            {
                som -= 5;
            }
            if (chkActief.Checked)
            {
                som -= 10;
            }
            txtTotaal.Text = Convert.ToString(som);
        }

well now that i look at it it would make more sense to actually state the answer in the checkbox and have the calculation take effect in the checkbox if its checked. makes the end line less cluttered in my eyes.

dont get me wrong im a good programmer for games etc but these simple things just break my balls really. overthinking is just one of the many bad things i have. but as you said gotta start somewhere, and ill start here!
 
Top