-5

How can I convert int (StaffID) to string in the below method. It wont accept Guid.NewGuid(ToString)

public void submitStaffID(StaffEnity staff)
{
    staff.StaffID = Convert.ToInt32(Guid.NewGuid(ToString));
    StaffEntity.Add(staff);
}

No overload for method NewGuid takes 1 argument.

namespace StaffEntity
{
    [DataContract]
    public class StaffEnity
    {
        [DataMember]
        public int StaffID;
        [DataMember]
        public string Forename;
        [DataMember]
        public string Surname;
        [DataMember]
        public DateTime TimeAdded;
    }
}
pb2q
  • 58,613
  • 19
  • 146
  • 147
G Gr
  • 6,030
  • 20
  • 91
  • 184
  • http://www.youtube.com/watch?v=WAq_Z-dT67o im following this tutorial but instead of string I declared my StaffID as an Int. (5 minutes into the video) – G Gr Mar 16 '12 at 00:39
  • 1
    Read [my answer](http://stackoverflow.com/a/9730264/601179), you can't convert `Guid` to int, I'm pretty sure you will get an exception in the conversion. – gdoron Mar 16 '12 at 00:40
  • 8
    I rolled back to your previous question. **You can't edit a question to a new one because you're prevented asking new questions.** it's invalid edit which cause all of answers and comments looks stupid and unrelated. – gdoron Mar 16 '12 at 01:37
  • ah ok first time I have ever had this 6 questions per day thing :S – G Gr Mar 16 '12 at 01:38

6 Answers6

3

You're looking for this:

staff.StaffID = Convert.ToInt32(Guid.NewGuid().ToString());

But converting it afterward to int32 won't work!

From MSDN:

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

An int structure, on the other hand:

Represents a 32-bit signed integer.

So, you can't convert from Guid to int without loosing most of the information.

https://stackoverflow.com/questions/4518684/convert-guid-to-int

You probably meant this

staff.StaffID = new Random().Next(int.MinValue, int.MaxValue);

Note for proper use of the Random class:
Random number generator only generating one random number

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • so what do i do if i want int as my staff id? – G Gr Mar 16 '12 at 00:43
  • actually wait wouldnt it be convert int tostring – G Gr Mar 16 '12 at 00:46
  • @Garrith. Use `Random` see my update – gdoron Mar 16 '12 at 00:48
  • something like `staff.StaffID = Convert.ToString(Guid.NewGuid);` – G Gr Mar 16 '12 at 00:48
  • No I realised my mistake in the first attempt what I ment was to try convert my staffid (int) to string – G Gr Mar 16 '12 at 00:49
  • @Garrith. that code will give you `string` and probably equals to `.ToString()` which you already have. You wrote you need an `int` and not a `string` . ? – gdoron Mar 16 '12 at 00:50
  • @Garrith. I got confused! **what are you trying to do?** get a random int? parse guid to int\string parse int t string? what? – gdoron Mar 16 '12 at 00:52
  • In the video he creates a string ID but I changed it to Int StaffID what I need to do is convert my int to string using whatever the Guid.NewGuid is? – G Gr Mar 16 '12 at 00:53
  • +1 for being first to mention that his method is actually wrong, but this is not a good idea either: `new Random().Next`. The problem here is that, if he calls this many times quickly, he will get a bunch of duplicates because it uses the same seed each time. Better to just not us a random number for the id to begin with or make the id a `Guid` and be done with it. – Ed S. Mar 16 '12 at 00:53
  • @Garrith. just take your int and use it's `.ToString()` method. like this: `var theString = staff.StaffID.ToString();` – gdoron Mar 16 '12 at 00:54
  • Yeah but I still dont know how to convert my staffID (int) to string using the Guid method? – G Gr Mar 16 '12 at 00:55
  • @EdS.. You're right on the Random part, I just gave an example, I'll look for marc garvell anwer about it and add it to my answer. – gdoron Mar 16 '12 at 00:56
  • @Garrith. **You can't use an int to get a `Guid` it's like taking a wheel and convert it to a car.** It can't be done. forget about the `Guid` if you're using `int`! – gdoron Mar 16 '12 at 00:58
  • SO if I use Ed S method `Guid StaffId { get; set; }` as the DataMember how do I add staff to my StaffEntity list? – G Gr Mar 16 '12 at 01:04
  • 1
    @Garrith. You need to create a `List` like with this: `var list = new List(); list.Add(staff);` Please open an new thread with an new question. stackoverflow complains with too much comments. – gdoron Mar 16 '12 at 01:10
  • ugh your only allowed 6 questions in a 24 hour period now? God thats awful. – G Gr Mar 16 '12 at 01:30
  • Ive updated this question to reflect what I need – G Gr Mar 16 '12 at 01:32
2

Well, the error message is telling you the problem... Guid.NewGuid does not take any arguments. You placed ToString inside the method call when you meant to place it after.

staff.StaffID = Convert.ToInt32(Guid.NewGuid().ToString());

Which of course won't work correctly as a Guid represents a 128-bit value, much too large for an int.

Why not make StaffId a Guid to begin with (not a Guid converted to something else, but Guid StaffId { get; set; }) or not a random value?

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Yeah I have never understood it? I dont know what it means when it says arguement. – G Gr Mar 16 '12 at 00:36
  • 2
    @Garrith: "Arguments" are what you pass to a function. If I have `void Foo(int x)` and I call it like so `foo(10)` `10` is the argument that `x` represents inside of the function. – Ed S. Mar 16 '12 at 00:37
  • Yes it will work, leaving him with an exception one moment later... =) – gdoron Mar 16 '12 at 00:45
  • @gdoron: And so he will... that I did not look into. – Ed S. Mar 16 '12 at 00:47
  • ok so if I do that do I just leave StaffEntity.Add(staff); in my submitstaffID area? – G Gr Mar 16 '12 at 00:59
1

I think Guid.NewGuid().ToString() will work.

GrantVS
  • 2,073
  • 1
  • 24
  • 27
1

Perhaps you mean to do this:

staff.StaffID = Convert.ToInt32(Guid.NewGuid().ToString());
Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
0

What exactly are you trying to do? What does the "ToString" variable represent?

Also, the Guid.NewGuid method doesn't have any arguments. The proper use would just be Guid.NewGuid() instead of Guid.NewGuid(ToString).

This will generate a random GUID for you. If you are worried about collision, don't mind it. The chances of generating the same GUID twice are really small.

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
0

ToString() is a method you apply on.

Guid.NewGuid().ToString() should give you a new guid

Shyju
  • 214,206
  • 104
  • 411
  • 497